From Bonus Bits
Jump to: navigation, search


This article gives information on Git syntax and operations.


Examples will use the following paths. They can be whatever you like.

  • ~/git/repos/
    mdkir -p ~/git/repos

Initial Setup

Configure Proxy

Configure Proxy for Git

Set Git Client Global or Local Project Settings

List Git Settings

git config --list


git config --global "Your Name Here"
git config --global "John Doe"

User Email

git config --global "Email Address"
git config --global ""

Set Default Editor

Set your favorite default editor.

git config --global core.editor <editor name>
git config --global core.editor vim

Set Certificate Authorities


sudo apt-get install ca-certificates
git config --global http.sslVerify true
git config --global http.sslCAinfo /etc/ssl/certs/ca-certificates.crt

Setup Local Repository

Initialize New

mdkir -p ~/git/repos/firstrepo
git init ~/git/repos/firstrepo

Create Local Repo

git init ~/git/repos/myproject

Manage Local Repository

Clone (Download) Remote

All Branches

git clone <URL> ~/git/repos/

Single / Master Branch Only

git clone -b master --single-branch <URL>


Add new files or folders

git add ~/git/repos/


Commit Changes to the Master

git commit -m 'blah blah' ~/git/repos/


  • To pull the latest changes for the all of the branches locally from the remote (github)
    git pull
  • To pull the latest changes for the current branch you have checked out locally from the remote (github)
    git pull origin master


Push Commits to Upstream Git Repositories

git push ~/git/repos/

Push Local to Github

  1. Create New Repo (Optional)
    git remote add origin
  2. Push to remote master branch
    git push -u origin master

Push New Local Branch to Remote

If we have created a local branch that is not yet been pushed to the remote repo, then we need to specific creating the upstream origin.

git push --set-upstream origin <branch name>


Stash can be useful if you make a bunch of changes in the wrong branch and want to move them to another branch.

  1. Stash changes
    git stash
  2. Switch to Correct Branch
    git checkout <branchname>
  3. Unstash Changes to Correct Branch
    git stash pop

Mark File Executable

By default on a Linux/UNIX system if you set the file permissions with chmod +x it should automatically update the git index of the file. If it doesn't or sometimes files pulled down to Windows don't get the correct permissions you can manually set the index by doing the following.

git update-index --chmod=+x

Remove Added Files/Directory Keep Local Copy

This is good for when something gets added by mistake that you want to keep local, but not push to remote. Such as, if you accidently add the .idea folder.

git rm -rf --cached .idea


List Local Branches

git branch

List Remote Branches

git branch -r

List All Branches

git branch -a

Remove Local Branches

git branch --delete <branchename>

Remove Remote Branches

git push -D origin <branchename>

Create New Local Branch

git branch <branch name>
git branch add_feature_xyz

Switch to Local Branch

git checkout <branch name>
git checkout add_feature_xyz

Commit Specific Branch

Switch to the branch and commit like normal.

git commit -m 'comments'

Push Branch

git push -u origin <branch name>
git push -u origin add_feature_xyz

Compare Branches

  • Show file syntax differences
    git diff <branch name1>..<branch name2>
    git diff master..xyz_fork
  • Show only file differences
    git diff --name-status <branch name1>..<branch name2>
    git diff --name-status master..xyz_fork
  • Show commit logs
    git log <branch name1>..<branch name2>
    git log master..xyz_fork
  • Show commit logs
    git shortlog <branch name1>..<branch name2>
    git shortlog master..xyz_fork

Update Local Branch with Master or Other Branch

As you work on a feature branch it's always a good idea to constantly pull any changes from master to avoid conflicts during pull request.

git pull origin <branch name>
git pull origin master


List Local Tags

git tag -l


git tag

Switch to Tag

git checkout <tag_name>

Delete Tag

  1. Delete latest Local
git tag -d latest

Commit Log

git log has many options to show the full commit history of the current local checkout. Here are a few useful examples.

List Current Short Commit Hash

List the current short hash for the current checkout you are on.

git log --pretty=format:'%h' -n 1

List Current Long Commit Hash

List the current short hash for the current checkout you are on.

git log --pretty=oneline -n 1 | cut --delimiter ' ' --fields 1

Resolve Conflicts


First you can try an automated way

git mergetool


If the mergetool doesn't work or would rather use manual way with editor etc.

  1. Find the conflicts and fix
  2. Commit you fixes to your working branch
  3. Attempt to merge/pull again

Change Origin URL

Say you want to rename a repository and keep the local work you have. You can change the Remote Origin URL of your local clone to match the new name.

  1. Change to the local repo directory
    cd /path/to/repo
  2. List current origin
    git config --get remote.origin.url
  3. Change origin
    git config --local remote.origin.url ""


Fork a Project

Forking is the standard way to contribute to github projects. This is how you can contribute without the need for permissions to a repository.

  1. Clone a copy of a github project to your own account.
    git clone
    Browse to the project in a web browser and click the fork button and select your user or organization you wish to clone it to

Configure Upstream

To update your copy of the project with the latest updates from the original project you first need to configure the local git project upstream.

  1. Change directories to root of the forked clone locally
  2. Add Remote upstream to git config
    git remote add upstream <git clone URL>
  3. Fetch original project branches
    git fetch upstream

Sync Local Fork with Upstream

With the upstream configured you simply can do a pull to get the original project's updates.

  1. git pull upstream
    git pull upstream master

Push Fork to Your Remote

After you finish your contributions and are ready to send to the original project. First you push your local changes to your remote. Then create a Pull Request (PR). The PR will show up to the owner/s of the original repository. They then have to decide when and if to merge your changes into their repository.

  1. Commit changes locally
    git commit -m 'blah blah blah'
  2. Push local changes to your remote (github)
    git push origin master
  3. Use the Github web interface and create a Pull Request on your Fork


Add Another Git Repo to a Repo

To add another repo into your repo you can use submodule. Such as, a shared Chef cookbook repo that needs to be nested in a folder structure inside your repo for a continuous integration service to pull.

git submodule add <git clone URL>

Include Submodules in Clone

If a repo has submodules in it you must use the recurse switch to clone all the submodules locally as well.

git clone --recursive <git clone URL>
OR if need to add the submodules after already pulling down the master git repo try this in the root of the local repo (There should be a .gitsubmodule or .gitmodules file)
git submodule update --init --recursive

Pull Updates to All Submodules

From the root where the .gitmodule file is run the following.

git submodule foreach git pull origin master
git pull --recurse-submodules

Icon-Tip-Square-Green.png This git configuration can be useful:

git config fetch.recurseSubmodules yes

Related Articles