记录git常用的命令

代码提交

使用一次新的commit,替代上一次提交,如果代码没有任何新变化,则用来改写上一次commit的提交信息

1
$ git commit --amend -m [message]

这个方法可以用来修改上次commit的备注信息

1
$ git commit --amend

这个命令会进入一个编辑模式,可以编辑commit信息

修改历史提交注释(修改历史多次提交注释)
git rebase -i HEAD~X比如我想修改最近两条commit的注释,即输入git rebase -i HEAD~2
git_rebase_it_HEAD_x

1.进入编辑模式,将需要修改commit的记录的pick修改成edit,
编辑好之后使用:wq保存退出

2.然后使用一下命令

1
$ git commit --amend

进入编辑模式编辑commit

1
git rebase --continue

分支

列出所有远程分支

1
$ git branch -r

列出所有本地分支和远程分支

1
$ git branch -a

新建一个分支,但依然停留在当前分支

1
$ git branch [branch-name]

新建一个分支,指向指定commit

1
$ git branch [branch] [commit]

新建一个分支,与指定的远程分支建立追踪关系

1
$ git branch --track [branch] [remote-branch]

选择一个commit,合并进当前分支

1
$ git cherry-pick [commit]

删除分支(删除本地分支)

1
$ git branch -d [branch-name]

删除远程分支

1
2
3
$ git push origin --delete [branch-name]

$ git branch -dr [remote/branch]

标签

新建一个tag在当前commit

1
$ git tag [tag]

新建一个tag在指定commit

1
$ git tag [tag] [commit]

删除本地tag

1
$ git tag -d [tag]

删除远程tag

git push origin :refs/tags/[tagName]

1
$ git push origin :refs/tags/v1.0.0.0

查看tag信息

1
$ git show [tag]

提交指定tag

1
$ git push [remote] [tag]

提交所有tag

1
$ git push [remote] --tags

新建一个分支,指向某个tag

1
$ git checkout -b [branch] [tag]

查看信息

显示commit历史,以及每次commit发生变更的文件

1
$ git log --stat

搜索提交历史,根据关键词

1
$ git log -S [keyword]

显示所有提交过的用户,按提交次数排序

1
$ git shortlog -sn

显示暂存区和工作区的差异

1
$ git diff

显示结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git diff
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory
diff --git a/package.json b/package.json
index 657c96c..59c4251 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"hexo": {
- "version": "3.7.1"
+ "version": "3.8.0"
},
"dependencies": {
"hexo": "^3.7.0",

显示暂存区和上一个commit的差异

1
$ git diff --cached [file]

显示工作区与当前分支最新commit之间的差异

1
$ git diff HEAD

显示结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git diff HEAD
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory
diff --git a/package.json b/package.json
index 657c96c..59c4251 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"hexo": {
- "version": "3.7.1"
+ "version": "3.8.0"
},
"dependencies": {
"hexo": "^3.7.0",

显示今天你写了多少行代码

1
$ git diff --shortstat "@{0 day ago}"

远程同步

下载远程仓库的所有变动

1
$ git fetch [remote]

推送所有分支到远程仓库

1
$ git push [remote] --all

撤销

恢复暂存区的指定文件到工作区

1
$ git checkout [file]

恢复某个commit的指定文件到暂存区和工作区

1
$ git checkout [commit] [file]

恢复暂存区的所有文件到工作区

1
git checkout .

重置暂存区的指定文件,与上一次commit保持一致,但工作区不变

1
$ git reset [file]

重置暂存区与工作区,与上一次commit保持一致

1
$ git reset --hard

重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变

如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法。

1
$ git reset [commit]

重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致

如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法。

1
$ git reset --hard [commit]

重置当前HEAD为指定commit,但保持暂存区和工作区不变

1
$ git reset --keep [commit]

新建一个commit,用来撤销指定commit

后者的所有变化都将被前者抵消,并且应用到当前分支

如果我们想撤销之前的某一版本,但是又想保留该目标版本后面的版本,记录下这整个版本变动流程,就可以用这种方法。

1
$ git revert [commit]

暂时将未提交的变化移除,稍后再移入

1
$ git stash

发布

生成一个可供发布的压缩包

1
$ git archive