L.Lauenburg
L.Lauenburg

Reputation: 472

Git: Overwrite file changes with "git checkout" before merge

The Issue:

When merging origin/branch_y into origin/master, after overwriting file_x on branch_y using git checkout origin/master file_x and pushing file_x to the origin/branch_y, file_x still differs from the version on origin/master and is listed under changes.

The Question:

Should not file_x at this point, at least content-wise, be precisely the same on both branches?

Full Story:

Comparing the branches sac_sb3 and master, while issuing a new merge request, I noticed I unwantedly pushed changes of the files env.py and utils.py to the branchsac_sb3 - both files should have stayed untouched.

The branch represents the work of multiple weeks, it comprises numerous commits, and the cleanliness of the graph is not of utmost importance in the given work setting. I deduced that the quickest solution would be to overwrite the files with the file states of the remote master. Therefore, following this StackOverflow post, I ran:


git checkout sac_sb3

git checkout origin/master rlmodel/sb3/utils.py

git checkout origin/master rlmodel/sb3/env.py

git add -u

git commit -m "restore env.py and utils.py with versions from origin/master"

git push

However, when I now reinitialize the merge request between origin/sac_sb3 and origin/master, env.py and utils.py get still listed under changes and even differ content-wise from the versions on origin/master.

Upvotes: 0

Views: 495

Answers (1)

suvayu
suvayu

Reputation: 4664

When specifying paths, it's always better to separate them with -- to prevent ambiguity. Try:

git checkout origin/master -- rlmodel/sb3/{utils,env}.py

You can find all different ways of specifying options, commits, hashes, and pathspecs at the top of the man page.

Upvotes: 1

Related Questions