Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git is losing history/contents on individual files

Tags:

git

git-log

I work in a small company and our Git repo is a little messed up. I just did a git pull and my changes made earlier today are gone!

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

Now if I do git log filename for the problematic file that lost my changes, that commit is not shown (only shows an earlier commit).

Performing git log --follow filename, my commit b94940c63ef965ce45b0d64ccfba4359134d2552 is shown as most recent.

And sure enough if I do:

git checkout b94940c63ef965ce45b0d64ccfba4359134d2552
git log filename

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

In other words, the commit I made is shown in the branch history (blocking a branch merge), but individual modified files do not have that commit in their history! (unless I explicitly checkout that commit).

Questions:

  1. How on earth did this happen?

  2. How do I fix it? (We have problems with multiple files in our repo)

like image 665
UsAaR33 Avatar asked Aug 05 '11 00:08

UsAaR33


People also ask

Where is git history saved?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo. This repo is usually in a hidden folder called . git sitting next to your files.

Does git rm remove history?

No, git rm will only remove the file from the working directory and add that removal into the index. So only future commits are affected. All previous commits stay the same and the history will actually show when you removed the file from the repository.

Does GitHub keep history?

On GitHub, you can see the commit history of a repository by: Navigating directly to the commits page of a repository. Clicking on a file, then clicking History, to get to the commit history for a specific file.


2 Answers

Alright figured out the problem. When a coworker pulled, he got some conflicts. Rather than resolving, he git reset every staged file. This was akin to doing a git checkout old_version on individual old files. So HEAD on the master ended up referring to some files that had old_version.

Now I'm manually restoring what he blew out.

Moral of story: Modifying git operations (checkout, reset, etc.) on individual files are quite dangerous.

like image 145
UsAaR33 Avatar answered Oct 05 '22 14:10

UsAaR33


This should be just a comment, but it would be hard to read. After checking out master:

git checkout master

what's the output of

git status

and

git whatchanged -m -p <path>

and

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

?

like image 45
Ryan Stewart Avatar answered Oct 05 '22 13:10

Ryan Stewart