Skip to content

Instantly share code, notes, and snippets.

@Eidansoft
Last active August 28, 2020 07:13
Show Gist options
  • Save Eidansoft/1d157b67828eb26eaf1194ddabb4e11a to your computer and use it in GitHub Desktop.
Save Eidansoft/1d157b67828eb26eaf1194ddabb4e11a to your computer and use it in GitHub Desktop.
Useful quick Git commands
# _____ _
# | __ \ | |
# | |__) |___| |__ __ _ ___ ___
# | _ // _ \ '_ \ / _` / __|/ _ \
# | | \ \ __/ |_) | (_| \__ \ __/
# |_| \_\___|_.__/ \__,_|___/\___|
#
# At the branch containing all the changes you must run...
git checkout BRANCH_WITH_ALL_CHANGES
git rebase -i BRANCH_TO_PUT_CHANGES_INTO
# your default text editor will be opened and you can choose to pick, drop, squash, ... every commit
# Another posibility is to merge into the destination branch the branch with all changes, but without
# fastforward neither commit files, so you can later to choose what files you wanna keep changed:
git checkout BRANCH_TO_PUT_CHANGES_INTO
git merge --no-ff --no-commit BRANCH_WITH_ALL_CHANGES
# Now you can discard the modified files that you do not need
git checkout HEAD file_with_changes_to_be_discarded.txt
# Once you are happy, just commit and push
# _______ _____
# |__ __|/\ / ____|
# | | / \ | | __ ___
# | | / /\ \| | |_ / __|
# | |/ ____ \ |__| \__ \
# |_/_/ \_\_____|___/
#
# Create tag in LOCAL
git tag -a TAG_NAME -m 'message for the tag'
# Save the local tag to REMOTE
git push origin TAG_NAME
# Delete tag from REMOTE
git push --delete origin TAG_NAME
# Delete tag in LOCAL
git tag --delete TAG_NAME
# _____ __ _ _ _
# / ____| / _| (_) | |
# | | ___ _ __ | |_| |_ ___| |_ ___
# | | / _ \| '_ \| _| | |/ __| __/ __|
# | |___| (_) | | | | | | | | (__| |_\__ \
# \_____\___/|_| |_|_| |_|_|\___|\__|___/
# When merge a branch and you face lot of conflicts, and want to solve them all toghether with the same
# Keeping our changes
git status | grep 'both modified' | cut -d ':' -f 2 | tr -d ' ' | xargs -I % git checkout --ours %
# OR accepting the other branch changes
git status | grep 'both modified' | cut -d ':' -f 2 | tr -d ' ' | xargs -I % git checkout --theirs %
# Now accept all changed files
git status | grep 'both modified' | cut -d ':' -f 2 | tr -d ' ' | xargs -I % git add %
# And commit them (and push if you want)
git commit
# __ __
# | \/ |
# | \ / | ___ _ __ __ _ ___
# | |\/| |/ _ \ '__/ _` |/ _ \
# | | | | __/ | | (_| | __/
# |_| |_|\___|_| \__, |\___|
# __/ |
# |___/
# In order to merge a branch into another, without allow git to auto-merge, but you been able
# to decide what and how merge, just run the merge with the parameters, then reset to HEAD:
git merge --no-commit --no-ff BRANCH_NAME
git reset HEAD
# Now you will got the changes done at your local files ready to be commited (you can check them
# with a diff):
git diff
# if you want to discard any one, just:
git checkout -- FILE_PATH
# And all the rest as usual, adding and commiting
git add FILE_PATH
git commit
# _ __ __ _ __ __ ______ __
# | | / /___ _____/ /__ _ __(_) /_/ /_ / ____/___ _____/ /_______
# | | /| / / __ \/ ___/ //_/ | | /| / / / __/ __ \ / /_ / __ \/ ___/ //_/ ___/
# | |/ |/ / /_/ / / / ,< | |/ |/ / / /_/ / / / / __/ / /_/ / / / ,< (__ )
# |__/|__/\____/_/ /_/|_| |__/|__/_/\__/_/ /_/ /_/ \____/_/ /_/|_/____/
#
# When you want to merge into a local branch code that it is into a forked repository, you can bring to
# your local the code from the Pull Request, just add this onto the ~/.gitconfig for general use, or into
# your project .git/config file.
[alias]
pr-checkout = "!f() { git fetch $1 +pull-requests/$2/from; git checkout -b PR-$2 FETCH_HEAD; } ; f"
pr-merge = "!f() { git fetch $1 +pull-requests/$2/from; git merge --no-ff FETCH_HEAD; } ; f"
# Now you are able to directly locally merge into your branch by the PR number
git pr-merge origin 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment