I am trying to move the content of entire folder on one branch into different branch. All the files that were modified or added are correctly reflected in the new branch however the files that were deleted are still displayed as if there was no update related to them.
When i modify one of the files, add a new file and delete one of the existing files in TSTGIT folder on DEV branch and then commit the changes, checkout the UAT branch and checkout TSTGIT folder from DEV branch, I would expect to see all 3 changes, but the file that was deleted is completely ignored.
git checkout UAT
git checkout DEV -- TSTGIT
git status
Changes to be committed:
new file: TSTGIT/addedFile.txt
modified: TSTGIT/modifiedFile.txt
The only way i found is to use 'patch' argument where it let me choose also to delete the file but i would like to do this automatically without the need to provide another information
git checkout -p DEV -- TSTGIT
Pls do you know if there is a way how to force all the changes to be included?
Thanks a lot for your help ;)
By default git checkout
only adds or modifies files in the working directory―it never deletes them. This is called overlay mode:
overlay
Only update and add files to the working directory, but don’t delete them, similar to howcp -R
would update the contents in the destination directory. This is the default mode in a checkout when checking out files from the index or a tree-ish.
Where <tree-ish>
is a reference that leads to a tree
object, usually a commit.
As of Git 2.22, you can pass the --no-overlay
option to git checkout
to delete any files from the working directory that are missing in the referenced tree:
When specifying
--no-overlay
, files that appear in the index and working tree, but not in <tree-ish> are removed, to make them match <tree-ish> exactly.
Your command should then be:
git checkout --no-overlay DEV -- TSTGIT
On an interesting side note, the reason why you were able to delete the file when using the --patch
option, is because it implies --no-overlay
.
From the documentation:
Note that this option uses the no overlay mode by default, and currently doesn’t support overlay mode.
The problem with git checkout DEV -- TSTGIT
is that the command copies files, not applies changes; and copied files certainly doesn't include deleted files.
If you want to synchronize the directory — i.e. make it an exact copy of the directory from the other branch, you can remove the directory before checking it out from the other branch:
git checkout UAT
rm -rf TSTGIT
git checkout DEV -- TSTGIT
The problem with the approach is that it removes files that existed in branch DEV
but never existed (and were not deleted) in UAT
.
If you want to apply changes without deleting the directory an approach similar to git checkout -p
is the only way. Could be git cherry-pick --no-commit
; but after that you have to restore all directories except TSTGIT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With