11 minutes
Git Commandline Reference
November 3, 2017
This page is a cheat sheet which has a list of commands to do various things in git from the command line. I will start adding more activities here as I have to do them. This won’t replace an actual tutorial but once you have an idea of how Git works it will help you figure out how to do common things.
How to create a repository
To create a new repository do the following:
-
Either create a new empty folder or change to a folder you want to put under source control.
-
Run the following:
git init
This will then create a new source repository.
How to commit code.
Once you have a repository you will want to check files into it. This is a 2 part process.
Add files to index:
Before you can commit anything to the repository you need to add files to the index. You can do this with the following command:
git add (path to file or folder)
This will add the target file or folder (including all sub files and folders) to the index.
If you want to add everything in the repository simply run
git add .
You can view the files that are currently in the index or files that have not been added by running the following command:
git status
If you messed up the index and want to start again use the following:
git reset
Committing files:
Once you have all the files you want to commit in the index run the following command:
git commit –m "meaningful message"
Replace meaningful message with an explanation of what changes you have made.
If you want to view all of the commits you have made use the following command:
git log
How to create a .gitignore file
It would get really tedious if you have to manually select which files you want to commit and which ones you don’t want to commit. So git supports the create of .gitignore files to filter which files it will automatically to the index.
This is really convenient because running git add .
Will ignore the files specified.
Typically files you would ignore are temporary files created by your build tools and artefacts created like exe and dll files.
You can also add extra .gitignore files to sub folders if you need to have ignore rules only apply at certain levels of the tree.
An ignore file is simply a bunch of regular expressions that target specific files or folders in the tree structure.
If you would like to see some examples of .gitignore files GitHub has a huge list of them in this repository: https://github.com/github/gitignore
How to check code into GitHub or bit bucket When you create a new repository on GitHub or bitbucket they will give you a bunch of commands to run to import your code.
The main commands you will want to look at is the following:
git remote add origin https://github.com/username/reponame.git
git push -u origin master
How to remove accidental committed files
If you have accidentally checked a file into git and want to remove it you can do the following:
git rm <path to file>
This will remove it from the repository and stop tracking it. If you commit changes now the file will no longer be in the repository.
However this will not remove historic entries of the file. If you want to remove it from previous commits see the Rebase section below.
How to push and pull changes from remote While it is all good to commit changes to your local repository chances are you want to push your changes up to GitHub or bit bucket. You can do this by doing the following:
To receive new changes from remote server:
git pull
To push changes to remote server
git push
A note on pushing and pulling. You usually need to do a pull before you can push your changes. This is to get any new changes anyone else has been since you last pushed.
How to check out old version: Say you want to look at code from a previous version. You can check that commit out by doing the following:
-
Identify the commit you want to look at by using the following command:
git log
Look at each commits hash which will look something like this 4b1628b2ca421af58a2f113545806787bb29cf35.
-
Now check that commit out by running the following command:
git checkout 4b1628b2ca421af58a2f113545806787bb29cf35
Replace the hash with your hash and it will roll back to that version.
Note: Thing to be aware of. You can’t simply make changes and commit it expecting for it to merge back in. If you want to make changes to past commits I would recommend looking at rebasing. Alternatively you can create a new branch and continue working on this commit from there. For both options there is more details on how to do it in their respective sections below.
Tip: If you need to refer to old code while you are working on new code you can clone the repository into another directory which lets you keep the previous commit as a reference.
How to show diff of files between versions:
If you want to view the difference between a file in 2 different commits do the following:
-
Identify the commit you want to look at by using the following command:
git log
Look at each commits hash which will look something like this 4b1628b2ca421af58a2f113545806787bb29cf35.
-
Now run the following command:
git diff 4b1628b2ca421af58a2f113545806787bb29cf35 filename/to/diff
Replace hash with your hash.
You will now see details of what changed between the two versions.
How to revert to an old file:
If you want to revert the contents of a file back to a previous commit do the following:
-
Identify the commit you want to look at by using the following command:
git log
Look at each commits hash which will look something like this 4b1628b2ca421af58a2f113545806787bb29cf35.
-
Execute the following command:
git checkout 4b1628b2ca421af58a2f113545806787bb29cf35 name/to/revert
How to append to last commit
If you left something out of your last commit and would like to append changes to it rather than creating a new one and you haven’t pushed it to a remote yet you can do so with the following commands:
Add your changes to the index.
git add .
Amend the commit.
git commit -amend
How to clean working directory
Say you want to reset the working directory back to the last commit and wipe all changes you have a few options for doing so.
Clean all untracked files including ignored files. This is handy for cleaning the repository back to an initial state like when it was first cloned from GitHub.
git clean –fxd
If you only want to remove ignored files change the X to an uppercase like so:
git clean –fXd
There are a few other options worth looking at too:
-e will let you exclude files from the clean. -n will run in dry run mode which simulates cleaning and not actually do it. -i for interactive mode where you can specify which files to delete one by one.
Branching
You can create branches to work on code in parallel. There are lots of uses for branches.
You can check which branch your currently on by running:
git branch
To create a branch simply run:
git branch <branch name>
To list all branches including remote branches run:
git branch --all
To change which branch you are currently on run the following:
git checkout <branch name>
To remove a branch run the following:
git branch –D <branch name>
To remove a remote branch run the following:
git push origin --delete <branch name>
To use a remote branch simply check it out with:
git checkout <branch name>
To push a locally created branch remotely run the following (replace origin with your remote name if its not origin):
git push --set-upstream origin <branch name>
Stash
The stash is a system where you can save your current folder state ready to be picked up later. A typical use case is you are working on a branch and need to switch to another branch to check something. Your current state is not ready to commit because it is half complete.
So what you can do is stash it for use later.
To stash the current state run:
git stash
Now you can change branch or do anything else you wanted.
To restore the most recently stashed contented run:
git stash apply
If you have multiple stashed items and want to see them all run:
git stash list
Then to restore the desired stash you just need to run (replacing stash@{0} with the index you want to restore):
git stash reply stash@{0}
Merging branches
Merging branches as its name suggests is getting the content of one branch and applying it into another. One use case for this is to take all the changes which have happened on the master branch and bring them into a feature branch bringing it up to date. Or in the opposite direction applying the contents of a feature branch on to master.
To marge a branch simply do the following:
-
Check out the branch you want to merge the other branch into.
-
Then run:
git merge <branch name>
How to rebase
Rebasing is a complex subject but in the context of this article I am going to explain how you can use it to merge multiple commits together and how to edit and exclude them.
Before you do anything with rebase however you need to follow the golden rule. DO NOT REBASE PUBLIC COMMITS! If you do and people are working on those commits you have just stopped them from committing anything. This is because rebase will create new commits.
The easiest way to do a rebase is in interactive mode which will ask the user what they want to do with each commit.
git rebase -I <branch name>
This will then bring up vim and let you select what to do with each commit:
pick abed851 Even more typos
pick 08bfc38 Fixed some more typos
pick 91ac5b5 Added comments from boss 3
pick fb88180 Added comments from boss 2
pick 4469b96 Added comments from boss 1
pick 2f47290 Added cover page to TPS report
pick bf78d75 Fixed typo in TPS report
pick 7f214d8 Created TPS report
# Rebase 2a58c0f..7f214d8 onto 2a58c0f (8 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
To edit content press I
to go into insert mode. Then press esc
when you are finished editing.
To save changes type :wq
and press enter.
As per the descriptions above we are mainly interested in the following options:
-
drop – Will remove a commit as mentioned above.
-
fixup and squash – These commits will merge commits together, we will want to use this a lot. Difference between the two is fixup won’t update the commit message whereas squash will.
-
edit – This option is extremely useful. It lets you edit a commit, here you can add or remove files or even modify them. Once you have finished modifying everything you can continue the rebase process with the following command:
git rebase --continue
-
Pick and reword – These options will keep the commit. The difference being reword lets you change the commit message.
How to split a commit
If you have ever created a commit which has lots of different unrelated things in it and would like to split it out to make it clearer you can do so. To do so you do a rebase like above however instead of running it against the whole branch you can run it against the commit like the following:
git rebase -i 4b1628b2ca421af58a2f113545806787bb29cf35
Once this runs change the single commit listed to edit like above.
Now you are back in the commit run the following command:
git reset HEAD^
This will rewind the head by one which leaves the files the same in the directory but not committed.
From here you can use git add to add the items you want in the current commit and then commit them like usual and then repeat for other commits you want to add them to. E.g.
git add foo.txt
git commit –m "only foo.txt"
git add bar.txt
git commit –m "only bar.txt"
Then when you are finished you can run:
git rebase --continue
Which will continue and complete the rebase like usual.
How to rename a branch
If you need to rename a local branch you can do so with the following:
git checkout oldbranchname
git branch -m newbranchname
If you need to rename the branch remotely you can do the following:
git push origin :oldbranchname newbranchname
The above will delete the old branch from the remote and add the branch back again with the new name. This means if you have any outstanding pull requests it will break them.