Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Iâve collected a treasure trove of git tricks over the years, the vast majority coming from raw necessity (and a few that were just technically neat). As Iâve started work at Hacker Noon as a Full Stack Developer, Iâve shared quite a few of these tips with the team, and Iâd like to share some of them with you.
Problem: Iâm afraid Iâm going to screw things up!
This isnât really a git problem, but Iâll answer it anyway, because git has a power tool for not screwing things up: branches. Branches are dirt cheap. Create as many as you need. If you think youâre going to screw up, branch off of your current branch, branching strategies be damned. Branching strategies are for the shared repo (Github, etc.), not your local repoâââjust donât push the branches, and youâre golden. Iâll write more about branching strategies, including the flow we use here at Hacker Noon, in a future post.
Problem: I committed to the wrong branch!
The solution to this issue very much depends on whether youâve pushed. If you have pushed, you might be screwedâââskip the next paragraph to see why.
Letâs start with the simple case. If you havenât pushed, no problem: this guide will walk you through the cherry-picking process. In a nutshell: just switch to the branch you want to commit to, and git cherry-pick $COMMIT_HASH. If you havenât pushed, thereâs a simple command you can run to delete the accidental commit. However, this command can break things. Only run it if youâre absolutely sure you can safely delete the last commit. To do this, run git resetâââhard HEAD~1 on the branch you accidentally committed to.
So, how can you be absolutely sure you can safely delete your accidental commit? If you have pushed, like I said, you might be screwed. If anyone else is working on that branch, thereâs nothing you can do. You canât just rewrite history on them, after all.
That said, if youâre really sure that it wonât mess up anyone elseâs work (e.g. youâre the only one working on a branch), you can run the reset above then use another dangerous command, a âforce pushâ, to get rid of the commit. Beware! If you force-push a branch that others are working on, youâll have different revisions on different machines, and work can be lost. This is not a good scenario.
If youâre sure, you can git push -f origin $BRANCH to force-push your branch.
Problem: I have a branch name with a task number or something weird, and Iâm sick of typing it.
The solution here is twofold: youâll want to install tab completion for git, and thereâs a neat trick for referring to the branch when pushing. For the first part, youâll want something like oh my zsh for tab completion. As for the second part, thereâs a simple shortcut I like to use. Simply git push -u origin HEAD and youâre good to go!
From then on, you can just git push and everything will âjust workâ. Please note, that -u switch sets the origin remote as the upstream source for that branch. Essentially, that just means that origin will be the default remote source for git pull and git push.
A related tip: you can use git checkoutâââto check out the last branch you were on. This is great if you need to check out a branch that a teammateâs working on, and want to switch back and forth between branches.
Problem: I found an answer to my problem on Stack Overflow, but I donât understand this funky git command.
I hate to tell you this, but: itâs time to learn git internals. But fear not! Git isnât that hard to learnâââat its core, itâs actually a simple set of commands on top of a relatively simple data structure. Itâs just a power toolâââyou can use it in crazy ways, because the authors decided to give you that ability. It was designed for a lot of different use cases, so specific use cases like cherry-picking from an accidentally-committed branch are not front-and-center.
I canât explain it nearly as well as Scott Chacon did in his book, Git Internals, which is generously available for free online. Please, read that book, and I guarantee youâll walk away with a better understanding of git.
Problem: Iâm sick of typing these long, crazy git commands to fix things.
This has a quick and useful solution: git aliases. You can configure an alias with something like git configâââglobal alias.ci commit, then you can write git ci to run git commit.
I donât know everything about git, by any stretch, but hopefully this helped. Do you have any git protips to share?
Protips: Git Your Ass to Mars was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.