News for hackers. Stuff that matters.
Re:Oh shit, git: Getting myself out of bad situations by dkns on Monday December 18, 2017 @10:10AM (#15952121)
> Oh shit, I accidentally committed to the wrong branch!

I find cherry-picking to be easier in this case. Just checkout the branch and cherry pick commits from 'wrong' branch.

https://git-scm.com/docs/git-cherry-pick

Re:Oh shit, git: Getting myself out of bad situations by mtrpcic on Monday December 18, 2017 @10:34AM (#15952276)
But then those commits are still in the wrong branch. If I accidentally commit something to master instead of a development branch, I can't deploy master until my development branch is merged in, as the one commit isn't ready for live.
Re:Oh shit, git: Getting myself out of bad situations by mseebach on Monday December 18, 2017 @10:46AM (#15952360)
If you haven't pushed, it's as easy as resetting the branch to the last good commit. If you have pushed, well, it's the same, then push -f, and then making sure that all your teammates pull the fixed branch. If that's a situation you regularly find yourself in, protecting master from direct pushes and only doing PRs is a good solution (GitHub, BitBucket and GitLab all have tooling for this).
Re:Oh shit, git: Getting myself out of bad situations by dkns on Monday December 18, 2017 @11:31AM (#15952813)
Maybe it's just me but I feel like if you work with multiple developers you should never, ever force push master. I'm wary of force pushing branches in general, unless that's a branch that only I work on.
Re:Oh shit, git: Getting myself out of bad situations by mseebach on Monday December 18, 2017 @11:58AM (#15953062)
It's not ideal, at all -- and if someone else has pushed in the meantime, they risk losing the commit. But if you're in a situation where doing this would be a significant problem, you should probably not be pushing directly to master in the first place, instead relying on a PR workflow.
Re:Oh shit, git: Getting myself out of bad situations by sigjuice on Monday December 18, 2017 @02:56PM (#15954876)
Why not simply push a second commit that reverts the broken commit? This will avoid rewriting history and messing up the rest of your team.
Re:Oh shit, git: Getting myself out of bad situations by mseebach on Tuesday December 19, 2017 @03:54AM (#15958738)
Because that will pollute the history and diminish its value as a record of what happened in the code base. For instance, blame will no longer tell you when a line was changed, but rather point to the revert commit. Bisect breaks if it divides through the reverted commits.

Of course, rewriting history is only feasible immediately after the mistaken commits, before anyone builds on top of them. If they've lingered, reverting is the right way.

And again, if this is a repeating problem, fix it upstream (no pushing to master, only PRs).

Re:Oh shit, git: Getting myself out of bad situations by Cthulhu_ on Monday December 18, 2017 @10:59AM (#15952501)
That's where tools like github and gitlab come in (or a pre-receive hook on the server), which can deny all commits to master. If that's something you want to prevent of course - and tbf, as soon as there's more than one or two people working on a project, I'd lock master down.
Re:Oh shit, git: Getting myself out of bad situations by yes_or_gnome on Monday December 18, 2017 @05:15PM (#15956005)
I don't see how that's easier. There's `git checkout -b my-new-branch` which is even easier than the originally proposed solution. Followed by `git checkout master && git reset --hard @{u}` or, if you don't want to switch between branches, `git branch -f master master@{u}`.
Re:Oh shit, git: Getting myself out of bad situations by nemetroid on Monday December 18, 2017 @05:02PM (#15955923)
I agree that the proposed solution is not the best. If you can fix your problem without dirtying the working tree (i.e. by simply moving changesets around), that's almost always a nicer way to do it.