Date created: Thursday, September 13, 2018 11:44:09 AM. Last modified: Thursday, January 11, 2024 9:30:54 AM

Git

http://ohshitgit.com/

 

Global Config

Global config in ~/.gitconfig

[user]
email = username@domain.tld
name = james
[core]
excludesfile = /home/james/.gitignore_global

Global config via CLI:

git config --global user.name "james"
git config --global user.email "username@domain.tld"
git config --global core.excludesfile ~/.gitignore_global

~/.gitignore_gobal:

.DS_Store
._.DS_Store
**/.DS_Store
**/._.DS_Store

List global config settings:

$git config -l
user.email=username@domain.tld
user.name=james
core.excludesfile=~/.gitignore_global

Disable HTTP SSL certificate validation in the global config file:

$git config --global http.sslverify false
$git config -l
user.email=username@domain.tld
user.name=james
core.excludesfile=~/.gitignore_global
http.sslverify=false

 

Local Repo Config

Exclude .DS_Store files from repos on a Mac:

echo ".DS_Store
._.DS_Store
**/.DS_Store
**/._.DS_Store" >> .gitignore  # Add them to the git ignore before committing
git add .gitignore
git commit -m 'gitignore .DS_Store'

 

Branches

Rename current branch:

git branch -m new-branch-name

Rename a branch which is not the current branch:

git branch -m existing-branch-name new-branch-name

 

Diff

Show the changes on unstaged files (files not added with "git add"):

git diff

Show the changes on staged files (files added with "git add"):

git diff --staged

Show the changes on unstaged and staged files:

git diff HEAD

Show the number of lines changes

git diff --stat

Check for merge conflict markers and whitespace errors:

git diff --check

 

Clone / Commit / Remote

Clone a specific branch:

git clone -b dev https://github.com/jwbensley/IP-Hashing.git

 

Clone with submodule recursion:

# Submodule recursion is disabled by default. One can enable it in the config:
git config -f ~/.gitconfig submodule.recurse true

# This doesn't apply to git clone, only to git pull and git fetch et al. They only way with "clone" is to explicitly specify recursion on the CLI:
git clone --recurse-submodules https://repo
# One method to enable submodule recursion by default is to use an alias:
git config --global alias.rclone "clone --recurse-submodules"
git rclone https://repo

 

Commit files in an existing directory to an existing git repo (as long as the local files don't overlap with the remote repo):

git init
git remote add origin git@github.com:jwbensley/network_graphs.git
git pull origin master
git reset --hard HEAD
git add *
git commit -m "commit to github.com"
git push origin master

 

Amend a git commit:

git commit --amend
# follow prompts to change the commit message

 

Ignore changes to a file (e.g. a file which should be present but changes must not be tracked):

git update-index --assume-unchanged a_file.txt

 

Adding a git remote origin:

git remote add origin ssh://login@IP/path/to/repository # Using SSH 
git remote add origin http://IP/path/to/repository # Using HTTP
git push -u origin master

 

Add a specific branch (e.g. master) from a remote origin:

git remote add --track master origin user@somesite.com:group/project.git # Using git
git remote add --track master origin http://github.com/group/project.git # Using HTTP

 

Replace a HTTP repo with an SSH one so that SSH keys can be used for authentication:

$ git remote --verbose
origin https://github.com/jwbensley/abc123.git (fetch)
origin https://github.com/jwbensley/abc123.git (push)
$ git remote remove origin
$ git remote --verbose
$ git remote add origin git@github.com:jwbensley/abc123.git
$ git remote --verbose
origin git@github.com:jwbensley/abc132.git (fetch)
origin git@github.com:jwbensley/abc123.git (push)

 

Replace the existing remote origin:

git remote set-url origin https://github.com/username/repo

 

Push

Push the local branch (2) to a different remote branch (3):

$ $git branch
branch1
* branch2
$ git push origin branch1:branch3

 

Rollback /  Revert / Squash

Rollback to a specific commit ID:

git reset --hard <old-commit-id>
git push -f origin main

 

Discard local commits and revert back to the state of the remote branch:

git reset --hard origin/main

 

Squash the last 5 commits:

git reset --soft HEAD~5
git commit

 

Squash back to a specific commit ID:

git reset --soft abc123def567
git commit

 

Handy Commands

Create a pretty graph of the git history:

alias gitgraph="git log --all --decorate --oneline --graph"

Previous page: Cisco Watch
Next page: Audio Size