knittl has already compiled a good list of the commands that rewrite history, but I wanted to build upon his answer.
Can you provide a list of [...] the operations or commands that can compromise the history in git? What should be absolutely avoided?
First of all, there is nothing wrong with rewriting/deleting history per se; after all, you probably routinely create feature branches, keep them strictly local, then delete (after merging them or realising they lead you nowhere) without thinking twice about it.
However, you can and certainly will run into problems when you locally rewrite/delete history that other people have already access to and then push it to a shared remote.
Operations that should count as rewriting/deleting the history of a local repo
Of course, there are dumb ways of corrupting or deleting history (e.g. tampering with the contents of .git/objects/
) , but those are outside the scope of my answer.
You can rewrite history of a local repo in various ways. The section of the Pro Git book entitled Rewriting history, mentions a few
git amend --commit
git rebase
git filter-branch
- Roberto Tyley's BFG Repo Cleaner (a 3rd-party tool)
Arguably, there are more. Any operation that has the potential to alter or otherwise move a non-symbolic reference (branch or tag) and make it point to a commit that is not a descendant of the branch's current tip should count as rewriting local history. This includes:
git commit --amend
: replaces the last commit;
- All forms of rebase (incl.
git pull --rebase
);
git reset
(see an example below);
git checkout -B
and git branch -f
: resets an existing branch to a different commit;
git tag --force
: recreates a tag with the same name but potentially pointing to another commit.
Any deletion of a non-symbolic reference (branch or tag) may also be considered history deleting:
git branch -d
or git branch -D
git tag -d
Arguably, deleting a branch that has been fully merged into another should be considered only a mild form of history deleting, if at all.
Tags are different, though. Deleting a lightweight tag is not such a big deal, but deleting an annotated tag, which is a bona fide Git object, should count as deleting local history.
Operations that rewrite/delete the history of a remote repo
As for as I know, only a git push -f
(equivalent to git push --force
) has the potential to rewrite/delete history in the remote repository.
That said, it is possible to
- disable the ability to force-update remote branches to non-fast-forward references, by setting
receive.denyNonFastForwards
on the server.
- disable the ability to delete a branch living on a remote repository, by setting
receive.denyDeletes
on the server.
Moreover I use git reset
a lot, but am not completely aware of the possible damage I could do to the repository (or to the other contributors copies). Can git reset
be dangerous?
git-reset
, as mentioned by knittl, usually changes where a branch reference points. This command can be dangerous, in so far as it can make reachable commits become unreachable. Because a picture speaks a thousand words, consider the following situation:
You're on the master
branch, which points at commit D
. Now, let's say you run, for instance,
git reset master~2
A soft reset is considered to be the most benign form of reset, because it "only" changes where the current branch points to, but doesn't affect the staging area or your working tree. That said, merely changing where a branch points to in that fashion has ramifications: after that soft reset, you will end up with
Commits C
and D
, which were reachable from master
before the reset, have now become unreachable; in other words, they're not ancestors of any reference (branch, tag, or HEAD). You could say that they're in "repository limbo"; they still exists in your Git repo's object database, but they will no longer be listed in the output of git log
.
If you actually found those commits valuable before the reset, you should make them reachable again by making some reference (e.g. another branch) point to commit D
again. Otherwise, commits C
and D
will end up dying a true death when Git runs its automatic garbage collection and deletes unreachable objects.
You can, in theory, fish commit D
out of the reflog, but there is always a risk that you will forget about those unreachable commits or won't be able to identify which entry of the reflog corresponds to commit D
.
In conclusion, yes, git-reset
can be dangerous, and it's a good idea to make sure the current tip of the branch you're about to reset will remain reachable after the reset. If needed, create another branch there before the reset, just in case, as a backup; and if you're sure you want to forget those commits, you can always delete that branch later.
git reset --soft/--hard
(if you don't reset to already pushed commits). I usually work on new features on a local branches, and thengit merge
orgit cherry-pick
them to the master branch before thegit push origin
. You can do (i guess) anything safely as long you stay on local branches (and, as said, don't touch already pushed commits). – Presage