Skip to content

Instantly share code, notes, and snippets.

@drAlberT
Last active December 16, 2017 15:55
Show Gist options
  • Save drAlberT/eaf406b4c5278ea1f97f to your computer and use it in GitHub Desktop.
Save drAlberT/eaf406b4c5278ea1f97f to your computer and use it in GitHub Desktop.
Git / GitHub CheatSheet

Git / GitHub CheatSheet

Quick reference for git / GitHub daily workflow

Git

create branches

  • create the local branch
git checkout -b branch_name
  • push it to remote and set upstream
git push -u origin branch_name

(if you have an existing tracking branch already set on the branch you're pushing, and push.default is set to upstream, this will not do what you think it will do. It will try to push over the existing tracking branch. In this case do branch --unset-upstream first)

remove branches

remove remote branch (note the ':')

git push origin :branch_name

(or, more verbosely, git push origin --delete branch_name)

remove your local checkout

git branch -d branch_name

pruning stale remote-tracking branches

git remote prune origin [--dry-run]

renamig branches

assumed your remote is named "origin":

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

old_branch can be omitted if renaming the current branch

set branch tracking

assumed your remote is named "origin"

git branch --set-upstream origin/foo

Or, if local branch foo is not the current branch:

git branch --set-upstream origin/foo foo

On git 1.8 you can use -u instead of --set-upstream

handle submodules

add a submodule

git submodule add --name "Submodule name" -- https://github.com/drAlberT/vimcfg4php.git path/to/submodule    

fully remove a submodule

git submodule deinit path/to/submodule
git rm path/to/submodule
# or, if you want to leave it in your working tree
git rm --cached path/to/submodule
rm -rf .git/modules/path/to/submodule

set / get configuration

choose among --local, --global, --system; specifing nothing means "local"

  • get config
git config --local -l
  • set value
git config --local user.name drAlberT

set global push infos and cache credentials

git config --global user.name "Your Name"
git config --global user.email you@example.com
git config --global credential.helper 'cache --timeout=86400'

reset local

git fetch origin
git reset --hard origin/master
git clean -f -d

reset a single file to a given version

git checkout <version_hash|branch> -- path/file

change commit's author

last commit

git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

entire repo

Use this bash script to search and replace every occurrence of given email(s) into the current checkout.

  • create a fresh, bare clone of your repository:
git clone --bare https://github.com/user/reponame.git FIXME
cd FIXME
  • if needed get the list of users having contributed with their emails
git shortlog -sne

or, to get only the emails

git shortlog -sne | sed 's/^.*<\([^>]\+\)>.*$/\1/' | sort | uniq
git push --force --tags origin 'refs/heads/*'
  • remove the local copy of the repo
cd ..
rm -rf FIXME

rebase et al.

merge a feature_branch into main devel

git checkout <feature_branch>
git fetch <upstream>
git rebase <upstream>/<main_branch>
git push --force-with-lease
#!/usr/bin/env bash
# see https://help.github.com/articles/changing-author-info/
git filter-branch --env-filter $'
OLD_EMAILS="
albert@faktiva.com
emiliano.gabrielli@gmail.com
"
CORRECT_NAME="Emiliano \'AlberT\' Gabrielli"
CORRECT_EMAIL="albert@faktiva.com"
for EMAIL in ${OLD_EMAILS}; do
if [ "$GIT_COMMITTER_EMAIL" = "$EMAIL" ]; then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$EMAIL" ]; then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
done
' --tag-name-filter cat -- --branches --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment