git config
配置 Git 的相关参数。
Git 一共有3个配置文件:
- 仓库级的配置文件:在仓库的 .git/.gitconfig,该配置文件只对所在的仓库有效。
- 全局配置文件:Mac 系统在 ~/.gitconfig,Windows 系统在C:\Users\<用户名>\.gitconfig。
- 系统级的配置文件:在 Git 的安装目录下(Mac 系统下安装目录在 /usr/local/git)的etc文件夹中的gitconfig。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 
 | 
 $ git config <--local | --global | --system> -l
 
 
 $ git config -l
 
 
 
 $ git config <--local | --global | --system> -e
 
 
 
 $ git config <--local | --global | --system> --add <name> <value>
 
 
 $ git config <--local | --global | --system> --get <name>
 
 
 $ git config <--local | --global | --system> --unset <name>
 
 
 $ git config --global user.name <用户名>
 $ git config --global user.email <邮箱地址>
 
 
 
 
 $ git config --global http.postBuffer <缓存大小>
 
 
 $ git config --global color.ui true
 
 
 $ git config --global credential.helper cache
 
 
 
 $ git config --global credential.helper 'cache --timeout=<缓存时间>'
 
 
 $ git config --global credential.helper store
 
 | 
git clone
从远程仓库克隆一个版本库到本地。
| 12
 3
 4
 5
 6
 7
 8
 
 | $ git clone <远程仓库的网址>
 
 
 $ git clone <远程仓库的网址> <本地目录>
 
 
 $ git clone <远程仓库的网址> -b <分支名称> <本地目录>
 
 | 
git init
初始化项目所在目录,初始化后会在当前目录下出现一个名为 .git 的目录。
git status
查看本地仓库的状态。
| 12
 3
 4
 5
 6
 7
 
 | $ git status
 
 
 
 
 $ git status -s
 
 | 
git remote
操作远程库。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | $ git remote
 
 
 $ git remote -v
 $ git remote --verbose
 
 
 $ git remote add <远程仓库的别名> <远程仓库的URL地址>
 
 
 $ git remote rename <原远程仓库的别名> <新的别名>
 
 
 $ git remote remove <远程仓库的别名>
 
 
 $ git remote set-url <远程仓库的别名> <新的远程仓库URL地址>
 
 | 
git branch
操作 Git 的分支命令。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | $ git branch
 
 
 $ git branch -v
 
 
 $ git branch <分支名>
 
 
 
 $ git branch -m [<原分支名称>] <新的分支名称>
 
 $ git branch -M [<原分支名称>] <新的分支名称>
 
 
 $ git branch -d <分支名称>
 
 
 $ git branch -D <分支名称>
 
 | 
git checkout
检出命令,用于创建、切换分支等。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | $ git checkout <分支名称>
 
 
 
 $ git checkout -b <分支名称>
 
 
 $ git checkout --orphan <分支名称>
 
 
 $ git checkout <文件路径>
 
 | 
git cherry-pick
把已经提交的记录合并到当前分支。
| 12
 
 | $ git cherry-pick <commit ID>
 
 | 
git add
把要提交的文件的信息添加到暂存区中。当使用 git commit 时,将依据暂存区中的内容来进行文件的提交。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | $ git add <文件路径>
 
 
 $ git add -u [<文件路径>]
 $ git add --update [<文件路径>]
 
 
 $ git add -A [<文件路径>]
 $ git add --all [<文件路径>]
 
 
 $ git add -i [<文件路径>]
 $ git add --interactive [<文件路径>]
 
 | 
git commit
将暂存区中的文件提交到本地仓库中。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | $ git commit
 
 
 $ git commit -m "<提交的描述信息>"
 
 
 
 $ git commit -a -m "<提交的描述信息>"
 
 
 $ git commit --amend
 
 | 
git fetch
从远程仓库获取最新的版本到本地的 tmp 分支上。
| 12
 3
 4
 5
 
 | $ git fetch <远程仓库的别名>
 
 
 $ git fetch <远程主机名> <分支名>
 
 | 
git merge
合并分支。
git diff
比较版本之间的差异。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | $ git diff
 
 
 $ git diff --cached
 $ git diff --staged
 
 
 $ git diff HEAD
 
 
 $ git diff <commit ID>
 
 
 $ git diff <分支名称> <分支名称>
 
 
 $ git diff <分支名称>...<分支名称>
 
 | 
git pull
从远程仓库获取最新版本并合并到本地。 首先会执行 git fetch,然后执行 git merge,把获取的分支的 HEAD 合并到当前分支。
git push
把本地仓库的提交推送到远程仓库。
| 12
 3
 4
 5
 6
 
 | $ git push <远程仓库的别名> <本地分支名>:<远程分支名>
 
 
 $ git push <远程仓库的别名> :<远程分支名>
 $ git push <远程仓库的别名> --delete <远程分支名>
 
 | 
git log
显示提交的记录。
| 12
 3
 4
 5
 6
 7
 8
 
 | $ git log
 
 
 $ git log <commit ID>
 
 
 $ git log -<指定的数量>
 
 | 
git reset
还原提交记录。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | 
 
 $ git reset [<文件路径>]
 $ git reset --mixed [<文件路径>]
 
 
 $ git reset <commit ID>
 $ git reset --mixed <commit ID>
 
 
 
 $ git reset --soft <commit ID>
 
 
 $ git reset --hard <commit ID>
 
 | 
git revert
生成一个新的提交来撤销某次提交,此次提交之前的所有提交都会被保留。
| 12
 
 | $ git revert <commit ID>
 
 | 
git tag
操作标签的命令。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | $ git tag
 
 
 $ git tag <标签名称> [<commit ID>]
 
 
 $ git tag -a <标签名称> -m <标签描述信息> [<commit ID>]
 
 
 $ git checkout <标签名称>
 
 
 $ git show <标签名称>
 
 
 $ git tag -d <标签名称>
 
 
 $ git push <远程仓库的别名> <标签名称>
 
 
 $ git push <远程仓库的别名> –tags
 
 | 
git mv
重命名文件或者文件夹。
| 12
 
 | $ git mv <源文件/文件夹> <目标文件/文件夹>
 
 | 
git rm
删除文件或者文件夹。
| 12
 3
 4
 5
 6
 7
 8
 
 | $ git rm <文件路径>
 
 
 $ git rm -r <文件夹路径>
 
 
 $ git rm --cached
 
 | 
Git操作场景示例
1. 删除掉本地不存在的远程分支
多人合作开发时,如果远程的分支被其他开发删除掉,在本地执行 git branch --all 依然会显示该远程分支,可使用下列的命令进行删除:
| 12
 3
 4
 5
 6
 
 | $ git pull -p
 
 
 $ git fetch -p
 $ git fetch --prune origin
 
 |