Changing file names in git repo

How will the git file name change?

Will there be a change in the file name as a modification or will there be a β€œlost” file that needs to be deleted and the new file should be added using git add ?

+65
git
Apr 05 2018-11-11T00:
source share
5 answers

It will be automatically 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 of course commit ..

+86
Apr 05 '11 at 12:11
source share

Each git commit records the state of the source tree, and not whether there is a renaming (or something else) that created this state. So, if you just rename the file normally (and not with git mv ), the output of git status will be something like this:

 # 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 the renamed file, you can make this 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 this with git commit , as usual:

 git commit -m "Renamed foo.txt to bar.txt" 

It is important to remember that when git tells you that the file was renamed when viewing the history, this is because it decided that the renaming must have happened by comparing the state of the tree with one version for another - this does not mean that the history was written rename operation.

+26
Apr 05 2018-11-11T00:
source share

As the previous answers explained, Git deduces the file rename operation from changing the contents in the source tree. To write a rename operation, Git stores the delete and add operation, not the rename operation.

As Magnus Skog pointed out git mv <filename1> <filename2> tells Git to add content to <filename1> to <filename2> and remove <filename1> from the file tree.

As Mark Longair explained , if instead of git mv you use the shell command mv <filename1> <filename2> , Git will not detect the rename operation until you call git rm <filename1> and git add <filename2> .

However, another way to tell Git to rename operations with mv is to use git add --all . This command tells Git to detect and prepare to commit all the files in your workspace that are different from the files in the repository, including the ones you renamed:

 $ ls abcd $ mv de $ 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 renamed in your workspace, for example, using the script tool or bulk renaming.

+8
Apr 05 2018-11-11T00:
source share

git mv

Saves history and moves the file. You need to commit after that so that the repo is up to date.

git will also collect files that are moved to the file system upon commit (several times), and sometimes a false positive value appears when a file is deleted and a new (but similar) file is created.

It will also move the file to the file system (this can be overridden). Therefore no need to do git add

+1
Apr 05 '11 at 12:18
source share

'git mv old_file_name new_file_name'

make the necessary changes, by default the old file name will be renamed to a newer one, 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 
0
Apr 07 '19 at 16:27
source share



All Articles