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



Global Config

Global config in ~/.gitconfig

email = username@domain.tld
name = james
excludesfile = /home/james/.gitignore_global

Global config via CLI:

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



List global config settings:

$git config -l

Disable HTTP SSL certificate validation in the global config file:

$git config --global http.sslverify false
$git config -l


Local Repo Config

Exclude .DS_Store files from repos on a Mac:

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



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



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


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 pull origin master
git reset --hard HEAD
git add *
git commit -m "commit to"
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 # Using git
git remote add --track master origin # Using HTTP


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

$ git remote --verbose
origin (fetch)
origin (push)
$ git remote remove origin
$ git remote --verbose
$ git remote add origin
$ git remote --verbose
origin (fetch)
origin (push)


Replace the existing remote origin:

git remote set-url origin



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

$ $git branch
* 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