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/


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 ""


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