Reputation: 297
The reason I am asking this question is that I suspect that occasionally where I work sometimes a commit seems to just "disappear" from a remote branch for no reason. I have seen in a different post on here that indeed this is possible to do using:
git push origin HEAD --force
I do hate feeling paranoid but would ease my mind if I could check this. I read somewhere about a command:
git reflog
It looked like the man page for this says that this command can also be used to delete entries from the log which well kind of sucks. Is there anyway I can check this and ease my mind?
Upvotes: 2
Views: 2231
Reputation:
You can indeed use git reflog
to check if there's a missing commit on a remote branch, but it will only work if you have an up-to-date copy of that remote branch in your local repo (your remote-tracking branch).
You can keep your local remote-tracking branch up-to-date by frequently running git fetch
. However, if someone pushes a commit that you haven't fetched and then later deletes it before your next fetch, then you're not going to know that there was a commit that was deleted. You'll only find out that the remote branch's history has been altered if you have a copy of the original history that has been changed, in which case Git will tell you that there has been a forced-update:
$ git fetch origin
From c:/Users/Keoki/Documents/GitHub/bare
+ 02f0d6e...5c7b77b master -> origin/master (forced update)
If someone has been force-pushing to your remote, it should be blatantly obvious as soon as you fetch or pull, because it will show up as a forced-update (as I've pointed out), unless you don't fetch or pull from your remote frequently. If you have direct access to your remote repo, you could check the reflogs directly on it as well.
You mention,
It looked like the man page for this says that this command can also be used to delete entries from the log which well kind of sucks.
git reflog
will only delete entries from the reflog (not the same log you get with git log
) if you pass any of the following extra options (from the official documentation):
--expire=<time>
--expire-unreachable=<time>
--all
Otherwise, git reflog
is read-only, and will just list the history of where your references has been. For your particular example, you can see the reflog history of your remote-tracking branch by using:
git reflog <remote>/<branch>
For example,
$ git reflog origin/master
5c7b77b refs/remotes/origin/master@{0}: fetch origin: forced-update
02f0d6e refs/remotes/origin/master@{0}: fetch origin: fast-forward
fc32eac refs/remotes/origin/master@{1}: pull origin master: fast-forward
b6a06e1 refs/remotes/origin/master@{5}: fetch origin: fast-forward
35a91e4 refs/remotes/origin/master@{6}: update by push
019ea3a
In the example above, you'll see the origin/master
was force-updated in the most recent (top) entry of the reflog, indicating that the original history of the remote branch has been altered, possibly indicating that a commit has been deleted (though rewritten commits will also cause forced-updates).
Upvotes: 2
Reputation: 11610
There are multiple ways effectively to delete or otherwise alter Git logs, including (from Git's docs): amend
, rebase
, squash
, and filter-branch
. Browse the options at:
http://git-scm.com/book/en/Git-Tools-Rewriting-History
Regarding the function of reflog
(from The Linux Kernel Archives):
https://www.kernel.org/pub/software/scm/git/docs/git-reflog.html
Reflog is a mechanism to record when the tip of branches are updated. This command is to manage the information recorded in it.
The reflog is useful in various git commands, to specify the old value of a reference. For example, HEAD@{2} means "where HEAD used to be two moves ago", master@{one.week.ago} means "where master used to point to one week ago", and so on. See gitrevisions(7) for more details.
Basically, Git keeps two logs: the one we all know, and the reflog, the latter of which exists only in your local repo. Git updates the reflog anytime HEAD moves (due to new commits, checking out branches, or resetting). It's commonly accessed when restoring commits after a hard reset.
To view the reflog, enter:
git reflog
To view with greater detail, enter:
git log --walk-reflogs
As to your question, I suppose you could run git log
and git reflog
(or git log
and git log --walk-reflogs
) and compare the results (remembering that reflog
only records local actions).
Upvotes: -1