Undo git mv (rename)

GitRenameUndoMv

Git Problem Overview


What is the right way to undo a rename in git, like:

git mv file1 file2

Git Solutions


Solution 1 - Git

Non-cheeky answer:

git mv file2 file1

> Updates the index for both old and new paths automatically.

Check documentation of git mv

Solution 2 - Git

If you have done no other changes (that you want to keep) since the last commit, you can do

git reset --hard

Solution 3 - Git

git reset HEAD file2

did the trick for me

Solution 4 - Git

In my case, I moved an entire folder, then realized I should not have.

I really liked @Dave Konopka's answer, but I did not have much success with that approach (maybe my version of GIT (1.8.4)? My files still showed as deleted. I had other changes on the stack that I did not want to lose (unfortunately).

I did have success doing this:

git reset moved_folder
git checkout original_folder

Solution 5 - Git

It depends on what you want to accomplish. If you want it to appear as if the file was never moved, then you can reset (or rebase) back to before the move. If you don't care about the history, then just move it back.

Solution 6 - Git

If you've accidentally renamed a large number of files and want to get back to where you started, delete all the renamed files that show up as adds under a git status call.

Once you delete all the changed files you can run git checkout -- * to get back the original file names locally.

Solution 7 - Git

git reset HEAD file2
git checkout -- file1
rm file2

The first command unstages file2 but leaves a copy of it around. The second command restores the original file and the third deletes the new file.

Solution 8 - Git

The trick I used was to do a git stash to undo all my changes (which includes restoring the mv'd files) and then deleted the stash with git stash drop.

Solution 9 - Git

Less scary is to go to top level of the repo and do:

git reset
git checkout .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjrdiokoView Question on Stackoverflow
Solution 1 - GitCanSpiceView Answer on Stackoverflow
Solution 2 - GitKlas MellbournView Answer on Stackoverflow
Solution 3 - GitzbigView Answer on Stackoverflow
Solution 4 - Gitzedd45View Answer on Stackoverflow
Solution 5 - GitWilliam PursellView Answer on Stackoverflow
Solution 6 - GitDave KonopkaView Answer on Stackoverflow
Solution 7 - GitKamaraju KusumanchiView Answer on Stackoverflow
Solution 8 - GitsevencontinentsView Answer on Stackoverflow
Solution 9 - Gitw0mbatView Answer on Stackoverflow