If you're learning software development, Git is one of the first tools you should build a working knowledge in. It's necessary for any job in today's market.
You can go from zero to working knowledge very quickly, however. We'll start there, then dive into some more advanced commands that took me way too long to understand.
Let's dive in!
What's Git?
Git is a tool designed to make collaboration across multiple machines easier. Typically, developers use GitHub to store their code in repositories. It's also frequently used for documentation, blogging, etc.
Zero to a working knowledge
Throughout development, I only use a handful of commands daily. Everything else can be googled as long as you remember the concepts exist. Don't worry about memorizing specific syntax.
These are the commands I use regularly, with a short explanation of each:
git branch <branch_name>
- Create a new local branch with the supplied branch_name.
git checkout <branch_name>
- Change your current branch to the local branch at the supplied branch_name.
git switch <branch_name>
- Similar to checkout, but works for remote branches as well as local branches.
git add <directory>
- Add the changes in the path supplied to the index from the working tree.
git commit -m "message"
- Commit all staged changes into your branch
git push
- Push your code up to the remote branch
git pull
- Pull down changes from the remote branch that aren't on your machine yet
git merge <branch_name>
- Merge supplied branch_name into current branch
That's really about it. You can make a GitHub account for free and practice these by making a repository and using branches, commits, and pull requests. Of course, each of these commands has loads of intricacies and options, and if you run into any issues the Git docs are a great resource
There are more complicated topics like rebasing when you need to squash your commits and using aliases, but that doesn't come up every day. If you do run into issues like these, keep reading below!
Interested in leveling up?
These are all tips and tricks I wish someone would've told me when I very first started learning git. I've found them handy in my career, but they don't necessarily come up every single day. Again, memorizing syntax isn't necessary. Knowing the concepts exist is more than enough to google for specific syntax later.
Without further adieu, let's begin!
"-"
In Git we can use "-" as shorthand for "revert to the previous branch".
For example:
# Starting from branch 'main'
git branch new-awesome-branch
git checkout new-awesome-branch
# Switched to branch 'new-awesome-branch'
git checkout -
# Switched to branch 'main'
It is technically shorthand for @{-1}
, which does the same thing. For the curious reader, yes, you can change that -1
value to any integer and move up or down the tree of checkouts.
This works to a degree in Unix as well, with commands like cd -
.
Aliases
Aliases are a way to shorthand your frequently used git commands. Think of it like setting up variables.
This is going to be a short tutorial in itself, but the effects will permanently increase the speed of your ability to maneuver through your workflow.
First, open up your Git config file. There's a local file for keeping config a per-project basis, and there's a global file on a per-user basis. We're sticking with global config for now.
Run the following:
git config --global -e
This will open the Git config file in your default text editor
Now add an alias section like below:
[alias]
alias-name = git command "123"
Save and close your configuration file, and your alias will exist.
In this example, you can input git alias-name
to your terminal and it will run git command "123"
. As you can imagine, this extends out to other commands as well.
Here are some common git aliases:
[alias]
st = status
co = checkout
br = branch
cm = commit
df = diff
lg = log --oneline --graph --all --decorate
Just to be clear, this would make running git st
always run git status
. As you can imagine, these will save you some time in the long run. Plus you'll look cooler while you're screen sharing during pair programming.
Interactive rebase
Earlier in the article I mentioned squashing down your commits. This is frequently done before every merge back into the main branch from your pull requests on large teams.
There are many ways to handle rebasing, I use a .zsh script for it instead of manually rebasing every merge, but knowing how to rebase is super useful! Here are the basics
git rebase -i HEAD~4
This will interactively rebase the last 4 commits.
-i
stands for interactive, and HEAD~4
means "rebase the last 4 commits from the HEAD commit".
Running the above will open up an editor with something like this displaying:
pick 1fc6c95 start the thing
pick 6b2481b fix the bug
pick dd1475d write the tests
pick ai9j31f rubocop
Each line represents a commit, and pick
means that we're choosing to keep the commit. You can replace pick
with the following commands:
reword
- Keep the commit, but change the commit message.edit
- Keep the commit, but make additional changes.squash
- Combine this commit with the one before it.fixup
- Like squash, but discard this commit's log message.drop
- Delete the commit.
Save and close the file, and Git will begin making the changes you specified.
If you chose commands reword
, edit
, or squash
, Git will pause on these steps to allow you to make changes.
In case of conflict, Git will make you aware. Then you can change your files to fix the conflict, run git add <file_path>
and continue the rebase.
If anything goes awry, you can always use git rebase --abort
to cancel the abort like none of this ever happened. Been there, done that.
Final notes
I've been using Git for years now and these are the most popular commands I use, plus a few extras that come up from time to time. Hopefully, you find it to be a handy reference guide.
Did I miss any? Leave a comment and let me know!
Thanks for reading!