Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming with Git: Using git mv vs deleting a file and adding another

Tags:

git

rebase

commit

I 'renamed' a file manually by deleting the old one and adding another with the new name. How do I undo this in the commit history and use git mv instead?I need to rebase my feature branch to the updated parent where the old file name is still being used. I don't want to lose the changes from both the old-name file and new-name file.

like image 387
Victor Kironde Avatar asked Mar 17 '17 09:03

Victor Kironde


People also ask

How do you delete and rename a file in git?

$ git commit -m "Rename file" # Commits the tracked changes and prepares them to be pushed to a remote repository. # To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again. Push the changes in your local repository to GitHub.com.

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.


1 Answers

git mv does the exact same thing as what you did.

When scanning the history, it then tries to spot "renaming" actions by comparing contents, and saying "hmm ... if those two files have 85% content identical, I'll say it's a renaming"

Use git rebase -m to tell git to look more carefully for renaming.

like image 56
LeGEC Avatar answered Nov 01 '22 11:11

LeGEC