Skip to content

Instantly share code, notes, and snippets.

@ozbe
Created May 25, 2020 20:22
Show Gist options
  • Save ozbe/c192d122f2c9212c2803d21f288c493d to your computer and use it in GitHub Desktop.
Save ozbe/c192d122f2c9212c2803d21f288c493d to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheatsheet

Rewrite author

Last commit

git commit --amend --author="[NAME] <[EMAIL]>" --no-edit

Batch

git filter-branch --commit-filter ‘
    if [ “$GIT_COMMITTER_EMAIL” = “[OLD_EMAIL]” ];
    then
        GIT_COMMITTER_NAME="[NAME]";
        GIT_AUTHOR_NAME="[NAME]";
        GIT_COMMITTER_EMAIL="[NEW_EMAI]L";
        GIT_AUTHOR_EMAIL="[NEW_EMAIL]";
        git commit-tree "$@";
    else
         git commit-tree "$@";
    fi' HEAD

Upstream branches

Global Default

git config --global push.default current

Per branch

git push -u origin [BRANCH]

Git Stash Unstaged files

git stash save —keep-index

Remove submodule

$ mv a/submodule a/submodule_tmp
$ git submodule deinit -f -- a/submodule 
$ rm -rf .git/modules/a/submodule
$ git rm -f a/submodule Note: a/submodule (no trailing slash)

Source: git - How do I remove a submodule? - Stack Overflow

Change email

Note: You can change name by using user.name in place of user.email

Global

$ git config --global user.email “[EMAIL]“

Per repo

$ git config user.email "[EMAIL]"

Empty commit

$ git commit -m "[MESSAGE]" —allow-empty

Mirror repo

$ git clone --bare https://github.com//exampleuser///old-repository/.git
$ git push —mirror https://github.com//exampleuser///new-repository/.git

Global .gitignore

$ vi ~/.gitignore

Example

.idea/
*.sublime-project
.DS_Store

Clean dirty and untracked files

$ git clean -xfd

Remove files from commit back to staged

Source Remove files from Git commit - Stack Overflow

$ git reset —soft HEAD~1
$ git reset HEAD [FILE]
$ git commit -c ORIG_HEAD  

Show ignored files

$ git status --ignored
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment