Skip to content

Instantly share code, notes, and snippets.

@MarcHeiden
Last active August 14, 2023 18:13
Show Gist options
  • Save MarcHeiden/aa11e6c520402df521b275416d45c06c to your computer and use it in GitHub Desktop.
Save MarcHeiden/aa11e6c520402df521b275416d45c06c to your computer and use it in GitHub Desktop.

Git Cheatsheet

Command Description
git config --get-regexp user list user credentials
git config --add user.name <username> set new local username
git config --add user.email <email> set new local email
git remote add [-f] <remoteConnection> <URL> add remote origin connetion; -f will fetch remote information => needed for subtree setup
git remote -v list remote connetions
git remote set-url <remoteConnection> <newURL> set new URL for remote connection
git remote rename <remoteConnection> <newRemoteConnection> rename remote connection
git remote rm <remoteConnection> remove remote connection
git push -u <remoteConnetion> <remoteBranch> set upstream branch for current branch and push to it
git branch list local branches
git branch -r list remote branches
git branch -a list local and remote branches
git branch <newBranch create new local branch
git branch -M <newName> rename current branch
git branch -d <branch> delete local branch
git push -d <remoteConnection> <branch> delete remote branch
git switch <branch> switch branch
git switch -c <newBranch> create new local branch and switch to it
git fetch -p delete stale remote branches and fetch new ones
git restore <file> discard changes in working directory
git reset <file> unstage file without touching the working directory
git reset <commit> go back to commit and unstage files without touching the working directory; use for local changes only
git reset --hard <commit> go back to commit and wipe staging tree and working tree; use for local changes only
git revert <commit> adds a new commit that undos/reverts the specified commit; use primarily for public changes
git commit --amend -m <newMessage> change message of latest commit; use for local changes only
git rm --cached <file> remove file from repo without removing it from the local working directory
git stash stash tracked files
git stash -u include untracked files
git stash list list stashes
git stash pop <id> apply stash
git stash apply <id> apply stash and keep it
git stash drop <id> drop stash without applying
git rebase <branch> rebase current branch against given branch; use for local branches only
git rebase -i <branch> start interactive rebase for current branch against given branch; use for local branches only
git rebase -i HEAD~<numberOfCommits> start interactive rebase for the last number of commits; allows squashing commits
git push -f force push; needed if branch history was rewritten; remote branch will be replaced with the current state of the local branch; do not use for public branches
git subtree add --prefix <newSubRepoDirectory> <remoteConnection> <branchOfSubRepo> --squash add sub-repo and squash merge with main repo
git subtree pull --prefix <subRepoDirectory> <remoteConnection> <branchOfSubRepo> --squash pull sub-repo and squash merge with main repo
git subtree push --prefix <subRepoDirectory> <remoteConnection> <branchOfSubRepo> push commits to sub-repo
git rm -r <subRepoDirectory> remove sub-repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment