newbie
newbie

Reputation: 1033

What is a fix for manually renamed files in a git project?

I had two files a.rb and b.rb. I manually renamed them in Sublime Text to c.rb and d.rb and made some changes to those files and pushed my branch to the remote repository using these commands:

git add
git commit -m "message"
git push origin branch-name

I should have done a git mv instead instead of manually renaming it.

Now I need a proper way to fix this issue so that others don't have merge conflicts. The only way I can think of is:

git checkout branch.
git rm c.rb.
git mv a.rb c.rb.
copy paste my old c.rb code to a.rb.

Is there a better way to fix this?

Upvotes: 4

Views: 3538

Answers (3)

Derek Mahar
Derek Mahar

Reputation: 28376

Assuming that you previously pushed new files c.rb and d.rb, your workspace HEAD points to this commit, and files a.rb and b.rb are deleted in your workspace:

[dmahar@dmahar rename3]$ git reset --hard HEAD
HEAD is now at d77decf Rename 'a.rb' to 'c.rb' and 'b.rb' to 'd.rb'.
[dmahar@dmahar rename3]$ ls
a.rb  b.rb  c.rb  d.rb
[dmahar@dmahar rename3]$ git rm c.rb d.rb
rm 'c.rb'
rm 'd.rb'
[dmahar@dmahar rename3]$ ls
a.rb  b.rb
[dmahar@dmahar rename3]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    c.rb
#   deleted:    d.rb
#
[dmahar@dmahar rename3]$ git commit --message "Delete 'c.rb' and d.rb'."
[master 60f97ab] Delete 'c.rb' and d.rb'.
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 c.rb
 delete mode 100644 d.rb
[dmahar@dmahar rename3]$ ls
a.rb  b.rb
[dmahar@dmahar rename3]$ git mv a.rb c.rb
[dmahar@dmahar rename3]$ git mv b.rb d.rb
[dmahar@dmahar rename3]$ git commit --message "Rename 'a.rb' to 'c.rb' and 'b.rb' to 'd.rb', again."
[master 8182195] Rename 'a.rb' to 'c.rb' and 'b.rb' to 'd.rb', again.
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename a.rb => c.rb (100%)
 rename b.rb => d.rb (100%)
[dmahar@dmahar rename3]$ ls
c.rb  d.rb
[dmahar@dmahar rename3]$ git log -M --summary
commit 8182195c1494541ab1dcaa97607ce4dc8b277d56
Author: Derek Mahar <[email protected]>
Date:   Thu May 30 15:21:12 2013 -0400

    Rename 'a.rb' to 'c.rb' and 'b.rb' to 'd.rb', again.

 rename a.rb => c.rb (100%)
 rename b.rb => d.rb (100%)

commit 60f97abb56e5b1e32d02dfb8ff14400910d4fef4
Author: Derek Mahar <[email protected]>
Date:   Thu May 30 15:20:11 2013 -0400

    Delete 'c.rb' and d.rb'.

 delete mode 100644 c.rb
 delete mode 100644 d.rb

commit d77decf9863fb1b4f8a2902becf687e6ececf4ce
Author: Derek Mahar <[email protected]>
Date:   Thu May 30 12:59:15 2013 -0400

    Rename 'a.rb' to 'c.rb' and 'b.rb' to 'd.rb'.

 create mode 100644 c.rb
 create mode 100644 d.rb

commit 219ce73f40d46690d77751fccd86b7c5a60b0d7d
Author: Derek Mahar <[email protected]>
Date:   Thu May 30 12:58:24 2013 -0400

    Add 'a.rb' and 'b.rb'.

 create mode 100644 a.rb
 create mode 100644 b.rb

Upvotes: 0

jcoder
jcoder

Reputation: 30035

git mv doesn't do anything special. It just does the git add and the git rm for you. The fact that it is a moved file rather than an add and delete isn't recorded anywhere, it's inferred when you request a log.

All you need to do is make sure that you did a "git add" for the new name which I think you did, and a "git rm" on the existing file.

Upvotes: 7

Bijendra
Bijendra

Reputation: 10015

You can do a soft reset of our branch "Head" just before the commit. Then use the git mv command to rename the file.

If you don't want to do the above, then you can just delete the files "a.rb" and "b.rb":

$ git ls-files --deleted | xargs, 

This will add the deleted files. Commit the change and push it so if the collaborators in the project pull it, the new files are pulled in and the previous ones are deleted.

Upvotes: 0

Related Questions