Git plays the history / contents of individual files

I work for a small company and our Git repo is a bit messy. I just did a git pull and my changes made earlier today are gone!

When I work on HEAD on the main branch, git log shows my last commit b94940c63ef965ce45b0d64ccfba4359134d2552 in its history.

Now, if I do git log filename for a problem file that has lost my changes, this commit is not displayed (only an earlier commit is displayed).

By executing git log --follow filename , my commit b94940c63ef965ce45b0d64ccfba4359134d2552 is shown as the most recent.

And of course, if I do this:

 git checkout b94940c63ef965ce45b0d64ccfba4359134d2552 git log filename 

then the commit is displayed and my changes are in the file!

In other words, the commit I made is shown in the branch history (blocking merging of branches), but individual modified files do not have this commit in their history! (unless I explicitly check what to do).

Questions:

  • How did this happen?

  • How can I fix this? (We have problems with multiple files in our repo)

+8
git git-log
source share
4 answers

Ok, got the problem. When a colleague reached out, he had conflicts. Instead of allowing it, he git reset every step-by-step file. It was like doing old_version git old_version on separate old files. So HEAD on the wizard ended up referencing some files with old_version.

Now I manually restore what he blew.

The moral of the story: modifying git operations (checkout, reset, etc.) on individual files is very dangerous.

+4
source share

It should be just a comment, but it would be difficult to read. After checking the wizard:

 git checkout master 

what conclusion

 git status 

and

 git whatchanged -m -p <path> 

and

 git log --graph --oneline b94940c63ef965ce45b0d64ccfba4359134d2552..master 

?

+1
source share

First of all, you need to understand what Git commands do and what data is stored in the repository.

  • Get a story visualization tool like Giggle or Gitk to view the commit history and see which commit is based on what to commit.

  • git pull does two things: fetch new commits from the remote repository and merge the remote repository head with the current head (in your current branch).

Thus, in light of this, you may be more careful in the future. Instead of git pull you can do git fetch and manually combine what you need to combine. This way you control what changes you make.

As for your current situation, I don't think Git is losing data. You said that your file is still in history. So, now you will probably need to perform some discarding ads (reverting to old versions) or patches (possibly manually) so that your project files are in the state you want.

0
source share

In fact, you can use this bash script in the folder in which you want to see which files could lose their commits:

 #!/bin/zsh for f in $(find . -name '*.php') do follow=$(git log --oneline -1 --pretty=format:"%h" -- $f) log=$(git log --follow --oneline -1 --pretty=format:"%h" -- $f) if [ $log != $follow ]; then echo "follow $follow , log $log => $f" fi done 
0
source share

All Articles