1

I have some confussion with git, so I created a toy sample which would let me understand how to solve my problem. Let's say I have a repo in which one of the files requirements.txt look like this:

# requirements.txt 
A
B
C
D

Let say I added in a new branch two lines of code:

git checkout -b newbranch
git add .
git commit -m "B2 B3"
git checkout master
git merge newbranch

# requirements.txt 
A
B
B2
B3
C
D

But then the collaborative history continues in master, and someone else creates new content, adding, updating or deleting other files and so on. After pulling master from the remote repo, the requirements file on my master looks like this (my previous changes were deleted by new commits, and also C ):

# requirements.txt 
A
B 
C1
C2
D
E

Now I want to restore the lines B1 and B2 which were added in newbranch, but without deleting what others did on master. Namely, my desired output would be:

Whatever GitMagicCommands to do this??:

# requirements.txt:
A
B
B2
B3
C1
C2
D
E

Which Git commands could I use to accomplish that in the best way (I assume there could be conflicts)?

1 Answers1

1

A pickaxe search can help: git log -SB2 will give you:

  • the commit C1 where you introduced B2
  • and, for your information, the commit C2 which removed B2.

You can then git cherry-pick C1, assuming it only modified that one file:

git cherry-pick C1

If not, you can cherry-pick only one file out of C1, using git show/git apply.

git show "C1" -- requirements.txt | git apply --index -
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250