Skip to content

Instantly share code, notes, and snippets.

@vkroz
Last active October 17, 2019 19:42
Show Gist options
  • Save vkroz/4ac9ea7f8b2216143ce5 to your computer and use it in GitHub Desktop.
Save vkroz/4ac9ea7f8b2216143ce5 to your computer and use it in GitHub Desktop.
GIT cheatsheet

Reset remote origin/master to a commit

git checkout master
git reset --hard e3f1e37
git push --force origin master
# Reset remote to a current HEAD
git reset --hard HEAD
git push -f origin master

Rename remote

$ git remote -v
# View existing remotes
> origin  https://github.com/OWNER/REPOSITORY.git (fetch)
> origin  https://github.com/OWNER/REPOSITORY.git (push)

$ git remote rename origin destination
# Change remote name from 'origin' to 'destination'

$ git remote -v
# Verify remote's new name
> destination  https://github.com/OWNER/REPOSITORY.git (fetch)
> destination  https://github.com/OWNER/REPOSITORY.git (push)

Squash my last X commits together

You can do this fairly easily without git rebase or git merge --squash. In this example, we'll squash the last 3 commits. If you want to write the new commit message from scratch, this suffices:

git reset --soft HEAD~3 &&
git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages (i.e. similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit:

git reset --soft HEAD~3 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment