Question from email:
how do I merge 1 file from master -> LIVE
You cannot. Merge operation creates new commit that has 2 or more parents and will always join full histories of the branches.
You can however:
- replicate all changes done in single commit to different branch - this is “cherry pick”
- take state of single file from different branch into current branch
How to replicate all changes done in single commit to different branch - “cherry pick”
Say you have latest commit in master that contains change to two files you need.
1.) You go to target branch - LIVE, make sure you have clean work tree
git checkout LIVE git status => should show no changes
2.) “cherry pick” the commit from master
git cherry-pick master
This will create new commit on LIVE that has same author, message etc as latest commit on master.
Only the files modified in the commit will be touched - and the state of these files will be identical to master
You can use any TREE-ISH expression in cherry pick - tag name, branch name, remote branch name or SHA1
Cherry pick will cause that “logically” same commit will exist twice in the project history (with different SHA1)
Overuse may cause harder to understand project histories
How to take state of single file from different branch into current branch
Use case: I need exact version of the file ‘src/com/something/Important.java’ from master to LIVE
Manual (stupid) version - DO NOT DO THAT
The only reason I mention this because I have seen people doing it,
- 1). Go to master -
git checkout master
- 2). save file to temp -
cp 'src/com/something/Important.java' /tmp
- 3). Go to live -
git checkout LIVE
- 4) copy file back -
cp /tmp/Important.java src/com/something/
- 5) Add and commit file -
git add 'src/com/something/Important.java'; git commit -m "Copied file from master'
If you specify file path in ‘git checkout’, it will get only that file(s) and will also add these files to index - ready for commit
1). Go to destination branch - LIVE
git checkout LIVE
2) get the file from master
git checkout master – src/com/something/Important.java
Now you can validate with git status that the file is modified and ready to committed
git commit -m “Copied file from master’
Difference between cherry-pick and the single file transfer
The end effect of cherry pick for commit with single file and the second way is the same: the file in LIVE has same content as in master.
Difference is that the second way does not indicate in any way that the file status is actually the same (commit has different author and no relation to original commit), while using cherry-pick this information is copied.
Nothing prevents you using the second approach for multiple file (as part of single commit) by repeating checkout – multiple times.
Both will make the history of the project a bit messier. Please always consider whether merge is not an option.
Author Miro Adamy
License (c) 2006-2019 Miro Adamy