Last active
August 12, 2021 23:04
-
-
Save ben8p/10ca9abe8303869a52ebc165c627d000 to your computer and use it in GitHub Desktop.
Git cheatsheet
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
# Rename local and remote branch | |
OLD_BRANCH=name_of_old_branch # Replace with the name of your old branch | |
NEW_BRANCH=name_of_new_branch # Replace with the name of the new branch | |
git checkout $OLD_BRANCH | |
git pull | |
git branch -m $OLD_BRANCH $NEW_BRANCH | |
git push origin :$OLD_BRANCH --no-verify | |
git push --set-upstream origin $NEW_BRANCH --no-verify | |
################################################################################################################## | |
# Merge a branch into another one | |
MERGE_BRANCH=from_branch # Replace with the name of the branch you will merge | |
INTO_BRANCH=to_branch # Replace with the name of the branch which will receive the merge | |
git checkout $MERGE_BRANCH | |
git pull | |
git checkout $INTO_BRANCH | |
git pull | |
git merge $MERGE_BRANCH | |
# if any conflict > git mergetool | |
git clean -f *.orig | |
git commit --no-verify | |
git push --no-verify | |
################################################################################################################## | |
# Selective merge | |
MERGE_BRANCH=from_branch # Replace with the name of the branch you will merge | |
INTO_BRANCH=to_branch # Replace with the name of the branch which will receive the merge | |
git checkout $MERGE_BRANCH | |
git pull | |
git checkout $INTO_BRANCH | |
git pull | |
git checkout -b ${INTO_BRANCH}temp | |
git merge --no-commit --no-ff $MERGE_BRANCH | |
git reset # to unstage all changes | |
# a) got to an IDE to stage files you want to merge | |
# b) or use | |
# git add --interactive | |
# to choose what to add, interactively | |
# then use the patch option | |
# c) eventually, use | |
# git add | |
# to stage any untracked files | |
git commit -m "merged selected patches from $MERGE_BRANCH branch" --no-verify | |
git checkout . | |
git checkout $INTO_BRANCH | |
git merge ${INTO_BRANCH}temp | |
# if any conflict > git mergetool | |
git clean -f *.orig | |
git commit --no-verify | |
git push --no-verify | |
git branch -D ${INTO_BRANCH}temp | |
################################################################################################################## | |
# Delete local branch (remote remains unchanged) | |
LOCAL_BRANCH=local_branch # Replace with the name of the local branch you want to delete | |
git branch -d $LOCAL_BRANCH | |
################################################################################################################## | |
# Create branch from tag | |
TAG_NAME=tag_name # Replace with the tag name you want to use for the new branch | |
BRANCH_NAME=branch_name # Replace with the name of the new branch | |
git checkout -b $BRANCH_NAME $TAG_NAME | |
git push --set-upstream origin $BRANCH_NAME --no-verify | |
################################################################################################################## | |
# Prune tracking branches and delete stale local branches | |
git remote prune origin | |
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d | |
git prune | |
################################################################################################################## | |
# Save current changes into a patch file | |
git stash | |
git stash show -p > ~/change.patch | |
git stash apply | |
################################################################################################################## | |
# Apply a patch file | |
git apply ~/change.patch | |
################################################################################################################## | |
# Reset only unstaged changes | |
git checkout -- . | |
# or | |
git checkout path/to/file/to/revert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment