從設計轉型學開發(fā)的第一天起,老大只教我一件事,使用 Git。 我記得很清楚,當時我問他,假如全世界的軟件你只能保留一個在電腦,你的選擇是什么? Git 的強大一本書都不足以全部說明,更何況一篇博客。 Hope you enjoy! 1. 超實用 Alias
2. 取回遠端 |
1 | gco master |
假設現(xiàn)在所在的分支是import
,指定推送到遠端分支liujin-import
1 | g push origin import:liujin-import |
假如遠端的 liujin-import
分支已經不需要,可以直接覆蓋掉
1 | g push -f origin import:liujin-import |
有時候修完某功能并提交了 commit 之后才發(fā)現(xiàn)還有一點小修改,這時候又不想再提交一個commit,可以追加這個文件到前一個commit,步驟如下:
1 | git add 你要追加修改的文件 |
1 | git log 文件路徑 |
或者
1 | git log --follow filename(絕對路徑) |
Ref: List all commit for a specific file
老大常說要養(yǎng)成一個小改動對應一個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
發(fā)現(xiàn)已經有文件被修改,這時候需要把修改暫存起來。
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項目,一開始的時候就忘記了創(chuàng)建.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 |
|