Skip to content

Instantly share code, notes, and snippets.

@PhearZero
Last active November 27, 2018 19:52
Show Gist options
  • Save PhearZero/992a14179550024dcc42a79d7f03d694 to your computer and use it in GitHub Desktop.
Save PhearZero/992a14179550024dcc42a79d7f03d694 to your computer and use it in GitHub Desktop.
Git Rebase and or Merge
#!/usr/bin/env bash
# This scenario creates a Github repository with two forks, one
# is stale/missing features from upstream forks/project
# Dir Structure:
# fakeproject
# fakeproject-fork
# fakeproject-stale-fork
############################## Dependencies ##############################
# Install Hub https://github.com/github/hub
if ! type "hub" > /dev/null; then
wget -c https://github.com/github/hub/releases/download/v2.6.0/hub-linux-amd64-2.6.0.tgz -O - | tar -xz
mv hub-linux-amd64-2.6.0 hub
sudo ./hub/install
rm -rf ./hub *.tgz
fi
# Install GHI https://github.com/stephencelis/ghi
if ! type "ghi" > /dev/null; then
gem install ghi
fi
############################## DANGER! :hazard: ##############################
# WARNING DELETE PERSONAL TOKEN WHEN DONE!!!! Not strict security
# Generate Example Repo, Make sure you have a ~/.config/hub creds or
# GITHUB_USER and GITHUB_PASSWORD environment variables
# Also make sure you have the permissions to delete/create
# https://github.com/settings/tokens
hub delete -y PhearZero/fakeproject > /dev/null 2>&1
hub delete -y PhearNet/fakeproject > /dev/null 2>&1
hub delete -y NodeJunkie/fakeproject > /dev/null 2>&1
hub create PhearZero/fakeproject > /dev/null 2>&1
############################## Main Repo Scenario ##############################
# Remove Example Repo
rm -rf fakeproject
# Create New Git Project
mkdir fakeproject
cd fakeproject
hub init > /dev/null 2>&1
# Checkout master locally
hub checkout -qb master
# Add some kind of base
echo "# Hello World" > README.md
hub add README.md
hub commit -qm ':pencil2: Init Repo Test'
# Add Remote and Force Push history to base
hub remote add origin git@github.com:PhearZero/fakeproject.git > /dev/null 2>&1
hub push -f --set-upstream origin master > /dev/null 2>&1
# Show Current Graph
echo '############### State 1 - Init Main Repo ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
############################## Create Issues ##############################
# Create issues for developers
read -d '' ISSUE_ONE <<-EOM
Add Feature 1
- [ ] Feature 1
EOM
# Create issues for developers
read -d '' ISSUE_TWO <<-EOM
Add Feature 2
- [ ] Feature 2
EOM
hub issue create -m "$ISSUE_ONE" > /dev/null 2>&1
hub issue create -m "$ISSUE_TWO" > /dev/null 2>&1
# Change to workspace
cd ..
############################## Init Scenarios ##############################
# Create Issue 1 Fork
rm -rf fakeproject-fork
hub clone PhearZero/fakeproject fakeproject-fork > /dev/null 2>&1
# Create Issue 2 Stale Fork
rm -rf fakeproject-stale-fork
hub clone PhearZero/fakeproject fakeproject-stale-fork > /dev/null 2>&1
############################## Developer 1 Scenario ##############################
cd ./fakeproject-fork
# Checkout feature branch
hub checkout -qb feat/developer-#1
# Add some base file and commit
touch developer-1.txt
hub add developer-1.txt
hub commit -qm ':construction: Adding Developer 1'
# Show Current Graph
echo '############### State 2 - Fork Feature Branch ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
#hub log --graph --full-history --all --pretty=format:"%h%x09%d%x20%s"
echo 'Hello Developer' >> developer-1.txt
hub commit -qam ':construction: Working on developer-1'
echo 'Hello Developer' >> developer-1.txt
hub commit -qam ':construction: Working on developer-1'
echo 'Hello Developer' >> developer-1.txt
hub commit -qam ':construction: Working on developer-1'
## Reset
echo '############### State 3 - Fork Feature Branch WIP ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
# Reset or rebase interactive history
hub reset --soft HEAD~4
# Commit the rebase/reset
hub commit -qm ':sparkles: Adding Developer 1 - Feature'
echo '############### State 4 - Fork Feature Branch Squashed ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
# Pull Request Text
read -d '' FEAT_ONE <<-EOM
Developer 1 Feature
- [X] :sparkles: Some Developer Feature
fixes #1
EOM
# Fork, Push feature and create pull request
hub fork --org=PhearNet > /dev/null 2>&1
hub push -f PhearNet feat/developer-#1 > /dev/null 2>&1
hub pull-request --no-edit --push -m "$FEAT_ONE" > /dev/null 2>&1
# Change to workspace
cd ..
############################## Developer 1 Merge/Rebase ##############################
# Issue 1 Fixed Text
read -d '' ISSUE_ONE_FIXED <<-EOM
Add Feature 1
- [X] Feature 1
EOM
# Rebase Example: Always Fast Forwarding(Drop Down Rebase PR Behavior)
cd ./fakeproject
ghi edit 1 -m "$ISSUE_ONE_FIXED"
hub fetch origin > /dev/null 2>&1
hub checkout -b feat/developer-#1 origin/feat/developer-#1 > /dev/null 2>&1
hub rebase master > /dev/null 2>&1
hub push -f > /dev/null 2>&1
hub checkout master > /dev/null 2>&1
hub rebase feat/developer-#1 > /dev/null 2>&1
hub push -f > /dev/null 2>&1
# Merge Example: Force No Fast Forwarding (Default PR Behavior)
#cd ./fakeproject
#hub fetch origin > /dev/null 2>&1
#hub checkout -b feat/developer-#1 origin/feat/developer-#1 > /dev/null 2>&1
#hub merge --no-ff --no-edit master > /dev/null 2>&1
#hub push > /dev/null 2>&1
#hub checkout master > /dev/null 2>&1
#hub merge --no-ff --no-edit feat/developer-#1 > /dev/null 2>&1
#hub push > /dev/null 2>&1
echo '############### State 5 - Fork Feature Branch Merge/Rebase ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
# Change to workspace
cd ..
############################## Developer 2 Scenario ##############################
cd ./fakeproject-stale-fork/
# Checkout feature branch
hub checkout -qb feat/developer-#2
# Add some base file and commit
touch developer-2.txt
hub add developer-2.txt
hub commit -qm ':sparkles: Adding Developer 2 - Feature'
# Show Current Graph
echo '############### State 6 - Stale Fork Feature Branch ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
# Pull Request Text
read -d '' FEAT_ONE <<-EOM
Developer 2 Feature
- [X] :sparkles: Some Developer Feature
fixes #2
EOM
# Fork, Push feature and create pull request
hub fork --org=NodeJunkie > /dev/null 2>&1
hub push -f NodeJunkie feat/developer-#2 > /dev/null 2>&1
hub pull-request --no-edit --push -m "$FEAT_ONE" > /dev/null 2>&1
# Change to workspace
cd ..
############################## Developer 2 Merge/Rebase ##############################
# Issue 2 Fixed Text
read -d '' ISSUE_TWO_FIXED <<-EOM
Add Feature 2
- [X] Feature 2
EOM
# Rebase Example: Always Fast Forwarding(Drop Down Rebase PR Behavior)
cd ./fakeproject
ghi edit 2 -m "$ISSUE_TWO_FIXED"
hub fetch origin > /dev/null 2>&1
hub checkout -b feat/developer-#2 origin/feat/developer-#2 > /dev/null 2>&1
hub rebase master > /dev/null 2>&1
hub push -f > /dev/null 2>&1
hub checkout master > /dev/null 2>&1
hub rebase feat/developer-#2 > /dev/null 2>&1
hub push -f > /dev/null 2>&1
# Rebase a stale feature if it is still active
hub fetch origin > /dev/null 2>&1
hub checkout feat/developer-#1 > /dev/null 2>&1
hub rebase master > /dev/null 2>&1
hub push -f > /dev/null 2>&1
# Merge Example: Force No Fast Forwarding (Default PR Behavior)
#cd ./fakeproject
#hub fetch origin > /dev/null 2>&1
#hub checkout -b feat/developer-#2 origin/feat/developer-#2 > /dev/null 2>&1
#hub merge --no-ff --no-edit master > /dev/null 2>&1
#hub push > /dev/null 2>&1
#hub checkout master > /dev/null 2>&1
#hub merge --no-ff --no-edit feat/developer-#2 > /dev/null 2>&1
#hub push > /dev/null 2>&1
echo '############### State Fin! ####################'
hub log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
# Change to workspace
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment