user976850
user976850

Reputation: 1106

Git - do amended commits persist?

On my Git branch, I created a commit and pushed. later, I added changes to the index, used git commit --amend, and then git push -f. My new commit overrode the first one both locally and remotely on my branch.

At this point, I'd expect (or want) the original commit to no longer exist, or at least exist (perhaps in the reflog) but no longer have a parent, so, once I run GC, it'll cease to exist. However, it seems like the commit is still there, and its parent is still intact. (In this case, the way I found out about it was via Jira, which linked me to my overridden commits on github)

Why is that so? How will it get deleted? Does this mean that, whenever I accidentally push a password to GitHub, and then use git commit --amend or git reset, and then git push -f, the commits never actually truly get deleted?

Upvotes: 2

Views: 232

Answers (2)

VonC
VonC

Reputation: 1325137

git 2.7 (Q4 2015) introduces a warning regarding "git gc --prune=all"

See commit fae1a90 (14 Oct 2015) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit ce555f3, 20 Oct 2015)

Documentation/gc: warn against --prune=<now>

"git gc" is safe to run anytime only because it has the built-in grace period to protect objects that are created by other processes that are waiting for ref updates to anchor them to the history.
In order to run with no grace period, the user must make sure that the repository is quiescent.

quiescent: already mentioned in the git user manual page:

You should only run git prune on a quiescent repository -- it's kind of like doing a filesystem fsck recovery: you don't want to do that while the filesystem is mounted.

So quiescent means in a state or period of inactivity or dormancy.

The new doc mentions:

--prune=all prunes loose objects regardless of their age
(do not use --prune=all unless you know exactly what you are doing.
Unless the repository is quiescent, you will lose newly created objects that haven't been anchored with the refs and end up corrupting your repository
).
--prune is on by default.

Upvotes: 1

Melebius
Melebius

Reputation: 6695

From git help gc:

--prune=<date>

Prune loose objects older than date (default is 2 weeks ago, overridable by the config variable gc.pruneExpire). --prune=all prunes loose objects regardless of their age. --prune is on by default.

So you probably want to run git gc --prune=all.

Upvotes: 1

Related Questions