Last active
July 16, 2018 12:04
-
-
Save lusy/34f6ff4481f58ab2d87a1895a56323f0 to your computer and use it in GitHub Desktop.
commonly used git tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add file to last commit: | |
$ git add left_out_file | |
$ git commit --amend --no-edit | |
# change last commit message | |
$ git commit --amend | |
# unmodifying a modified file | |
$ git checkout -- <modified-file> | |
# rebase | |
$ git fetch origin | |
$ git rebase origin/master % or origin/<branch-you-want-to-have-as-base> | |
$ git push --force % history was re-written, so push alone wouldn't work (on github you may want to change the base of the pull request then) | |
# squashing commits | |
$ git rebase -i HEAD~3 % allow me to interactively edit the last 3 commits | |
% a vim interface with possible options pops out | |
# delete branches | |
## locally | |
$ git branch -d branch_name % only if branch has been fully merged | |
$ git branch -D branch_name % alias for --delete --force | |
## remotely | |
$ git push <remote_name> --delete <branch_name> | |
so e.g. | |
$ git push origin --delete branch-tralala | |
# cherry-picking | |
* check the hash of the commit you want to cherry-pick | |
* switch to the branch you want to cherry-pick it in | |
run | |
$ git cherry-pick <commit-hash> | |
# Move most recent commit(s) to a new branch | |
$ git branch newbranch # Create a new branch, saving the desired commits | |
$ git reset --hard HEAD~3 # Move master back by 3 commits (GONE from master) | |
$ git checkout newbranch # Go to the new branch that still has the desired commits | |
# throw away local changes, get remote branch | |
$ git checkout mybranch | |
$ git reset --hard origin/mybranch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment