Skip to content

Instantly share code, notes, and snippets.

@wamoyo
Last active October 15, 2020 03:36
Show Gist options
  • Save wamoyo/0f278d3019e506f449d6927200a7a811 to your computer and use it in GitHub Desktop.
Save wamoyo/0f278d3019e506f449d6927200a7a811 to your computer and use it in GitHub Desktop.

Git Quick Reference

Git is awesome, but a lot of the time, I just need a quick reference for which commands to use in which situations. Here Goes!


Basics

git init

This command initializes a git repository in the current directory. Hit ctrl + h to see the hidden .git folder.

vim .gitignore

This is not a git command obviously, but definitely one of the first things to do. In this hidden file just list out files and directories that git shouldn't worry about. This might include PSD files, some config files, database passwords, user interface junk, documentation, or compiled stuff. It's helpful to end directories with a / just so you can tell them apart form the files. Use an * as a catchall.

git rm --cached <file> <dir>

If you add a .gitignore file after the fact, use the above command to remove files that should be ignored. If you don't they'll stay in your repo.

git add index.html

git add styles/

git add index.html about.html styles/

git mv index.html home.html

git rm about.html

git add --all

Adds new files or modified files, directories, multiple things, rename files (with mv) or delete files (with rm), or stage everything including deletes and renames to the staging area, ready to be commited. I pretty much always use git add --all.

git commit -m "my commit message"

Commits everything you added in the previous step to the repo (the repo is a hidden folder called .git/, it's weird in there). Without the -m "my commit message", vim (or whatever code editor you prefer) opens up to edit the commit message.

git commit --amend -m "my commit message"

The --amend option overights the last commit. This is useful if you noticed a small error and need to replace your last commit, but it's a royal pain if you ever need to push your repo to a remote server, like Github, which is obviously a very common thing, so I don't recommend amending things.

Branches

git branch branchName

Create a new branch with name branchName.

git checkout branchName

Switch from current branch to branchName.

git checkout -b branchName

Create a new branch, branchName, and switch to it.

git branch -d branchName

Delete branch with name branchName.

git merge branchName

This command does a three-way merge between the two lastest snapshots of the current branch, branchName and the most recent common ancester, and creates a new commit in the current branch.

git rebase -i HEAD~3

Use this command to squash together the current commit with the 2 directly beneath it. I forget why this was important... o _ O

Checking Stuff

git status

Reports the status of your git repository, staging area, remote repos, branches and so on.

git diff

Check the difference between commits, or between commit and the working tree. By default it produces the differences between the current state and the last commit.

git branch

git branch --all

Display the local branches and denote the current branch. With the --all option it will also display remote branches.

git tree

git tree is my custom git log command, which is equivalent to: git log --all --graph --format=format:'%C(bold blue)%h%C(reset) %C(dim black)%s%C(reset)%C(bold red)%d%C(reset) %C(green)by %an, %ar%C(reset)'

This displays the full tree diagram of you git repository, and some useful info like where different branches are at the moment, how your local branches compare to remotes, who authored which commits, dates and so on.

git remote show remoteName

Displays info about the remote repository remoteName.

Remote Branches (like Github)

git remote add name url

Add remote repository at url with name for a shortname.

git fetch remoteName

This command grabs all the repository data from the remote repository. This assumes a remote repository has been added already.

git checkout -b localBranch remoteName/remoteBranch

git checkout --track remoteName/remoteBranch

Either of these commands creates and switches over to a new local branch and sets it up so it tracks a remote branch. It's probably good if the local and remote branch names are the same.

git clone

This is essentially a git init then git add remote, git fetch and git checkout. It automatically sets up a master branch tracking the remote master branch, but doesn't set up any other branches. It's very common, but it comes this late in our cheatsheet because it does so many thing automatically.

git pull remoteName localBranch:remoteBranch

This command pulls a remote branch into a local branch. A pull is a fetch and then a merge.

git push remoteName localBranch:remoteBranch

This command pushes a local branch onto a remote branch.

git push remoteName :remoteBranch

This weird looking command deletes a remote branch on the remote server... really.

If a local branch is set up to track a remote branch git push and git pull will work just like that, without specifying remotes and branches.

Stashing

git stash

Let's say you've been working for a while, and you have uncommited changes, but you want to go look at something in another branch. Doing git stash will save those changes, so you can then go checkout other branches, have a look around, make commits there, etc, and come back.

git stash apply

This command adds back the changes you saved with git stash. It can be used to apply changes to the original place you made them, but also can be used to apply those changes to another branch. Let's say you were working in your production branch, but really should have been working in the development branch. Stash the changes, and apply them in the dev branch.

git stash pop

This works like apply, but deletes the changes after applying them.

git stash list

This command will show you a list of everything stored in the stash.

git stash clear

This command will clear everything in your stash from previous stashings.

Merging

To merge one branch into another...

git merge oneBranch

...while you have anotherBranch checked out. This will do some git magic, haa, and combine the code. If you have a conflict, go manually edit the conflicts choosing which code to keep and which to ditch, and then run:

git merge --continue

To complete the merge.

Hooks

Hooks are used to run arbitray code (usually bash) when some event happens in git. For example with pre-commit or post-commit hooks, you can run code prior to committing, or just after making a commit.

You can find some samples for arbitrary code to run in .git/hooks/hook-name where hook-name is something like 'pre-commit' or 'post-commit'.

git rev-parse --abbrev-ref HEAD

It's helpful to be able to distiguish branches inside those files. The above git command will print the name of the current branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment