Basic Git Commands

Basic Git commands to get you started if you are just starting to use Git for version control.

• Commands you should type into your terminal window are highlighted with grey.
Underlined text is just an example, replace with what’s relevant to what you want to do.

The Absolute Basic Commands to Get You Started

Check the current branch you’re in:
git branch

Switching branches:
git checkout master

Show changes in your branch (which haven’t been committed yet.):
git status

Pull in latest code into a shared branch
(make sure to type the correct branch name:)
git pull origin master

Pull in latest code from shared branch into a private branch to keep it up to date
git pull --rebase origin master

Committing files:
git add .

“.” = all files
You can also enter the file path for one or more specific files

git commit –m "describe change being committed"

Push to remote branch (you should usually do another pull first to check for conflicts):
git push origin master

Fixing Merge Conflicts

After pulling you may occasionally see a message warning that you have a merge conflict in one or more files.

(To fix these you can just open the problem file(s) in your code editor and so a search for “>>” and clean up the problem code then do another git add and commit.)

Revert your changes and overwrite with latest version before you merged:
git checkout file path

Get rid of all your changes (if something goes wrong and you don’t want to commit):
git checkout .

After resolving a merge conflict you can commit again normally:
git commit –m "resolved merge conflict

Working with Branches

Check which branch you are in (returns a list of all your branches with the current one marked):
git branch

Switching branches:
git checkout branch-name

Checkout a new branch which someone else has created and track it
(Make sure to enter the correct branch name or you will get an error):
git fetch
git checkout –t origin/friends-branch-name

Delete an old branch:
git branch –d branch-you-want-to-delete

Create a new duplicate branch to your current one
(”cd” to your main code directory first. Enter any name you like for your new branch.):
git checkout –b your-new-branch-name

After creating a new branch make sure to commit there.

Temporarily Setting Aside Work in Progress

So you don’t have to commit when switching branches or if you only want to commit one small thing:
git stash save "work in progress on foo feature"

You can now edit and commit some other little piece of code if you want
(or switch to another branch temporarily and then switch back):
IE: git add "blah", git commit

Go back to what you were working on:
git stash pop

Useful Utility Commands

Clean up old files so Git doesn’t start responding too slowly (run once a week or so):
git gc

Leave a comment

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s