Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git undo auto merging on a specific file only, not the whole branch

Tags:

Is there a way I can undo a auto-merge of one file that has been changed instead of the whole branch in Git?

I want to undo this merge:

Auto-merging public/stylesheets/application.css

to the previous version and leave the rest at its current state.

I tried:

git reset HEAD~1 public/stylesheet/application.css

but seems like it's not working.

like image 441
Dee-M Avatar asked Mar 18 '11 09:03

Dee-M


People also ask

How do I undo a specific merge?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!

How do you revert a merge commit from a branch?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do you undo a merge conflict in git?

On the command line, a simple "git merge --abort" will do this for you. In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.


2 Answers

When you do:

git reset HEAD~1 public/stylesheet/application.css

... that changes the version of your file in the index (i.e. the staged version) to the version in HEAD~1 - your working copy remains the same. The current documentation explains this as:

This form resets the index entries for all <paths> to their state at <commit%gt;. (It does not affect the working tree, nor the current branch.)

If you do:

git checkout HEAD~1 -- public/stylesheet/application.css

... that will change both the working copy and the staged version to the version from HEAD~1. The -- is there for safety, just in case you have file name that can also be understood as a commit.

Now that you're got that change staged, you could amend the merge commit with:

git commit --amend

... which is fair enough as long has you haven't pushed the merge anywhere, since the merge will still just have changes from one branch or the other.

like image 165
Mark Longair Avatar answered Nov 16 '22 05:11

Mark Longair


You probably want git checkout commit-ish -- file.

like image 31
Bombe Avatar answered Nov 16 '22 07:11

Bombe