Git Commands Every Developer Should Know
The handful of git commands you'll use every single day.
When you start using Git it can feel like there are hundreds of commands to learn. The good news is you'll only use a small handful of them day to day. Here are the ones I use the most, in roughly the order you'll need them.
Starting a Repo
Use git init to turn the current folder into a Git repo, or git clone to
download an existing one:
git initgit clone https://github.com/username/my-project.git
Checking What Changed
git status shows which files are changed, staged or untracked. Run it often.
git status
git diff shows the actual line changes that aren't staged yet. Add --staged
to see the changes you've already added:
git diffgit diff --staged
Saving Your Work
Saving is two steps. First you stage the changes with git add, then you save
them with git commit:
git add index.js # stage one filegit add . # stage everything in the current foldergit commit -m "add the signup form"
Keep commit messages short and describe what the commit does.
Looking at History
git log lists past commits. The --oneline flag keeps it to one line each,
which is much easier to scan:
git log --oneline# 3f2a1c9 add the signup form# 9b8e7d2 set up the project
Working with Branches
Branches let you work on something without touching master. Create a branch
and switch to it in one step with checkout -b:
git branch # list branchesgit checkout -b signup-form # create and switch to a new branchgit checkout master # switch back to master
When the work is done, merge it back in from master:
git checkout mastergit merge signup-formgit branch -d signup-form # delete the branch once it's merged
Syncing with a Remote
git push sends your commits up to GitHub (or wherever your remote is), and
git pull brings down everyone else's:
git push -u origin signup-form # first push of a new branchgit push # after that, this is all you needgit pull
The -u flag tells Git to remember where this branch lives, so next time a
plain git push or git pull knows where to go.
Setting Work Aside
git stash saves your uncommitted changes and gives you a clean working
directory. This is handy when you need to quickly switch branches:
git stashgit checkout master# ...fix something...git checkout signup-formgit stash pop # bring your changes back
That covers most of what I do in Git day to day. Anything else I look up when I need it.
Have some feedback on this post? .