Skip to content

git cheat sheet

Martin Shetty edited this page May 4, 2021 · 1 revision

While git is a complex tool, you can get by using only a few command.

Assuming that you are disciplined about:

  • working on only one feature at a time
  • working on a personal branch that no one else will commit to
  • do not keep draft/trash/unrelated files around in your repository folders

The following commands should suffice:

Command Notes
git status Do this often, pretty much in between running any other commands. It will not tell you everything, but if something goes wrong, it will tell you useful things. In the case you need support from another team member, it will be good to have a leger of status reports to help figure out what happened.
git pull Updates your local repository copy to reflect the latest on the GitHub server. Do this first thing in the morning and every time you switch branches. Do this often, though maybe not quite as often as git status. This command will also attempt to merge any server-side changes into your local copy, so if you have been double-teaming a branch, you may have conflicts to resolve. That's why it's better to keep unrelated work well isolated.
git checkout branch_name Switches to different branch. Probably a good idea to have closed, saved and committed changes on whatever other branch you have been working on before switching.
git checkout -b new_branch_name Creates new branch and switched to it. Make sure you are already in a branch that you want to branch off of. Make sure you have done a git pull so that your starting point is as up-to-date as possible. This only creates the branch locally, so you will want to do a git push to make it appear server-side.
git push Pushes all local changes to the server. If the branch does not yet exist on the server, it will bitch at you. But it will also kindly provide the command that you should run instead. You can just copy-paste it and try again. Otherwise, the changes that it pushes are only those that have been "committed". More on this below.
git add -A Add all untracked changes to the next commit. Any newly created files will be added. Any deleted files will be removed. Any changed files will be registered. Do git status before you do this to make sure you are not about to bring in any junk. If there are file types that are to be systematically ignored, you may want to update the .gitignore file at the root of the repository.
git commit -m "this is what I did; updates #123" Creates a commit (package of changes) with a message providing a helpful summary. Again, make sure you run git status prior to this to make sure everything is "in the green" and if not, run git add again to include any untracked changes. The tacked on update note with the hashtag will allow GitHub to link this commit to the relevant ticket being addressed. Now is also a good time to do a git push so that your colleagues can enjoy the fruits of your labor and celebrate your achievements.
git rebase -i master Rewrites the history of your current branch in relation to the most recent master. This is better than straight up merging changes. Do this before you create a pull request or any time you need to adopt recently merged changes for whatever reason. Rebasing is a really big deal. Read more about this here.

This is by no means a comprehensive list of commands that you may need, but if used prudently should suffice for 99% of everything you do.

Clone this wiki locally