The goal of this repository is to help you re-create the need to rebase a branch. Then perform an interactive rebase and push your results. The set of steps will generally look like:
- Create
a-branchthat addsa.txtwith one text line to the repo. Pusha-branchto GitHub and open a pull request froma-branchtomaster - Create
b-branchthat comes out ofa-branch. Make and commit an adjustment toa.txt. Then pushb-branchto GitHub and open a pull request fromb-branchintoa-branch. - Squash merge
a-branchintomaster. See that on the PR ofb-branch, it is now pointing tomasterAND it has a conflict with master. git pull origin masteronmasterlocallygit checkout b-branchgit rebase -i master(This is THE MAIN SHOW)git push --force-with-lease
You can clone this repo by running the following command. It will create a learn-rebase folder where you run it. After creating it, cd into it
gh repo clone agam-armis/learn-rebase
cd learn-rebase
Just so that you aren't conflicting with other people learning rebase with this repo, create a directory with your name, and add files inside that directory:
mkdir your_name
cd your_name
Add a.txt with one line of text, commit it to your-name-a-branch and push your-name-a-branch to GitHub:
echo "Hello" > a.txt
git add a.txt
git checkout -b your-name-a-branch
git commit -m "adding a.txt"
git push origin --set-upstream-to origin/your-name-a-branch
Make a change to a.txt, commit it to your-name-b-branch and push your-name-b-branch to GitHub:
git checkout -b your-name-b-branch
echo "Goodbye" >> a.txt
git add a.txt
git commit -m "Concat goodbye to a.txt"
git push --set-upstream-to origin/your-name-b-branch
Let's open our PRs:
- Go to GitHub
- Open a PR for your
your-name-a-branchintomaster - Open a PR for your
your-name-b-branchintoyour-name-a-branch
Now is the time to cause a conflict between your-name-b-branch and master:
- Squash merge
your-name-a-branch - Check the PR for
your-name-b-branchand see that it has a conflict with master
Very important to first pull master (or fetch, but that's for a different session). So we'll pull master and checkout your-name-b-branch again:
git checkout master
cd .. # This is needed as when checking out master you removed the directory with your name, and this will take you back to the main directory
git pull
git checkout your-name-b-branch
And now, for the crucial moment:
git rebase -i master
After running this, you will get somehing like the following in your text editor of choice:
pick 2fd8ac1 adding a.txt
pick 308cc30 Concat goodbye to a.txt
# Rebase bb4765b..308cc30 onto bb4765b (1 command)
#
....
What we want here is to only keep the Concat goodbye to a.txt. To get this result we need to remove the other commit's line and save the file. You should get a success message. Now push the new your-name-b-branch but you should use the --force-with-lease flag as a good practice:
git push --force-with-lease