Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing file names in a Git repository

Tags:

git

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?

like image 524
some_id Avatar asked Apr 05 '11 12:04

some_id


People also ask

How do I change a file name in git bash?

Step 1: Open GitHub. Step 2: Open the repository to rename any file in that repository. Step 3: Open the file which we want to rename. Step 4: Click the edit button and rename the file.

How do you changes file names?

Find and select the file, then select File > Rename. Type the new name and press Enter.

Can you rename folders in GitHub?

To change the name of a folder or subfolder in a specific directory in GitHub, you should follow the below steps: Open the GitHub directory where the folder is located. Click on any file inside this folder to open it. Click on "Edit File" .


2 Answers

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...

like image 164
ralphtheninja Avatar answered Oct 15 '22 06:10

ralphtheninja


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.

like image 43
Mark Longair Avatar answered Oct 15 '22 07:10

Mark Longair