Skip to content

Instantly share code, notes, and snippets.

@antoni
Last active December 29, 2022 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoni/ab29e5d2a9aee3aeea41 to your computer and use it in GitHub Desktop.
Save antoni/ab29e5d2a9aee3aeea41 to your computer and use it in GitHub Desktop.
Git cheat sheet
# Move code from one repo to another, preserving commit history
# Source: http://stackoverflow.com/a/17373088/963881
cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master
git remote rm r1remote
# Push to master without having to --set-upstream-to first
git push -u origin master
# Revert push to given commit hash
git push -f origin last_known_good_commit:branch_name
# Alternative
git checkout branch_name
git reset --hard commit_hash
git push origin +branch_name
# Push a new local branch to a remote repo
git checkout -b feature_branch_name
# ... edit files, add and commit ...
git push -u origin feature_branch_name
# Undo a git add <file> command
git reset <file>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment