In this tutorial, you will learn how to:
main
branch is cleanBefore we get started, let’s make sure we’re all on the
main
branch:
git switch main
And make sure we have a clean status (warning: this will get rid of any unpushed commits or uncommitted changes):
git fetch
git reset --hard origin/main
Now let’s get started on a pull request where we’ll be adding a guide for contributors to our repo.
First, make a new branch and switch to it:
git branch contributing-guide
git switch contributing-guide
Now open a CONTRIBUTING.md
:
nano CONTRIBUTING.md
Enter the following content:
* Pull requests are welcome!
Save and exit nano
(Ctrl + x
,
y
, Enter
), then add and commit
CONTRIBUTING.md
:
git status
git add CONTRIBUTING.md
git status
# We'll use a shortcut to write the message inline
git commit -m "Add contributing guide"
git status
You should now see contributing-guide
one commit ahead
of main
:
git graph
q
to exit git graph
)main
branch.First, let’s push the contributing-guide
branch to the
origin
remote:
git push -u origin contributing-guide
-u
is only needed the first time you push a particular
branchNow open your repo’s page on GitHub, you’ll see various shortcuts to create a pull request for your newly pushed branch, but let’s do it the standard way:
Pull requests
tabNew pull request
main
for the base branchcontributing-guide
for the
compare branch
contributing-guide
, along with a diff of the changesCreate pull request
Create pull request
Now let’s see what happens if someone updates main
and
causes a merge conflict between your pull request branch and
main
.
Let’s switch back to main
:
git switch main
And add a CONTRIBUTING.md
on this branch
nano CONTRIBUTING.md
Give it some different content:
* Please rebase conflicting pull requests
Save and exit nano
(Ctrl + x
,
y
, Enter
), then add and commit
CONTRIBUTING.md
:
git status
git add CONTRIBUTING.md
git status
# We'll use a shortcut to write the message inline
git commit -m "Remind contributors to rebase"
git status
Now push the new commit on main
to the
origin
remote:
git push origin main
And see that the two branches have now diverged, both locally and on
origin
:
git graph
Look at your pull request on GitHub, and you’ll see that it can no longer be merged because of a merge conflict.
Let’s resolve the conflict by rebasing your pull request branch on
top of main
, which is the standard approach used by most
teams and open-source projects:
First, switch back to your pull request branch:
git switch contributing-guide
Before rebasing onto main
, it’s important to make sure
you have fetched all of the commits on it:
git fetch
Now rebase onto origin/main
to update our current
branch, just as we would if we were merging:
git rebase origin/main
origin/main
is safer than rebasing onto
the local main
because we might forget to make sure
main
is up to date with origin/main
If you’ve done this correctly, you should see a conflict!
Let’s check the details:
git status
git diff
git rebase --abort
Just like last lesson, we need to manually resolve the conflict by
editing CONTRIBUTING.md
nano CONTRIBUTING.md
Edit it to contain both lines and nothing else:
* Please rebase conflicting pull requests
* Pull requests are welcome!
Git diff shows the resolution we have made:
git diff
To finish the rebase, we need to add the resolution:
git add CONTRIBUTING.md
git status
And finally, tell Git to commit the resolution, and continue rebasing any more commits in the rebased branch:
git rebase --continue
Ctrl + x
,
y
, Enter
)Let’s see what that’s done to the repo:
git graph
Add contributing guide
commits:
origin/contributing-guide
still points tocontributing-guide
now points to
origin/main
What happens if we try and push our branch:
git push origin contributing-guide
Because we do actually want to get rid of the old commits, we can destructively update the remote with a force push:
git push --force origin contributing-guide
git graph
before force pushing.Now that the push has succeeded,
origin/contributing-guide
will be on the same commit as
your local branch, and you should no longer see the old commit in the
graph:
git graph
Now that the merge conflict has been resolved, we can complete the pull request.
Merge pull request
Confirm merge
Now let’s fetch the results of that merge from the
origin
remote to our local repo:
git fetch
git graph
origin/main
contains a new merge commitmain
You’ll now need to switch back to your main branch and “fast-forward”
merge it to the latest state of origin/main
- just like we
did last week:
git switch main
git merge --ff-only origin/main
Now you can understand why we use the “fast-forward only” option:
Finally, now that our branch is merged, we should tidy-up by deleting our feature branch on the remote and locally:
# Delete the remote branch
git push -d origin contributing-guide
# Delete the local branch
git branch -d contributing-guide
Finally, a last look at the graph will show that we have removed the
branch, and are just left with main
:
git graph