Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git mv (rename)

Tags:

git

undo

rename

mv

People also ask

What happens if you rename a file in git?

Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).

How do I rename a directory in git without losing history?

emiller/git-mv-with-history. git utility to move/rename file or folder and retain history with it. # git-mv-with-history -- move/rename file or folder, with history. # Git has a rename command git mv, but that is just for convenience.


Non-cheeky answer:

git mv file2 file1

Updates the index for both old and new paths automatically.

Check documentation of git mv


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

git reset --hard

git reset HEAD file2

did the trick for me


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

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.


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.