Reputation: 669
I have a commit at Wed Aug 27 19:43:46 2014 +0800,
commit bbdbbb7214de8611a787c92daf93dbc2719600d0
Author: malloc ([email protected])
Date: Wed Aug 27 19:51:17 2014 +0800
commit a5f8bcbf7fdfa995325a338a02ad8eef611ac9f8
Author: malloc ([email protected])
Date: Wed Aug 27 19:43:46 2014 +0800
the edit of the commit shows that 3 files had been modified.
git d --name-only a5f8bcb^..a5f8bcb
res/layout/layout_login.xml
base/BaseAct.java
ui/login/Login.java
(END)
then other people commit their modifies.
commit 833dee16869ceb834cb1b8d8ac38bf3d0f147e66
Author: simon ([email protected])
Date: Wed Aug 27 20:13:42 2014 +0800
commit b391737ac94d5d779c1cb00b05a7c3bccee98915
Author: muham ([email protected])
Date: Wed Aug 27 20:00:35 2014 +0800
at version b391737 the Login.java is the new version, but at version 833dee1 the content of Login.java is old version. (and also other filess been modified)
check the commit 833dee1, only one file is commited
git d --name-only 833dee1^..833dee1
res/values/strings.xml
(END)
and then check the log of Login.java
git log ui/login/Login.java
commit c5a5ae9a48c2f1d44b6cd3654c20834ed49b3991
Author: simon ([email protected])
Date: Thu Aug 21 15:54:39 2014 +0800
commit 7e65405d19a946349ee4ac07176a37098f52867b
Author: shubin ([email protected])
Date: Fri Aug 15 15:22:01 2014 +0800
the lastest commit is at Thu Aug 21 15:54:39 2014 +0800, there is no log of my edit.
How could this happen? The commit 833dee1 didn't have file I edited and history of Login.java also lost my edit .
Is there some command that I can find the whole history of Login.java?
=========================================
we use only one branch master, and the log is not only at local, git clone from remote get the same log history.
Add some output:
$ git log --oneline --graph --color=auto --decorate --all ui/login/Login.java
c5a5ae9 is at Aug 21 15:54:39 2014, many days ago, 9f88669 and b77af0c is commits after we find the problem, git losts the log at a5f8bcb.
git log --oneline --graph --color=auto --decorate 833dee16...bbdbbb72
Upvotes: 1
Views: 56
Reputation: 490098
Even with the edit it's hard to be sure, but I think there is enough information here to guess what happened, at this point: you made the commit (on master
since that's your lone branch), but then you threw it off the branch, probably by running git reset --hard
(almost certainly some variant of git reset
).
In fact, at least two commits are not on your branch: you show:
bbdbbb7214de8611a787c92daf93dbc2719600d0
a5f8bcbf7fdfa995325a338a02ad8eef611ac9f8
at the top, but your main git log
output shows neither. In fact 833dee1...
is also missing from that output, as is b391737...
.
The nice thing about git is that the commits are in fact still in your repository. Thanks to reflogs, commits stick around for at least 30 days by default. All you need to do is resurrect or copy them.
If you want to resurrect that commit (and any previous commits that you also discarded via git reset
), give the tip-most commit a name (branch or tag, either one will do). Assuming bbdbbb7...
is the tip-most and you want to create a branch for it:
git branch somework bbdbbb7 # you can use the full 40-char SHA1
# or an abbreviation, either does thes
# same job here
will do the trick; now git log --graph --decorate --oneline --all
should show both those commits, under the new branch-name somework
. (If bbdbbb7...
is not the tip-most commit, and a5f8bcb...
is, you can point the new branch there. Look through the reflogs, git reflog
or git log -g
, to find any "lost" commits you want to bring back.)
If you simply want to copy some previous commit, you can use git cherry-pick
to do that. This will attempt to repeat the same changes in the named commit, but applying them to your current work-tree and then making a new commit from the result (and also copying the original commit message for this new commit).
Upvotes: 1
Reputation: 333314
It's a little hard to tell what's going on from just the information provided.
You have listed a few commits, but have done nothing to show what branches they are in or what the merge history is. It's entirely possible that the commits you seem to have lost are on a different branch than the one you are currently looking at.
Another possibility is that you're being misled by the fact that most recent commits may have timestamps that are older than the ones you're missing. It's possible to rebase commits, applying older commits on top of newer ones, so when you look at the history you see the older ones first.
Or another possibility is that you have somehow just discarded your commits, leaving them detached within your local repository without ever having pushed them and merged them with the other history. This could happen if you ever did a git reset
to the upstream branch.
Here is how you can find out what's going on. One of the best ways to do so is to visualize the history. You can do so with a graphical tool like gitk
if you have it installed, or just use git log --graph
to do it in the terminal if not.
Try running the following commands to see what's going on. If you're not sure how to interpret the output, edit your question to include it and I can explain further. The first one should show all of the history of that one file, in all branches, local and remote. The second should show the history that is included in one of those commits, but not the other.
$ git log --oneline --graph --color=auto --decorate --all ui/login/Login.java
$ git log --oneline --graph --color=auto --decorate 833dee16...bbdbbb72
You can also look at a history of what you have done in your repository. Just type git reflog
; that will show you the history of which particular versions you have had checked out; so you will be able to see if there was a time when you had your missing commit checked out, and then switched to another commit that didn't include it.
Upvotes: 0