Skip to content

Instantly share code, notes, and snippets.

@LutherBaker
Last active August 29, 2015 14:01
Show Gist options
  • Save LutherBaker/3b5be99b70bdb38ec84d to your computer and use it in GitHub Desktop.
Save LutherBaker/3b5be99b70bdb38ec84d to your computer and use it in GitHub Desktop.
Git Cheatsheet
. ~/.bash_colors
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
export PS1=$txtred'[\W]'$txtylw'$(__git_ps1)'$txtrst' \$ '
fi
export GIT_PS1_SHOWUPSTREAM=auto
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1

Cleanup

Assume commit history ‘A’-c-c-c-c-‘B’-c-c-c-c-c-‘C’

  • you want to cleanup the commits from ‘A’ to ‘B’

$ git reset —hard B
$ git reset A

You might use this technique by creating branch ‘B’ if necessary.

[alias]
br = branch
ci = commit
st = status
put = push origin HEAD
co = checkout
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
precomposeunicode = false
[push]
default = simple
[credential]
helper = cache --timeout 1
[remote "origin"]
url = https://[email protected]/project/product.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
##
## temporarily ignore files
## http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html
##
git update-index --assume-unchanged <file>
git update-index --no-assume-unchanged <file>
##
## config: (per repo) user.name, user.email
##
$ git config user.name "Your Name"
$ git config user.email [email protected]
##
## config: (global) user.name, user.email
##
$ git config --global user.name "Your Name"
$ git config --global user.email [email protected]
##
## config: push.default changed from matching to simple
##
git config --global push.default simple
##
## amending a commit to reset author
##
$ git commit --amend --reset-author
##
## http://git-scm.com/docs/git-credential-cache
##
$ git config credential.helper 'cache --timeout=1'
##
## http://stackoverflow.com/questions/15381198/remove-credentials-from-git
##
git config --global --unset credential.helper
##
## https://help.github.com/articles/updating-credentials-from-the-osx-keychain
##
$ git credential-osxkeychain erase
host=github.com
protocol=https
# [Hit return]

Adding git commands

  • The rule is simple, if you want to create a validate command, the script will be called git-validate and must be placed in one of the folders present in your PATH.
#!/bin/bash
echo "ufetch : select user"
USERS=(
"jessica.tate"
"jon.doe"
"luther.baker"
)
for idx in "${!USERS[@]}"
do
echo "$idx) ${USERS[$idx]}"
done
read -p"> " USERNUM
USERNAME=${USERS[$USERNUM]}
ORIG_URL=$(git config --get remote.origin.url)
NEW_URL=$(echo "$ORIG_URL" | sed -e"s|https://[^@]*@gitlab\.replace_domain\.com|https://${USERNAME}@gitlab.replace_domain.com|")
git config --replace-all remote.origin.url "$NEW_URL"
git config --replace-all user.name "$USERNAME"
git config --replace-all user.email "$USERNAME@replace_domain.com"
echo
echo "Running the following command as $USERNAME:"
echo " git fetch $@"
echo
git fetch "$@"
if ! git diff-index --quiet HEAD --; then
# do this to simply log the changes to the console (for later inspection)
git diff
# do this because we need to add, comimt and push
# note that the comment, the path and the branch are hardcoded
git --git-dir=/Users/jenkins/.jenkins/.git --work-tree=/Users/jenkins/.jenkins commit -a -m "Automatic evening repository update." && git push origin master
fi

Scripting

git --git-dir=/mycode/.git --work-tree=/mycode status
if ! git diff-index --quiet HEAD --; then
    VN="$VN-mod"
fi
good-cmd && echo "succeeded" || echo "failed"

bad-cmd || {
    recovery-cmd || 
    echo "recovery failed && 
    echo "recovered"
}
ffmpeg <your args>
RC=$?
if [ "${RC}" -ne "0" ]; then
    # Do something to handle the error.
else
    # Everything was ok.
fi
  1. Stack

Check


git config --local credential.helper
git config --global credential.helper
git config --system credential.helper

Unset


git config --local --unset credential.helper
git config --global --unset credential.helper
git config --system --unset credential.helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment