git 常用命令记录。(持续更新)
1. 初始化
1 | git init |
2. 用户名邮箱配置
设置全局用户属性:
1 | git config --global user.name "champyin" |
–global: 设置当前用户的全局属性,当你的 repository 没有设置项目的 user.name 和 user.email 的时候,会默认用这个。
查看全局用户属性:
1 | git config user.name |
如何知道本地有没有设置属性:
1 | git config --local --list |
如何设置本地属性:
1 | git config user.name "champyin002" |
3. 远程仓库管理
克隆远程仓库到本地
1 | git clone <remote-repo-url> |
查看当前配置有哪些远程仓库
1 | git remote |
git remote
列出每个远程仓库的简短名字。在克隆完某个项目后,至少可以看到一个名为 origin 的远程仓库git remote -v
显示对应的远程仓库地址。-v
为-verbose
的简写。
添加远程仓库
1 | git remote add [shortname] [url] |
[shortname]是自己为远程仓库取的一个简单的名字,便于以后引用。
修改远程仓库协议
1 | git remote set-url origin git@github.com:lovecoding/lovecoding.github.io.git |
将远程仓库origin改为git协议的地址。
查看远程仓库信息
1 | git remote show [remote-name] |
重命名远程仓库(修改某个远程仓库在本地的简称)
1 | git remote rename origin sam #把远程仓库 origin 重命名为 sam |
对远程仓库的重命名,也会使对应的分支名称发生变化。原来的 origin/master 分支现在成为了 sam/master。
本地移除对应的远程仓库
1 | git remote rm sam |
4. 分支管理
查看本地分支
1 | git branch |
查看各个分支最后一个提交对象的信息
1 | git branch -v |
查看远程分支
1 | git branch -r |
查看所有分支,包括本地和远程
1 | git branch -a |
创建分支
1 | git branch mytest1 |
切换分支
1 | git checkout mytest1 |
创建并切换到该分支
1 | git checkout -b mytest2 |
重命名分支
1 | git branch -m existBranch newName |
重命名当前分支
1 | git branch -m newName |
删除空分支
1 | git branch -d mytest1 |
删除有内容的分支,上方的命令会被拒绝,需要使用-D
:
1 | git branch -D mytest1 |
删除远程分支
1 | git push origin --delete mytest1 //git v1.70 以上 |
同步远程删除的分支到本地
1 | git remote prune [remote-name] |
查看分支于远程的映射关系
1 | git branch -vv |
5. 获取远程分支代码
1 | git pull [remote-name] [remote-branch-name]:[local-branch-name] |
注意,分支推送、拉取命令的写法规则是<来源地>:<目的地>。所以 git pull 是[remote-branch-name]:[local-branch-name],git push 是[local-branch-name]:[remote-branch-name]。
6. 上传代码到远程仓库
push 某个分支
1 | git push [remote-name] [branch-name] |
如果远程有一个 bugfix 分支,我想要有一份自己的 bugfix 来开发。
先将远程分支抓取下来:
1 | git fetch origin |
在远程分支的基础上分化一个新的本地分支:
1 | git checkout -b [local-branch-name] [remote-name]/[remote-branch-name] |
采用此命令建立的本地分支会自动和远程分支建立映射关系。
修改完代码后,上传到远程 bugfix 分支:
1 | git push [remote-name] [branch-name] |
git push origin bugfix
意思为取出在本地的 bugfix 分支,推送到远程仓库的 bugfix 分支中去。git push orign bugfix:bugfix
意思为上传完本地的bugfix分支到远程仓库中去,仍旧称它为 bugfix 分支。实现跟上一条命令相同的效果。通过此语法,可以把本地分支推送到某个命名不同的远程分支:例如使用git push origin bugfix:hotfix
来推送,如果远程分支 hotfix 不存在,则会在远程仓库被新建。当我的协作者再次从服务器上抓取数据时,他们将得到一个新的远程分支 origin/hotfix.
push 所有分支(不管是否存在对应的远程分支,将本地的所有分支都推送到远程仓库)
1 | git push --all [remote-name] |
7. 打tag
注意:tag是打在commit上,不是分支上。
轻量级标签
1 | git tag v1.0.0 |
给历史提交打标签
1 | git tag v1.0.0 88aa731 #历史commit的id的前7位 |
查看标签
1 | git tag |
搜索符合模式的tag
1 | git tag -l 'v0.1.*' |
将本地标签同步到远程仓库
1 | git push origin --tags #提交所有的tag |
切换到tag
与切换到分支命令相同
1 | git checkout [tagname] |
查看tag信息
用
git show
命令
1 | git show v0.1.1 |
删除本地标签
1 | git tag -d v1.0.0 |
将删除标签同步到远程
1 | git push origin :refs/tags/v1.0.0 #推送空的同名版本到远程 |
拉取某个版本
1 | git fetch origin tag v1.0.0 |
8. 查看提交历史日志
1 | git log --pretty=oneline --abbrev-commit #将commit id显示为缩写(前7位) |
退出查看日志
1 | q |
9. 删除本地untrack file
1 | git clean -fd // 删除本地的untrack文件和文件夹 |
10. 修改已经提交的信息
当你不小心写错了提交信息,理论上,SCM上是不应该修改历史信息的,但是在git中可以修改最后一次提交的信息。
1 | git commit --amend |
amend
参数提供了对最后一次commit的修改,对于历史提交,如果想修改,就必须使用 rebase
了。
1 | git rebase -i HEAD~3 #表示要修改当前版本的倒数第三次状态。 |
11. 取消对某个工作区文件的修改
1 | git checkout -- <file>... |
该指令的作用是把文件在工作区的修改全部撤销:如果这个文件修改后没有放到暂存区,那撤销修改就回到版本库中的状态(即回到最近一次 git commit
时的状态);如果这个文件在添加到暂存区后又做了修改,那撤销修改就回到添加暂存区后的状态(即回到最近一次 git add
时的状态)。
放弃所有修改
1 | git checkout . |
12. review changes 查看文件的修改之处
1 | git diff <file> |
注:只能在
git add
之前才能查看到修改的内容
13. 其他命令
查看所有文件
1 | git ls-files # 列出工作区的所有文件 |
查看某段代码是谁写的
1 | git blame <file-name> # 会列出每行代码的提交id、作者以及提交时间。 |