Reputation: 29906
How does Git handle filename changes?
Will a file name change be detected as a modification or will there be a "lost" file which needs to be removed and the new file needs to then be added with git add
?
Upvotes: 82
Views: 88335
Reputation: 1240
Go to the specific directory where your desired file is placed.
Then run the below command.
git mv [OLD FILENAME] [NEW FILENAME]
Upvotes: 2
Reputation: 396
'git mv old_file_name new_file_name'
will do the necessary modification. By default, it will rename the older file name with the newer file name as shown below,
rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git status
On branch development
Your branch is up to date with 'origin/development'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
problem_2.py
nothing added to commit but untracked files present (use "git add" to track)
rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git mv problem_1.py multiples_of_3_and_5.py
rkalaiselvan@CHN-LAP-RAVICHA MINGW64 ~/Documents/GitHub/project-euler-solutions/python (development)
$ git status
On branch development
Your branch is up to date with 'origin/development'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: problem_1.py -> multiples_of_3_and_5.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
problem_2.py
Upvotes: 2
Reputation: 468191
In each commit, Git records the state of your source tree, rather than whether there was a rename (or whatever) that produced that state. So, if you just rename a file normally (rather than with git mv
), the output of git status
will be something like:
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: foo.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bar.txt
no changes added to commit (use "git add" and/or "git commit -a")
If you then decide that you want to record the state of the tree with that file renamed, you can stage that change with:
git rm foo.txt
git add bar.txt
... then git status
will show you:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: foo.txt -> bar.txt
... and you can commit that with git commit
as usual:
git commit -m "Renamed foo.txt to bar.txt"
The important point is to bear in mind is that when Git tells you that a file has been renamed when you view history, that's because it has worked out that rename must have happened by comparing the state of the tree from one version to another - it doesn't mean that a rename operation was recorded in the history.
Upvotes: 32
Reputation: 689
git mv
This keeps the history and moves the file. You need to do a commit afterwards, so the repository is up-to-date.
Git will also pick up files that are moved in the file system on commit (sometimes) and on occasions comes up with a false positive when a file is deleted and a new (but similar) file is created.
It will also move the file in the file system (this can be overridden). So there isn't any need to do an git add
.
Upvotes: 1
Reputation: 133248
It will automatically be detected as a modification and the "new" file will be added to the index, so you only need one command:
$ git mv application.py newApplication.py
$ git status
# On branch buildServer
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: application.py -> newApplication.py
And a commit of course...
Upvotes: 121
Reputation: 28386
As previous answers have explained, Git derives a file rename operation from the change in content in your source tree. To record a rename operation, Git stores both a delete and add operation, and not the rename operation itself.
As Magnus Skog pointed out git mv <filename1> <filename2>
tells Git to add the content in <filename1>
to <filename2>
and remove <filename1>
from the file tree.
As Mark Longair explained, if instead of git mv
, you use shell command mv <filename1> <filename2>
, Git will not detect the rename operation until you invoke git rm <filename1>
and git add <filename2>
.
However, another way to tell Git about rename operations with mv
is to use git add --all
. This command instructs Git to detect and prepare to commit all files in your workspace that differ from those in the repository, including those that you've renamed:
$ ls
a b c d
$ mv d e
$ git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: d
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# e
no changes added to commit (use "git add" and/or "git commit -a")
$ git add --all
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: d -> e
#
git add --all
is a very convenient way to commit a large number of files that you've renamed in your workspace using a script or bulk renaming tool, for example.
Upvotes: 10