Skip to content

Instantly share code, notes, and snippets.

@l2dy
Last active September 6, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l2dy/7da9621954ebcf1a19869f391662a41e to your computer and use it in GitHub Desktop.
Save l2dy/7da9621954ebcf1a19869f391662a41e to your computer and use it in GitHub Desktop.
# The canonical remote (tracked repository) at
# git@github.com:macports/macports-ports.git (SSH) or https://github.com/macports/macports-ports.git (HTTPS)
UPSTREAM_REMOTE=origin
# It should be "origin" if you cloned from one of the two URLs above.
# If you cloned your fork, add the canonical remote by executing the command below once.
# git remote add $UPSTREAM_REMOTE $REMOTE_URL
# "upstream" or "macports" is recommended for UPSTREAM_REMOTE.
# Add your SSH key to https://github.com/settings/keys to use the SSH URL.
# Run "git remote -v" to see a list of existing remotes.
# 0. Switch to master branch
git checkout master
# The master branch (local) is usualy used to track the master branch (remote) from the canonical repository (upstream).
# The master branch upstream represents the current ports tree.
# Don't commit in this branch unless you're going to push the changes right away.
# 1. Update the master branch.
git pull $UPSTREAM_REMOTE master
### Disaster Recovery
# CAUTION: All changes to tracked files in the working tree would be lost.
#git reset --hard $UPSTREAM_REMOTE/master
### Goto 1
# To clean untracked files:
#git clean -df
###
# URL of the originating repository
PR_ORIGINATING_REPO_URL=git@github.com:${GH_USER}/macports-ports.git
# Originating branch
PR_ORIGINATING_BRANCH=patch-1
### If the pull request is too big Then
# 2. Fetch changes
git fetch $PR_ORIGINATING_REPO_URL $PR_ORIGINATING_BRANCH:pr-874
# 3. Switch to the new branch, rebase onto master
git checkout pr-874
git rebase master
# Rebase with "-X ours" or "-X theirs" can automatically resolve conflicts, but
# may make mistakes.
### Else
# 2. Create a new banch
git checkout -b pr-874
# 3. Fetch changes
curl -fsSL "https://github.com/macports/macports-ports/pull/874.patch" | git am -3
# In case of conflicts:
# `git checkout --ours -- <file>` to check out <file> before `git am`.
# `git checkout --theirs -- <file>` to check out <file> in PR.
# or
# vim <file> # resolve conflicts by hand
# then
# git add <file>
# git am --continue
### End If
### Disaster Recovery
#git checkout master
#git branch -D pr-874
### Goto 2
### If only the last commit needs amending Then
# See also https://trac.macports.org/wiki/WorkingWithGit#commit
# 4. Make and review changes
git status
git diff
# 5. Stage changes
git add -A
# 6. Amend commit
git commit --amend
# This command calls $EDITOR and you can edit the commit message there.
### Else
# 4&5&6. Interactive Rebase (Advanced Git)
git rebase -i master
### End If
# 7. Review commits
git log --stat --pretty=fuller master..
# 8. Push changes to PR
git push -f $PR_ORIGINATING_REPO_URL HEAD:$PR_ORIGINATING_BRANCH
# HEAD refers to the currently checked out commit. You can use branch name instead.
# 9. Switch back to master branch
git checkout master
### If PR can be merged Then
# 10. Merge changes to master (fast-forward, without any merge commit)
git merge --ff-only pr-874
# 11. Push changes upstream
git push $UPSTREAM_REMOTE master
#### If push was "! [rejected]"
## -r: rebase
#git pull -r $UPSTREAM_REMOTE master
#### Goto 8
# 12. Delete the merged branch
git branch -d pr-874
### End If
### Tips
## Local branches can each track an "upstream branch"
# List local branches along with relationship to upstream branch
git branch -vv
# List remote branches too
git branch -avv
# Set upstream branch
git branch -u origin/master master
# Push to upstream branch of current branch
git push
# 5&6. Stage and amend commit
# -a: Automatically stage files that have been modified and deleted
git commit -a --amend
# Quick rebase and merge
# 0-1, 3 (git am), 8, 11
# Review changes including the previous commit before commit --amend
git diff HEAD^
# @ alone is a shortcut for HEAD.
# View changes made between --amend
git diff @@{1}
# Optimize local repository
# CAUTION: reflog entries are all pruned.
git reflog expire --expire=now && git gc --prune=now --aggressive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment