Renamed files inside git

I am a new programmer on the team. And on my first day, I have this renamed file inside the scene, ready to be executed with git:

$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: .gitignore new file: reports/SQLS.rar renamed: account/enter_rules.php -> enter_rules.old.php Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: account/enter_rules.old.php Untracked files: (use "git add <file>..." to include in what will be committed) account/enter_rules.php 

For the first two lines, I have no problem. I know what is going on. But for the last renamed:, I'm not sure what to do. The system is working well.

Inside the working directory there are:

 account/enter_rules.php 

I can not find enter_rules.old.php file.

It seems that another programmer created a copy from the file, named the actual one as .old, did some tests and new code, which made changes and forgot to commit. Then he manually deleted old.php

What should be the best approach to deal with this situation? I would like to set the status to git status before starting to work on it and make my changes.

I found this post, but I'm not sure what I should or could have done in my situation. Renaming files in git


After reading all the suggestions that helped me a lot, this is what I did:

  • I just made a copy (only for safe) from enter_rules.php file
  • I said โ€œgit (index) that the file name has been changed. $git commit -m "committing name changes" . At the moment, git does not know that .old has been deleted.
  • Then I typed $git rm "account/enter_rules.old.php" to resolve the line: No changes were made to commit:. Similarly, we use $git add . for tell git to track <file> , we should use $git rm <file> for tell git to forget about <file> .
  • Then I typed $git status and I got the message GREEN: deleted: account / enter_rules.old.php. . Therefore, I must indicate to the index that the .old file has been deleted. I typed $git commit -m "committing delete changes"
  • Now git knows that enter_rules.php has been renamed enter_rules.old.php and then enter_rules.old.php has been deleted (deleted).
  • Then I solved the last line ( Tracked files :). I just told git that a new file was created and it calls enter_rules.php . I added and did as it should be.

All the suggestions helped me solve the problem, so I will try to be honest and give a check, whose is less.

+6
source share
3 answers

It seems to me what happened here:

In the initial state, there were two files account/enter_rules.php and account/enter_rules.old.php .

Your colleague should have done something like this:

 git rm account/enter_rules.old.php git mv account/enter_rules.php account/enter_rule.old.php 

then edited the new account/enter_rules.php and left it uninstalled.

If you're alright with the changes, just

 git add account/enter_rules.php git commit 

else

 rm account/enter_rules.php git reset HEAD 

to undo its changes.

What seems strange to me, why are you working on a fellow repository clone with uncommitted changes?

+1
source

Although git says that enter_rules.old.php been renamed, it also says that it has been removed below:

 Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: account/enter_rules.old.php 

This means that the file is gone, and in order for git apply this deletion to the repository, the deletion action must be included in your commit. Since the change is tracked but not executed, you can do this with a simple command (described in part 3 below).

  • Add account/enter_rules.php with git add account/enter_rules.php .
  • If you run git status again, enter_rules.old.php should be marked for deletion, and account/enter_rules.php should be set for commit.
  • Run git commit --all to complete all your incremental changes.

Running git commit --all will also result in the deletion that your colleague performed, but didnโ€™t.

It looks like your employee used git mv to rename the file and then deleted it, but did not perform the deletion.

+1
source

Git does not work with files in the working folder. Instead, if working on something called an "index". This means that you can modify the files, put them in the index ("soon, I would like ...") and make the index persistent with git commit .

The main advantage here is that you can collect many changes in the index using separate commands (for example, when you made several changes and would like to put them in separate commits). This process is called staging. git add works, for example, with an index.

What happened in your case:

  • You renamed the file (possibly in your IDE) and told Git to complete the โ€œstepโ€ of the change. Git remembers this in the index.
  • You deleted the file without telling Git
  • You got an instance of account/enter_rules.php from somewhere and copied it to your working folder, also without specifying Git.

For Git, the situation now is that it knows that once there was the enter_rules.old.php file that you wanted to commit. You can do this because Git remembers enough to do this.

At the same time, your workspace has changed a lot. The phased file disappeared and a new copy appeared. Because Git cannot read your mind, it is not trying to figure out what this can mean. He simply lists the facts.

Now clear:

To make Git forget about renaming enter_rules.old.php , use git reset HEAD enter_rules.old.php

Now you need to remember that account/enter_rules.php . If you modify this file, it will display it as modified. Run git status to make sure.

+1
source

Source: https://habr.com/ru/post/988320/


All Articles