Skip to content

Git

Config

Disable SSL verification in global config

git config --global http.sslVerify false

Disable SSL verification for a single repo

git -c http.sslVerify=false clone <repo>

Then from the root of the repo (where the .git folder resides)

git config http.sslVerify false

Cater for different users using git as user 'root'

In root's /.kshrc

SUDO_USER="${SUDO_USER:-'none'}"
if [ ${SUDO_USER} = "johndoe" ]; then
    export GIT_AUTHOR_NAME="John Doe"
    export GIT_COMMITTER_NAME="John Doe"
    export GIT_AUTHOR_EMAIL="[email protected]"
    export GIT_COMMITTER_EMAIL="[email protected]"
elif [ ${SUDO_USER} = "janedoe" ]; then
    export GIT_AUTHOR_NAME="Jane Doe"
    export GIT_COMMITTER_NAME="Jane Doe"
    export GIT_AUTHOR_EMAIL="[email protected]"
    export GIT_COMMITTER_EMAIL="[email protected]"
fi

Remotes

Changing a remote's URL (https or ssh)

$ git remote -v
origin  https://github.com/Kristijan/notes.git (fetch)
origin  https://github.com/Kristijan/notes.git (push)
$ git remote set-url origin https://github.com/somethingelse/notes.git
$ git remote -v
origin  https://github.com/somethingelse/notes.git (fetch)
origin  https://github.com/somethingelse/notes.git (push)

Add remote upstream after fork

$ git remote -v
origin  ssh://[email protected]:7999/~kristijan/compliance.git (fetch)
origin  ssh://[email protected]:7999/~kristijan/compliance.git (push)
$ git remote add upstream ssh://[email protected]:7999/unix/compliance.git
$ git remote -v
origin  ssh://[email protected]:7999/~kristijan/compliance.git (fetch)
origin  ssh://[email protected]:7999/~kristijan/compliance.git (push)
upstream        ssh://[email protected]:7999/unix/compliance.git (fetch)
upstream        ssh://[email protected]:7999/unix/compliance.git (push)

Pull down changes from both upstream and origin

git remote update

Aliases

Decorated oneline graph

git config --global alias.lga "log --oneline --all --decorate --graph"

One line summary

git config --global alias.lgo "log --color --pretty=format:'%C(yellow)%h%Creset - %C(cyan)%ad%Creset %<(80,trunc)%s   %C(cyan)%>(20,trunc)%an - %Cgreen%>(12)%ar%Creset' --date=short --abbrev-commit --all"

Commits

Automatically commit all files being tracked

git commit -a -m "commit message"

Squashing commits and rebase onto master

$ git switch master
$ git pull
$ git switch <other_branch>
$ git rebase -i master
pick dedd08d commit1
squash 8e782a4 commit2
squash 036bca2 commit3
squash ff8a125 commit4
$ git push --force-with-lease

Find parent commits of a merge

$ git log --format=raw -n1
commit 61713c9ac641091c20045e9d3bde34f31d8e3621
tree 9d8c97f166cc9082c1949f21ed7b61a4fc33a420
parent c507b242517ba651b4fae5254c607571f666ffe2
parent a12033cb2e0933849a374defd272bd71a06b62ed

The highlighted commit above should be where the branch was before the merge.

Create empty commit

Create an empty commit to do things like trigger builds, etc...

git commit --allow-empty -m "Trigger build"

Branches

Create new branch and switch

git switch -c <new-branch>

Create new branch from another branch and switch

git switch -c <new-branch> <start-point>

Switch back and forth between two different branches

git switch -

Update a branch with changes from master

git fetch origin master
git rebase origin/master

Reset local branch to match remote

git fetch origin
git reset --hard origin/master

Rename a branch

git branch -m <new_name>

Rename branch if already pushed to remote

git branch -m <new_name>
git push -u origin <new_name>
git push origin --delete <old_name>

Delete a branch

git branch -d <branch>

Delete a remote branch

git push origin -d <branch>

List all branches and the commit they point to

git branch -av

Delete local remote tracking branch if the remote branch has been merged

git checkout master
git remote update origin --prune
git branch --merged | grep -v master | xargs git branch -d

Logs

Condensed summary

git log --summary

Decorated graph

git log --oneline --all --decorate --graph

Details of a specific commit

git show <commit> [-s]

Diff

Diff from staged files

git diff --staged

Show committed files to be pushed to remote

git diff --stat --cached <remote/branch>

Diff between local and remote file

git diff origin/branchname -- location/of/file.txt

Diff between two branches

git diff <branch1>..<branch2>

Diff between file in two different branches

git diff <branch1>..<branch2> -- location/of/file.txt

Diff of filenames only between two commits

git diff --name-only 9e9384392 61713c9ac

Diff of filenames and their modifications between two commits

git diff --name-status master..vios_ntp

Diff contents of a single file between two commits

git diff 9e9384392 61713c9ac <filename>

Tags

Tag an existing commit

git log --pretty=oneline
git tag -m "Package version 1.5.0.22" -a v1.5.0.22 <commit_id>

Push all tags to remotte

git push origin --tags

Push a single tag to remote

git push origin v1.5.0.22

Delete local tag

git tag -d v1.5.0.22

Delete remote tag

git push --delete origin v1.5.0.22

Update local tags from remote

git fetch --tags --force

Show the commit a tag is pointing to

git rev-list -n 1 <tag>

Reset, Restore, & Remove

Unstage a file

git reset <file>

Undo last commit that hasn't been pushed

git reset HEAD^

Discard all branch changes and refresh from remote

git reset --hard origin/master

Unstaging a file, but leaving its actual changes untouched

git restore --staged myFile.txt

Discard local changes in a certain file

git restore myFile.txt

Undo all local changes in the working copy

git restore .

Restore any previous version of a specific file

git restore --source 6bcf266b index.html

Remove a staged file

git rm <file>

git showing unstaged changes

git rm --cached -r .
git reset --hard