Skip to content

Instantly share code, notes, and snippets.

@zxchris
Last active May 29, 2020 08:53
Show Gist options
  • Save zxchris/efc606d935a5322dc4ff980eaf085df2 to your computer and use it in GitHub Desktop.
Save zxchris/efc606d935a5322dc4ff980eaf085df2 to your computer and use it in GitHub Desktop.

Git Crib Sheet

Transfer files to another repo, keeping history

  1. Clone new version of repo, on required branch.
git clone --branch <branch> --origin origin --progress -v <git repository A url>
  1. Remove origin to prevent accidentally pushing
git remote rm origin
  1. Strip everything from the repo, except required "directory"
git filter-branch --subdirectory-filter <directory> -- --all
  1. Clean redundant information
git reset --hard
git gc --aggressive 
git prune
git clean -fd
  1. The files will now be at the root directory. Move into a subdir if required
mkdir new_sub_dir
git mv * new_sub_dir
git add .
git commit
  1. Now prepare recipient repository. Create a temporart remote connection to the stripped local repo
git remote add stripped-repo <stripped repository directory path>
  1. Pull files and history into repo
git pull stripped-repo master --allow-unrelated-histories
  1. Remove temporary remove from repo
git remote rm stripped-repo
  1. Push changes
git push

Branch deleting

Local branch

git branch -d branch_name
git branch -D branch_name

Delete remote branch

git push <remote_name> --delete <branch_name>

git push origin --delete feature/something

Git branch view commands

Show the parent branch of a branch

git show-branch | grep '*' | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

Terse branch parent-child relationship tree

Black and White version

git config --global alias.lbc = log --graph --simplify-by-decoration --pretty=format:'%d' --all

Colour version

git config --global aliases.lbcc = log --graph --simplify-by-decoration --oneline --pretty=format:'%C(auto)%d %s' --branches

Colour branch tree with commits

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches

git config --global alias.lbf = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment