从设计转型学开发的第一天起,老大只教我一件事,使用 Git。 我记得很清楚,当时我问他,假如全世界的软件你只能保留一个在电脑,你的选择是什么? Git 的强大一本书都不足以全部说明,更何况一篇博客。 Hope you enjoy! 1. 超实用 Alias
2. 取回远端 |
1 | gco master |
假设现在所在的分支是import
,指定推送到远端分支liujin-import
1 | g push origin import:liujin-import |
假如远端的 liujin-import
分支已经不需要,可以直接覆盖掉
1 | g push -f origin import:liujin-import |
有时候修完某功能并提交了 commit 之后才发现还有一点小修改,这时候又不想再提交一个commit,可以追加这个文件到前一个commit,步骤如下:
1 | git add 你要追加修改的文件 |
1 | git log 文件路径 |
或者
1 | git log --follow filename(绝对路径) |
Ref: List all commit for a specific file
老大常说要养成一个小改动对应一个commit的习惯,但是有时候写得太乱懒得去分割就把很多改动做成了一个commit,这样子增加了以后维护的难度,所以要把一个 commit 分拆为多个 commit 怎么办呢?
1 | git reset HEAD~1 path/to/file |
1 | git commit --amend -v |
1 | git commit -v path/to/file |
这样就把一个 commit 分拆为两个啦,^_^
1 | git rebase -i HEAD~10 |
假如 gst
发现已经有文件被修改,这时候需要把修改暂存起来。
1 | git stash |
接着找到你需要追加修改的那个commit id,如4b739bb
1 | g rebase 4b739bb~ -i 或者 |
这时候会自动打开编辑器,把你需要修改的 commit 前面的 pick
改成 edit
,保存,关闭编辑器,这时候会回到终端,再输入:
1 | g stash pop |
把暂存的修改读出来,然后做修改,g add .
,最后
1 | g rebase --continue |
git log --grep
git log --grep=frotz --grep=nitfol --since=1.month
查找一个月以内commit log message里含有 frotz
或者 nitfol
的 commits
git log --grep=frotz --author=Linus
查找指定作者
git grep -l -e frotz --and -e nitfol
查找同一行含有 frotz
和 nitfol
的文件
git grep -l --all-match -e frotz -e nitfol
查找文件里面含有 frotz
和 nitfol
的文件(不局限于同一行)
git clean -f
git clean -f -d
如果还想删除目录
git clean -f -X
如果只是想删除忽略的文件
git clean -f -x
如果想删除忽略和非忽略的文件
长时间做一个项目,经常需要 git fetch
,这样做每次都会拉回远端的全部分支。
即使远端有些分支已经删除,但是运行git branch -a
还是会显示已删除的分支,
长时间下来这个列表就会很长很长,这时候就需要清理一下本地的仓库了:
1 | git remote prune origin |
1 | gb dev origin/r1-dev |
1 | g branch -f master HEAD~3 |
git revert -m 1 M
-> W
1 | ---o---o---o---M---x---x---W |
1 | git rev-parse --short HEAD |
.gitignore
, 垃圾文件都已经提交比如说一个nodejs项目,一开始的时候就忘记了创建.gitnore
文件忽略掉node_modules
的内容,所以其中的内容就已经被提交了。
即使紧接着你在.gitignore
加了一行node_modules
, 已经被提交的文件是不会自动删除的。
这时候你就需要做的就是:
1 | git rm --cached path/to/ignored |
1 | $ git add -u |
This tells git to automatically stage tracked files — including deleting the previously tracked files.
If you are using git 2.0, you should now use:
1 | $ git add -u :/ |
Warning, starting git 2.0 (mid 2013), this will stage files on the whole working tree.
If you want to stage file only under your current path with that working tree, then you need to use
1 | $ git add -u . |
See “Difference of “git add -A” and “git add .””.
Ref: StackOverflow
git add .
操作这种情况通常是因为忘记添加.gitignore
文件,
或者一时手快把一些非必要的文件(如node_modules
)跟踪了, 解决办法:
You can use git reset
. This will ‘unstage’ all the files you’ve added after your last commit.
If you want to unstage only some files, use
1 | git reset -- <file 1> <file 2> <file n> |
Also it’s possible to unstage some of the changes in files by using
1 | git reset -p |
|