Earlier we covered the basics of how git pull integrates upstream changes… Now let‘s dive into some more advanced pull techniques for full-stack developers managing complex Git workflows.
To Rebase or To Merge: Weighing Different Pull Strategies
By default, git pull
merges commits from the fetched branch directly into the target branch via a merge commit. But an alternative approach is to rebase the changes instead.
So what is rebasing and when should you use it?
An Introduction to Git Rebasing
Rebasing is the process of taking a sequence of commits…
Comparing Merge and Rebase Pull Approaches
Let‘s look at how each pull method affects the final commit history…
Customizing Pull to Always Rebase
Rather than choosing merge vs rebase manually for each pull, we can configure pull to rebase by default:
git config --global pull.rebase true
Now all pulls will replay commits onto the base branch with --rebase
instead of --merge
.
Automating Quality Control with Pull Requests
Beyond directly running git pull
from branches, another way to bring in upstream changes is through pull requests (PRs). PRs provide code review and quality control between merging branches.
Using PR Checks to Prevent Broken Builds
Unlike raw git pull commands, with PRs we can run automated checks before allowing upstream merges:
Some examples include:
- Unit test verification
- Static analysis like linters
- Checking for compile errors
Hooking PR Pipelines into CI/CD Workflows
An advantage of PR-based workflows is each branch acts as a staging environment for validating changes…
Special Considerations for Pulling with Submodules
Git submodules allow embedding external repositories inside a parent repository…
While useful, submodules pose unique challenges when pulling upstream changes.
Fetching the Latest Commit SHAs
The first step is always to fetch the latest SHA references of the submodule projects:
git submodule update --remote
This doesn‘t merge updates but retrieves pointers for the child repos.
Updating Submodules to Match SHAs
Once the SHAs are fetched, we can pull changes within each submodule:
git submodule foreach git pull origin master
This will update all submodules to align with the SHAs from the parent repo.
Pitfalls to Avoid
Without running the submodule update commands, pulls in the parent repository can leave projects out of sync:
Troubleshooting Tricky Git Pull Scenarios
Like any complex Git workflow, there are bound to be tricky situations and outright failures from time to time. Here are some battle-tested troubleshooting techniques…
Recovering from a Bad Pull with Reflog
In case you pull changes that break the codebase, Git provides an escape hatch to revert back in time…
<introduce git reflog, example undoing bad pull>
Resolving Merge Conflicts
The bane of any Git user is running into merge conflicts during pull:
Auto-merging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.
Here are methods for troubleshooting resolution:
- Review conflict markers
>>>
and<<<
- Temporarily revert problematic commits
- Lock down upstream branch until conflict addressed
What to Do with Diverged Branches
Sometimes upstream changes take the codebase in a radically different direction…
… Additional sections with more data, examples, and insider advice …