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!
This command initializes a git repository in the current directory. Hit ctrl + h to see the hidden .git folder.
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.
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.
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
.
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.
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.
Create a new branch with name branchName.
Switch from current branch to branchName.
Create a new branch, branchName, and switch to it.
Delete branch with name 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.
Use this command to squash together the current commit with the 2 directly beneath it. I forget why this was important... o _ O
Reports the status of your git repository, staging area, remote repos, branches and so on.
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.
Display the local branches and denote the current branch. With the --all option it will also display remote branches.
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.
Displays info about the remote repository remoteName.
Add remote repository at url
with name
for a shortname.
This command grabs all the repository data from the remote repository. This assumes a remote repository has been added already.
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.
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.
This command pulls a remote branch into a local branch. A pull is a fetch and then a merge.
This command pushes a local branch onto a remote branch.
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.
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.
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.
This works like apply, but deletes the changes after applying them.
This command will show you a list of everything stored in the stash.
This command will clear everything in your stash from previous stashings.
To merge one branch into another...
...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:
To complete the merge.
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'.
It's helpful to be able to distiguish branches inside those files. The above git command will print the name of the current branch.