Git 使用小貼士

git --verion git version 2.11.1

github地址
簡書地址

慎用git push -f

作用:強(qiáng)行把自己的git repo推送到遠(yuǎn)端,忽略可能的merge和conflict.

可能的恢復(fù)方法:

  1. 找到代碼丟失者的git repo, 運(yùn)行git reflog查看操作歷史。
2e21737 HEAD@{0}: commit: commit1
787e1c4 HEAD@{1}: commit: commit1
ef4bece HEAD@{2}: commit: commit2
  1. git show 2e21737查看詳細(xì)的commit信息
commit 2e21737cff4d630558b30707f1a9087290d14fae
Author: ...
Date:   Mon Mar 27 16:00:13 2017 +0800

    commit1

diff --git a/Git/README.md b/Git/README.md
  1. git cherry-pick 2e21737獲得丟失的提交

使用git rebase

經(jīng)??梢园l(fā)現(xiàn)很多人提交代碼的時候有Merge branch 'master'的操作。
導(dǎo)致代碼在開發(fā)中有非常亂的軌跡,使得代碼的維護(hù)非常復(fù)雜。
其實在真正的開發(fā)過程中,合并代碼是管理員操作的很小一部分操作,而不應(yīng)該出現(xiàn)在日常的提交中。

屏幕快照 2017-03-30 上午11.09.11.png

處理方法:

  1. 在代碼提交前,使用git pull --rebase代替git pull, 把本地代碼和遠(yuǎn)端pull的代碼衍合,

Tips: git pull = git fetch + git merge

  1. 當(dāng)代碼提交后,不要執(zhí)行遠(yuǎn)端的rebase操作,修改遠(yuǎn)端git repo上的提交樹。
    例如:

這是一個簡單的merge操作。

屏幕快照 2017-03-31 上午10.56.14.png
     Test -> Test1
       /            \ 
Master -> Master1 -> Master2(Test)

現(xiàn)在我在Master上執(zhí)行git rebase Test,可以發(fā)現(xiàn)衍合之后如下

屏幕快照 2017-03-31 上午11.06.08.png
Master -> Test1 -> Master1

Warning: 如果這是一個本地的分支,則做衍合是可以的;如果是一個遠(yuǎn)程分支,這個操作會覆蓋合并歷史,是不允許的

使用git ignore

.gitignore文件可以方便定義不需要提交的文件和文件夾,即使你使用git add *也不會放到提交區(qū)中。

/node_modules #依賴
/dist #build結(jié)果
/.idea #IDE設(shè)置目錄
/.tmp
/.sass-cache
/bower_components

使用git reset

git reset不會產(chǎn)生新的提交,這是和git revert不同的,git revert經(jīng)常用來在遠(yuǎn)端進(jìn)行代碼回滾
git reset往往是開發(fā)者本地進(jìn)行代碼比較和修改用的。

git revert HEAD產(chǎn)生一個提交,可以使用git log查看

commit faf26c107ab2edb2b43a8e0319e8ef3134a83f5f
Author: *
Date:   Fri Mar 31 11:13:28 2017 +0800

    Revert "commit1"

git reset HEAD^1 --hard會提示現(xiàn)在HEAD所在的地址,git log看不見最新的一次提交

HEAD is now at 5634c99 commit1

三種模式:
--hard 所有add到提交區(qū)中的提交,將被回滾
--soft 所有本地的修改保留,回退的代碼修改會放到提交區(qū)中
--mixed(默認(rèn)) 所有本地的修改和回退的代碼都放到未跟蹤的文件區(qū),需要重新add

例如:現(xiàn)在的狀態(tài)是2.txt在提交區(qū)中,3.txt在未跟蹤區(qū)中,都是用git reset回退一步的區(qū)別。

git st
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   2.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    3.txt

運(yùn)行git reset HEAD^1 --mixed,發(fā)現(xiàn)本地的2.txt和3.txt都放到了為跟蹤區(qū)中,包括提交的回滾1.txt,需要重新add。

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  1 add some
  
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   1.txt
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    2.txt
    3.txt

如果運(yùn)行git reset HEAD^1 --soft,發(fā)現(xiàn)本地的2.txt和3.txt都沒有變化,回滾的1.txt放到了提交區(qū)中。

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   1.txt
    new file:   2.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    3.txt

使用git stash

git stash用來緩存本地代碼,使用git stash pop來恢復(fù)stash中的代碼

小結(jié)

  • 不要使用git push -f,使用git push,發(fā)現(xiàn)conflict需要解決。
  • git pull --rebase十分有用,可以用來從遠(yuǎn)程拉取代碼,并且和本地的提交衍合,保證提交樹的清晰。
  • git reset提供三種不同的模式。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容