Skip to content

Instantly share code, notes, and snippets.

@troyfolger
Last active April 15, 2020 00:08
Show Gist options
  • Save troyfolger/cb4251657914dbb0fae81be436ef85ed to your computer and use it in GitHub Desktop.
Save troyfolger/cb4251657914dbb0fae81be436ef85ed to your computer and use it in GitHub Desktop.
Handy Git techniques

Git tips

Squashing commits with no rebase:

git checkout master
# do code stuff, decide to start a branch
git checkout -b newbranch
# do more code stuff
git commit -m "commit one"
git push
git commit -m "commit two"
git push
# ... etc
# decide to squash commits for a pull request:
git reset --soft $(git merge-base --fork-point master)
git commit -m "feature xxx complete"
git push --force
# boom - single commit with specified message

Create and apply patches:

# clone repoX
git checkout master
# do code stuff, perhaps start a branch
git checkout -b newbranch
# do more code stuff
git commit -m "commit one"
git commit -m "commit two"
# ... etc
# decide to create a patch without pushing commits
# (useful when you don't have write access to a repository,
#  or maybe you just don't want to pollute the branch):
git format-patch origin/master --stdout > repoX_diff_to_master.patch
# woot, 'reponameX_diff_to_master.patch' has a formatted diff of all commits
# clone repoX somewhere new
git checkout master
git apply reponameX_diff_to_master.patch
git status
# boom, all files are patched (but not committed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment