Git

From Bonus Bits
Jump to: navigation, search

Description

This article gives information on Git syntax and operations.


Notes

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

Username

git config --global user.name "Your Name Here"
Example
git config --global user.name "John Doe"

User Email

git config --global user.email "Email Address"
Example
git config --global user.email "john.doe@domain.com"

Set Default Editor

Set your favorite default editor.

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


Set Certificate Authorities

Ubuntu

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

Add new files or folders

git add ~/git/repos/


Commit

Commit Changes to the Master

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


Push

Push Commits to Upstream Git Repositories

git push ~/git/repos/


Push Local to Github

  1. Create New Repo (Optional)
    git remote add origin https://github.com/username/project_name.git
    
  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

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


Branches

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>
Example
git branch add_feature_xyz

Switch to Local Branch

git checkout <branch name>
Example
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>
Example
git push -u origin add_feature_xyz

Compare Branches

  • Show file syntax differences
    git diff <branch name1>..<branch name2>
    
    Example
    git diff master..xyz_fork
    
  • Show only file differences
    git diff --name-status <branch name1>..<branch name2>
    
    Example
    git diff --name-status master..xyz_fork
    
  • Show commit logs
    git log <branch name1>..<branch name2>
    
    Example
    git log master..xyz_fork
    
  • Show commit logs
    git shortlog <branch name1>..<branch name2>
    
    Example
    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

Tags

List Local Tags

git tag -l

OR

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

Mergetool

First you can try an automated way

git mergetool

Manual

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 "https://github.com/orgname/reponame.git"
    


Submodules

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
OR
git pull --recurse-submodules

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

git config fetch.recurseSubmodules yes

Related Articles


Sources