Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git cherry-pick merge deleting file

I'm trying to cherry-pick a commit in git, which adds a bunch of files, but also modifies a file that doesn't yet exist in my branch.

There is a conflict where the files to add are all staged, and the file that was changed in the commit but doesn't yet exist in the branch is not staged and and is listed as:

deleted by us: app/changed_file.rb

If I just commit the staged files, will that cause issues when the changed_file.rb is eventually merged into the branch?

like image 634
Solomon Avatar asked Oct 04 '22 14:10

Solomon


2 Answers

https://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html

Cherry-pick creates new commit, so if you omit file that does not exist in your branch but exists in a branch you are going to merge in eventually nothing will happen.

mkdir test && cd test && git init && touch foo.txt
git add foo.txt && git commit -m "Init repo"
git branch test && git checkout test && vi foo.txt 
git add foo.txt && git commit -m "Changed foo on branch test"
git checkout master && touch bar.txt
git add bar.txt && git commit -m "Create bar.txt"
vi bar.txt 
touch baz.txt && git add . && git commit -m "Will be cherry picked"
git checkout test && git cherry-pick bfd1b58
git rm bar.txt && git commit
git checkout master && git merge test

And as a result of ls: bar.txt baz.txt foo.txt

like image 98
Ruslan Osipov Avatar answered Oct 10 '22 12:10

Ruslan Osipov


You can simply remove that file from the commit you are cherry picking if it is not required...

git rm file_name

git commit

-here make sure that your commit message contains Change ID : line as a last paragraph

git push origin HEAD:refs/for/branch_name

This way you will not get any conflicts and it wil make your merge easier next time..

like image 37
mrutyunjay Avatar answered Oct 10 '22 13:10

mrutyunjay