News for hackers. Stuff that matters.
Re:Some bad Git situations and how I got myself out of them by mcv on Friday September 09, 2016 @03:39AM (#12460273)
I think the biggest problem people have with git is that you can't and shouldn't want to change shared history, and yet git provides some tools that suggest that maybe you can.

It's better to accept history as it is, and fix the problem with reverts, cherry-picking and new commits. History won't be as pretty or clean, but it will reflect what actually happened, which is what history is, after all.

The biggest trick to understanding git is to learn to think in commits rather than file content. If one branch has a commit and the other branch doesn't, merging them will mean that the commit is there. If one branch has the commit while the other branch has the commit and a revert for that commit, merging will mean it will be reverted.

Same goes with pushing. If you reset locally to before a commit that you already shared, that commit returns on your next pull. If you revert it, it will stay reverted.

The most dangerous thing git can do, is rebasing. Rebase changes history. This is fine if it hasn't been shared yet (you commited a new change and try to push, but remote changes need to be pulled first, so rebasing is fine). Rebasing is not fine if the commit has already been shared, through a different branch or a different remote repository, for example. In that case, you need a merge.

As pretty as rebase can make your history, you should really only use it when you understand what it does. If you don't, stick with merge.

Re:Some bad Git situations and how I got myself out of them by l0b0 on Friday September 09, 2016 @04:57AM (#12460539)
`git rebase -i` by default rebases only back to the first commit you haven't shared (at least that's how I interpret the actual behaviour). This is a great default - it's the simplest way to change history and at the same time powerful without being too dangerous.
Re:Some bad Git situations and how I got myself out of them by ZeroGravitas on Friday September 09, 2016 @05:34AM (#12460670)
Ah, interesting. I'd noticed this behaviour but hadn't conciously thought about it.

The docs say:

All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area. This is the same set of commits that would be shown by git log <upstream>..HEAD

Re:Some bad Git situations and how I got myself out of them by mcv on Friday September 09, 2016 @07:33AM (#12461171)
If they're not in upstream but they are in another branch, it's still dangerous.
Re:Some bad Git situations and how I got myself out of them by majewsky on Friday September 09, 2016 @08:34AM (#12461526)
Well yeah, sometimes this power is needed. For example, I might need to do a rebase over published history to remove a patch that was found to be a copyright infringement.
Re:Some bad Git situations and how I got myself out of them by phaemon on Friday September 09, 2016 @09:50AM (#12462145)
I don't understand. Could you give an example?
Re:Some bad Git situations and how I got myself out of them by skissane on Friday September 09, 2016 @05:31AM (#12460652)
I think the ability to edit history is one of the great advantages of Git. I encourage people to use it rather than ignore it.

Cleaner history makes life much easier for those who will try to understand what you did weeks or months or even years later (including your future self).

Of course, you shouldn't (with rare exceptions) edit history you have already shared with others; but, I never found that concept, or the way git implements it, particularly hard to understand. (I will admit some of git's commands are hard to remember - I am always forgetting the difference between "git reset --hard" and "git reset --soft".)

Re:Some bad Git situations and how I got myself out of them by tech2 on Friday September 09, 2016 @04:39AM (#12460483)
> but it will reflect what actually happened, which is what history is, after all.

Recorded history is not necessarily what actually happened though, often (or always, according to some) it's an interpretation.

"History is written by the victors" - Walter Benjamin?

Re:Some bad Git situations and how I got myself out of them by falsedan on Friday September 09, 2016 @01:48PM (#12464486)
> I think the biggest problem people have with git is that you can't and shouldn't want to change shared history, and yet git provides some tools that suggest that maybe you can.

Funny, I think the biggest problem is people who refuse to understand the caveats of rewriting shared history and spread FUD about it.

Re:Some bad Git situations and how I got myself out of them by Ntrails on Friday September 09, 2016 @10:24AM (#12462480)
> If one branch has a commit and the other branch doesn't, merging them will mean that the commit is there. If one branch has the commit while the other branch has the commit and a revert for that commit, merging will mean it will be reverted.

See, I fundamentally disagree that this is what I wanted. When I merge my thing with change x into head I expect to see my changes actually go in. Not to see commits retroactively added to head. The merge was the action, not some weird rewriting of historical order of events.

> The biggest trick to understanding git is to learn to think in commits rather than file content.

Ok, I'm starting to see what you meant. (I didn't understand this until the second part and my being confused as heck)