News for hackers. Stuff that matters.
Re:Is Git Irreplaceable? (2019) by DaiPlusPlus on Tuesday January 07, 2020 @09:06AM (#21979975)
I wish git had a “metahistory” feature to allow everyone to undo anything. A `git revert` isn’t of any help when you’ve already merged and pushed.
Re:Is Git Irreplaceable? (2019) by jerf on Tuesday January 07, 2020 @09:07AM (#21979987)
It's called "git reflog": https://www.edureka.co/blog/git-reflog/

Though you do have to have committed. One of the things I hammer on in my tutorials for work is that if you get confused in git, make sure you commit. If you commit, you can take your problem to the other engineers and we can almost certainly get you straightened away. Fail to commit, though, and you really may lose something.

Also, metapoint about git: While I won't deny its UI carries along some dubious decisions carried over from the very first design, in 2020, basically, if you thing "Git really ought to be able to do [this sensible thing]", it can. It has that characteristic that open source software that has been worked on by a ton of contributors has, which is that almost anything you could want to do was probably encountered by somebody else and solved five years ago. It just may take some searching around to figure out what that is. (And on the flip side, when you read the git man pages and are going "Why the hell is that in there?", the answer may well be "a problem that you're going to have in six months".)

Re:Is Git Irreplaceable? (2019) by jorvi on Tuesday January 07, 2020 @09:34AM (#21980224)
> make sure you commit

This is not absolute gospel. If you screw up a rebase and commit, whatever you removed in the rebase is simply gone.

Re:Is Git Irreplaceable? (2019) by jerf on Tuesday January 07, 2020 @09:52AM (#21980401)
Are you saying that with knowledge of what "git reflog" is? I suspect not. I'd really need to see a sequence of commands that removes committed state from the repo to buy this. If you try to produce it, bear in mind the first thing I'm going to do is run "git reflog" on the result, so if you find your committed state is still there, then I'm going to say it's still saved.

(That's not a git thing. I don't really even want some sort of hypothetical source control system that literally tracks every change I make. It's technically conceivable and should be practical to what would at least be considered a "medium sized" project in the git world, but I'd just be buried in the literally thousands of "commits" I'd be producing an hour. Failing that sort of feature, a source control system can't help but "lose" things not actually put into it.)

Re:Is Git Irreplaceable? (2019) by mannykannot on Tuesday January 07, 2020 @10:57AM (#21981133)
As I am not familiar with the details of reflog (I don't recall ever using it) I took a look at the article. I wasn't long until I reached what looks like a caveat: "This command has to be executed in the repository that had the lost branch. If you consider the remote repository situation, then you have to execute the reflog command on the developer’s machine who had the branch." Joe, who works on another continent, quit last week and his computer was a VM...

OK, so we have backups of his VM and we can recreate a clone of it, but will that be satisfactory? Are there any issues with hardware MAC addresses or CPU ids? How far down the rabbit hole of git minutiae do you have to go before you are confident that you can do all basic source-control operations safely?

Re:Is Git Irreplaceable? (2019) by jerf on Tuesday January 07, 2020 @12:11PM (#21981951)
No source control system can solve the problem of not having things it was never given.
Re:Is Git Irreplaceable? (2019) by gallier2 on Tuesday January 07, 2020 @12:59PM (#21982586)
git log --reflog --all

is the more important imo than

git reflog

Re:Is Git Irreplaceable? (2019) by RichardCA on Tuesday January 07, 2020 @10:58AM (#21981148)
The main thing that people fail to understand is that commits are immutable and the overall commit graph is immutable (with the caveat that pathways in the graph that don't end in a branch head are subject to garbage collection).

A rebase does not destroy information. It creates new commits and moves the branch head to a different spot on the graph.

The reason git is seen as painful is because you can't claim expertise until you develop the ability to form a mental map of the graph. But once you do this the lights turn on and everything starts to make sense.

This is why the mantra "commit early and often" still holds. The more experienced git user will tell the newer people this, so when they come with a mess it will always be recoverable.

Re:Is Git Irreplaceable? (2019) by fulafel on Tuesday January 07, 2020 @11:24AM (#21981413)
That GC a pretty big caveat! Combined with the fact that unreferenced objects are never pushed.

reflog is like undelete in filesystems, it's a probabilistic accident recovery mechanism for an individual computer (repo checkout in this case) that you can try to use if you don't have backups.

Re:Is Git Irreplaceable? (2019) by vvillena on Tuesday January 07, 2020 @12:59PM (#21982583)
In any case, git garbage collection isn't a common phenomenon. It usually triggers every few weeks, even in repos with high activity. The chance of hitting a GC that deletes an untracked commit you need is extremely small.
Re:Is Git Irreplaceable? (2019) by chousuke on Tuesday January 07, 2020 @09:46AM (#21980329)
How would it be gone? You can't rebase onto a dirty working directory so you can't blow away uncommitted changes accidentally and any state prior to and during the rebase is always recoverable via the reflog.

It takes trying to do anything that's not easily recoverable as long as you commit before you start messing around and don't rm -rf .git.

Re:Is Git Irreplaceable? (2019) by rohansingh on Tuesday January 07, 2020 @09:52AM (#21980393)
It's definitely not gone. Just `git checkout ORIG_HEAD` and you'll be back to whatever the tree was before you rebased.
Re:Is Git Irreplaceable? (2019) by gallier2 on Tuesday January 07, 2020 @12:58PM (#21982570)
NO. Nothing is gone after commit. You just don't see it.

Make a git log --reflog --all and you will see all the commits you made (or rebase made) in the last 3 months.

You can than rescucitate an old branch by simply putting a branch name on it with git branch newname <old sha1>

Re:Is Git Irreplaceable? (2019) by sergiosgc on Tuesday January 07, 2020 @09:56AM (#21980441)
It won't be gone until you actively prune dangling commits. The commit may not be reachable from the HEADs of existing branches, but go through the reflog and your commits will be there. You can then create a branch to make the commit reachable.
Re:Is Git Irreplaceable? (2019) by vrsfvwae5tbh on Tuesday January 07, 2020 @09:45AM (#21980321)
Does this not show up in the reflog?
Re:Is Git Irreplaceable? (2019) by gallier2 on Tuesday January 07, 2020 @12:55PM (#21982529)
You have a branch named <branchname> you do your operation that fouls up your branch. If you check with

git log --reflog --all

you will see with this magical command that git DOESN'T REMOVE any commit. Your old tree is still there, only normally hidden. The commit that was there before you fouled up your branch is still there.

You now only need to set your branch to the old commit. A branch is nothing else than a pointer in the tree.

You have 2 possiblities to change the commit a branch points to

1. git branch --force <branchname> SHA1 (works only if <branchname> is not the current checked-out branch. Simply checking out with the SHA1 works also as it deteches the HEAD).

2. replace the SHA1 in the text file in .git/refs/heads/<branchname> by the SHA1 where you want the branch to point to.

With that, your repo is in the same state it was before your error.