Git的日常處理流程


前提

本地有2個分支,一個是master,還有一個是local

master 默認追蹤origin/master

local 通過git branch -u origin/master來映射

開發的時候,在local上,進行開發。master僅用於和remote進行同步

備注:origin只是一個remote的名字而已,一個repository可以和多個remote進行關聯

 

情況1.早上到公司后,進行代碼同步,本地的commit暫時不需要push到服務器

因為處於開發環境,所以,當前處於local分支

1.首先確認git status,確保本地的clean的狀態

2.git fetch origin master:master

3.git status 

   3.1如果提示告訴你,可以fast-forward

          3.1.1      git merge master

這種情況,只在你本地沒有commit的時候才發生

  3.2 local 和master發生了diverged

$ git status
On branch local
Your branch and 'origin/master' have diverged,
and have 1 and 10 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean

        3.2.1    git rebase master  

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: local configuration

         如果遇到沖突的話,用TortoiseGit處理好,Mark文件為resolved。然后回到命令行,git rebase --continue

4.可以繼續開發了

 

其中第2步,參考自

https://stackoverflow.com/questions/18857570/git-pull-without-checkout 

git fetch <remote> <srcBranch>:<destBranch>

 

情況2.local 分支有一些*.config 文件進行了提交,之后,還有一些有效的需要push到服務器的commit

 當前處於local分支

1.git status確保clean狀態

$ git status
On branch local
nothing to commit, working tree clean

2.git checkout master

3.git pull

4.使用TortoiseGit進行cherry-pick,將local分支上有效的commit拿到master分支

5.git push 

   5.1push失敗

    在你pull之后,push之前,可能又有人往服務器push了新的commit

    處理方法,git fetch同步到最新的代碼后,rebase origin/master。

    然后再次嘗試push

6.使用TortoiseGit進行cherry-pick,將local分支上的config相關的commit拿到master分支上

7.使用TortoiseGit ,reset master 分支到origin/master

8.git checkout local

9.使用TortoiseGit,將local指向新生成的config的commit

10.可以繼續開發了

11. remark

       上面使用的reset都是reset --hard,所以第一步的時候,需要確保git status是clean的狀態

 

情況3.新的處理方式,和服務器進行代碼交互

前提,在local分支進行代碼開發,master分支用來和服務器同步。當前處於local分支

3.1 git checkout master

3.2 git pull

3.3 使用TortoiseGit進行cherry-pick

3.4 git push

3.5 git rebase master local

     此步驟,最新的git for windows,已經可以自動跳過內容相同的commit。

     此命令的意思,以master作為base,然后把local上的代碼拼接過去

     此命令,執行完成后,會自動切換到local

 

 

參考

 cherry-pick in TortoiseGit 

 

注意

因為涉及到local和master兩個分支,所以在TortoiseGit查看日志的界面,務必勾選上左下角的All Branches。

最好能仔細看完progit第三章,深入理解branch的概念,branch僅僅是一個指向commit的指針。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM