Reprint and indicate the source: http://blog.csdn.net/xiaohanluo/article/details/53214933
Git installation
There are two ways to download Git
- Download the installation package directly, Git download address
- Download it with the homebrew command, but first you need to install homebrew
- Execute the following instructions in the terminal to install,
homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Once done, install Git using the later command,
brew install git
- Execute the following instructions in the terminal to install,
After installing Git, configure the username and user email address, which will be used for every interaction with Git in the future.
git config --global user.name "your_name"
git config --global user.email "[email protected]"
- 1
- 2
The configuration information can be changed, and you can use the above command to change it later. At the same time, you can use the git config --list
command to view the configuration information of Git.
Git is case-insensitive by default, that is to say, if a letter in a file name is case-converted, Git ignores the change, resulting in errors when synchronizing the code, so it is recommended to set Git to Case Sensitive.
git config core.ignorecase false
- 1
generate key
When Git associates with a remote repository, it needs to provide the public key and save the private key locally. Every time it interacts with the remote repository, the remote repository will use the public key to verify the identity of the interactor. Use the following instructions to generate a key.
ssh-keygen -t rsa -C "[email protected]"
/Users/当前电脑用户/.ssh
After the key is generated, two files id_rsa
, id_rsa.pub
, will be generated in the local directory. The id_rsa
file stores the private key, which is stored locally, and the id_rsa.pub
file stores the public key. The contents need to be uploaded to the remote warehouse.
The specific operation for obtaining the public key string is as follows.
Figure-1 Get the public key
- Enter
cd
the command to enter the current user directory - Enter
ls -a
the command to view all files in the current user directory, including hidden files - Enter
cd .ssh
the command to enter the.ssh
directory - Enter the
ls
command to view.ssh
the files in the directory - Enter the
cat id_rsa.pub
command to viewid_rsa.pub
the content of the file
Add key to remote warehouse
Taking GitHub as an example, add the public key to the remote repository. The public key has been obtained above, and you only need to add the public key to the remote repository.
Figure-1 Add public key
On the personal settings page, select SSH and GPG keys
it on the left, and add the public key on the right. It title
is the name of the key, which can be taken or changed. It key
is the public key we obtained above. After filling in, click the add SSH key
button, so that the remote end will be added to the key.
Local association with remote warehouse
- Execute the command under the local folder
git init
to initialize the folder as a local warehouse. - implement
git remote add origin 仓库的ssh链接
- Execute the
git pull
command to pull the remote code to the local
After the execution is complete, the remote warehouse code has been synchronized to the local.
Common Git commands
-
Initial configuration
git config —global user.name “XXX" git config --global user.email “XXXX"
- 1
- 2
-
set case sensitive
git config core.ignorecase false
- 1
-
generate key
ssh-keygen -t rsa -C “your_email”
- 1
-
Clone remote code to local directory
git clone <远端git> <本地目录>
- 1
-
Submit changes
git add <文件> // 将有修改的文件添加到本地缓存中 git add . 是添加所有修改 git commit -m "本次修改信息" // 提交本次修改,一般是在git add之后操作 git reset . // 撤销add git rm --cached . // 撤销add
- 1
- 2
- 3
- 4
-
switch branch
git checkout <branch_name>
- 1
-
Create a new branch based on the current branch and switch to the new branch
git checkout -b <branch_name>
- 1
-
current branch merges other branches
git merge <branch_name>
- 1
-
To create a new branch at the remote end is actually to push the local branch to the remote end
git push origin <local_branch_name>:<remote_branch_name>
- 1
-
Deleting a remote branch is actually pushing an empty branch to the remote end, overwriting the original remote branch
git push origin :<remote_branch_name>
- 1
-
Pull the branch from the remote end and establish the corresponding relationship
git checkout -b <local_branch_name> origin/<remote_branch_name> // 或者 git branch —track <local_branch_name> origin/<remote_branch_name>
- 1
- 2
- 3
-
Establish a corresponding relationship between the existing local branch and the remote branch
git branch —set-upstream <local_branch_name> origin/<remote_branch_name>
- 1
-
Add remote library
git remote add <远端库代称> <远端库地址> git clone <远端库地址> <目录> //不需要git init
- 1
- 2
-
rollback to a commit version
git reset --hard/soft <commit_id> // 回滚到某一个版本 git reset --hard/soft HEAD~<num> // 回滚num个提交 git revert <merge_commit_id> -m number // 撤销某一次merge
- 1
- 2
- 3
-
Force the remote to overwrite the local
git fetch --all git reset --hard origin/<remote_branch_name>
- 1
- 2
-
How to view the commit log
git log -p 每一次提交具体差异 git log —stat 显示文件修改差异,没显示具体修改 git log —graph 树形状提交记录,可查看分支合并信息
- 1
- 2
- 3
-
git pull —rebase
When there is a conflict, resolve the conflict, usegit add .
then usegit rebase --continue
-
switch to a branch and apply a commit from another branch to that branch
git checkout <branch_name> git cherry-pick <commit id>
- 1
- 2
-
The former means to pick the commit cherry-pick between (left open and right closed, excluding start-commit-id) to the current branch; the latter means to commit cherry-pick between to (closed interval, including start-commit-id) -pick to the current branch.
git cherry-pick <start-commit-id>..<end-commit-id> git cherry-pick <start-commit-id>^..<end-commit-id>
- 1
- 2
-
tag
-
new tag
git tag <tag_name> //轻量标签 git tag -a <tag_name> -m "tag_msg" // 附注标签 git tag -a <tag_name> <commint_id> // 给某次提交添加标签
- 1
- 2
- 3
-
delete tag
git tag -d <tag_name>
- 1
-
View tags
git tag // 查看所有tag git show <tag_name> // 查看某条tag
- 1
- 2
-
Submit to remote
git push origin <tag_name> // 将某个tag提交到远端 git push origin –tags // 将所有tag提交到远端
- 1
- 2
-
-
branch related instructions
git branch -d <branch_name> // 删除某个分支 git branch -D <branch_name> // 强制删除某个分支 git branch -avv // 查看本地分支与远端分支关系,并且显示分支最新一次提交信息 git remote show origin // 查看远端分支间关系