开发者

Git: How to create a new branch from an earlier/older commit in master branch?

开发者 https://www.devze.com 2023-03-17 03:36 出处:网络
I made three commits in the ma开发者_运维技巧ster branch: C1, C2 and C3. From that point I made a new branch \'Branch1\'. Did some

I made three commits in the ma开发者_运维技巧ster branch: C1, C2 and C3. From that point I made a new branch 'Branch1'. Did some changes and have them commited in C4 on Branch1.

Things would look as follows:

C1--C2--C3<--Master
         \     
          C4<--Branch1    

However, now I want to use an earlier commit in the Master to fork off a new branch 'Branch2'. i.e. in the Master branch, I want to go back in time at C2 and build on top of that in a new branch. The master branch is not to be disturbed in any way. C5 is a commit saving changes done in the new 'Branch2'. Graphically, the following figure should explain what i want:

      C5<--Branch2
     /
C1--C2--C3<--Master
         \     
          C4<--Branch1    
  1. What git command sequence(s) will take me there?

  2. My second question is that after achieving the case in second figure, if a 'revert' is performed to remove C3 in Master branch, What effect would it have on Branch1? Would code in Branch1 become corrupted or unstable?


simply check out that commit and specify a branch to create:

git checkout -b Branch2 C2

for your second question, that depends on how you define 'revert' (git revert or git reset). in both cases Branch1 will still contain the commit C3 (remember, in git branches are just 'pointers', ancestry information is stored in the commits themselves).

0

精彩评论

暂无评论...
验证码 换一张
取 消