Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: renamed file manually, Git confused

I am using Git and manually renamed a file that I had added to the repository. Now, I have added the "new" file which I renamed to the repository but Git complains that the "old" file has been deleted. So how can I make Git forget about the old file? Better yet, how do I tell Git that the "new" file really is the "new" file so that I can keep the change history intact?

like image 547
mmersz Avatar asked Jan 16 '11 22:01

mmersz


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 does git know a file was renamed?

Git addresses the issue by detecting renames while browsing the history of snapshots rather than recording it when making the snapshot. [37] (Briefly, given a file in revision N, a file of the same name in revision N−1 is its default ancestor.

Can we rename a file in git?

We use the git mv command in git to rename and move files. We only use the command for convenience. It does not rename or move the actual file, but rather deletes the existing file and creates a new file with a new name or in another folder.

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.


1 Answers

There is no problem. Simply git rm old or even git add -A and it will realize that it is a rename. Git will see the delete plus the add with same content as a rename.

You don't need to undo, unstage, use git mv etc. git mv old new is only a short hand for mv old new; git rm old; git add new.

like image 87
Jakob Borg Avatar answered Sep 22 '22 21:09

Jakob Borg