Git. Checkout function element between merge transactions

This is rather strange, but I cannot perform a fairly general operation with git. Basically, I want to check the function branch, not using it, but using the SHA identifier. This SHA points between merges with the main branch.

The problem is that all I get is just the host branch without commits from the function branch. I'm currently trying to fix the regression introduced earlier in the master branch.

To be more visual, I created a small bash script to recreate the problem repository:

#!/bin/bash rm -rf ./.git git init echo "test1" > test1.txt git add test1.txt git commit -m "test1" -a git checkout -b patches master echo "test2" > test2.txt git add test2.txt git commit -m "test2" -a git checkout master echo "test3" > test3.txt git add test3.txt git commit -m "test3" -a echo "test4" > test4.txt git add test4.txt git commit -m "test4" -a echo "test5" > test5.txt git add test5.txt git commit -m "test5" -a git checkout patches git merge master #Now how to get a branch having all commits from patches + test3.txt + test4.txt - test5.txt ??? 

Basically, all I want to do is just β€œpatches” the validation branches with files 1-4, but not including test5.txt.

Doing: git checkout [sha_where_test4.txt_entered]

... just gives a branch with test1, test3, test4, but excluding test2.txt

More complex example:

 #!/bin/bash rm -rf ./.git git init echo "test1" > test1.txt git add test1.txt git commit -m "test1" -a git checkout -b patches master echo "test2" > test2.txt git add test2.txt git commit -m "test2" -a git checkout master echo "test3" > test3.txt git add test3.txt git commit -m "test3" -a echo "test4" > test4.txt git add test4.txt git commit -m "test4" -a echo "test5" > test5.txt git add test5.txt git commit -m "test5" -a git checkout patches git merge master echo "test6" > test6.txt git add test6.txt git commit -m "test6" -a #Now how to get a branch having all commits from patches + test3.txt + test4.txt - test5.txt ??? git log --topo-order | cat # Now I need something to help me going back to history # without manually calculating that patches~2 sha's git checkout -b patches.tmp master~1 git merge patches~2 

Thanks.

+4
source share
3 answers

As for your first example, you need to replay test3 and 4 on top of test2: this is the classic rebase --onto case :

Start with:

alt text http://img169.imageshack.us/img169/2255/gitr1.png

Mark the current patch installation and move the patch branch where you want to end (test4):

 C:\Prog\Git\tests\rep\main>git checkout patches Switched to branch 'patches' C:\Prog\Git\tests\rep\main>git checkout -b tmp Switched to a new branch 'tmp' C:\Prog\Git\tests\rep\main>git checkout patches Switched to branch 'patches' C:\Prog\Git\tests\rep\main>git reset --hard master~1 HEAD is now at 8448d0f test4 

This gives you:

alt text http://img169.imageshack.us/img169/4826/gitr2.png

And just reinstall the correct sequence of commits on what you want:

 C:\Prog\Git\tests\rep\main>git checkout tmp Switched to branch 'tmp' C:\Prog\Git\tests\rep\main>git rebase --onto tmp tmp~1 patches First, rewinding head to replay your work on top of it... Applying: test3 Applying: test4 

What gives:

alt text http://img52.imageshack.us/img52/372/gitr3.png

This only works to move a linear set of commits.

+1
source

There is no such point. Here you have two parallel development paths: one contains the files test1 and test2, the other contains the files test1, test3, test4 and test5. However, you can create such a merge point.

Check SHA1 for the commit adding test4, and combine with the commit that adds test2. For example, after running the script, my repository looks like this:

 * b054987 (HEAD, patches) Merge branch 'master' into patches |\ * | 5ae790f test2 | * f2a3dac (master) test5 | * 70e8cd2 test4 | * c4102ed test3 |/ * d448eaa test1 

In this case, run:

 % git checkout 70e8c % git merge 5ae79 

The result is a HEAD that contains files 1-4:

 * bcc8f7a (HEAD) Merge commit '5ae79' into HEAD |\ | | * b054987 (patches) Merge branch 'master' into patches | | |\ | |/ / | * | 5ae790f test2 | | * f2a3dac (master) test5 | |/ |/| * | 70e8cd2 test4 * | c4102ed test3 |/ * d448eaa test1 % ls test1.txt test2.txt test3.txt test4.txt 

Now you can create a branch from this point.

+1
source
Operation

A git merge does not return or change history, but creates a new commit, pointing to the two stories from which it arose. So in the pictures you have something like this:

  2 ----------- \
  / \
 1 --- 3 --- 4 --- 5 --- M (HEAD)

When you rewind history to return to revision 4, you get files 1, 3 and 4, but not 2.

0
source

Source: https://habr.com/ru/post/1304082/


All Articles