Engineering practice: git, code, tests · Chapter 2 of 12 · Craft · new chapter every Sunday night
By the end of this chapter: Resolve a real conflict and recover from a mistake using the reflog.
The problem
You are a junior engineer on a two-week sprint for the payments team. Mid-sprint, you discover a flaky test that fails on the CI runner every night. You open a branch, add a one-line guard clause, and push. The CI pipeline passes, so you think you're done and type:
git commit -am "fix bug"
git push origin payment-guard-2024-09-15
However, when you try to merge your branch into the main branch, you encounter a conflict. The CI runner has also changed some of the same files, and now your changes are no longer compatible. You try to rebase your branch onto the main branch, but the rebase fails due to a conflict in the discount.py file. You're not sure how to resolve the conflict or recover from the failed rebase.
The conflict costs you time, as you have to spend hours resolving the issue instead of working on new features. You also worry about introducing bugs or losing changes during the conflict resolution process. You want to learn how to resolve conflicts and recover from mistakes using Git's reflog.
Before you start
To follow along with this chapter, you need to have Git installed on your system, and you should have completed the previous chapter on Git as a thinking tool. You should also have a basic understanding of Git branches and commits. Make sure you have the latest version of Git installed by running:
git --version
You should see a version number, such as 2.39.2. If you're using an older version, update to the latest version before proceeding.
Understanding conflicts and rebasing
When you rebase a branch, Git reapplies your commits on top of the main branch. However, if there are conflicts between your changes and the changes in the main branch, the rebase will fail. To resolve the conflict, you need to edit the conflicting files, stage the changes, and then continue the rebase.
In your case, the conflict is in the discount.py file. To resolve the conflict, you need to open the file, edit it to resolve the conflict, and then stage the changes using:
git add discount.py
Once you've staged the changes, you can continue the rebase using:
git rebase --continue
If you encounter any further conflicts, you'll need to repeat the process of editing the conflicting files, staging the changes, and continuing the rebase.
Build it
To resolve the conflict and recover from the failed rebase, follow these steps:
-
Check the conflict: Run
git statusto see which files are conflicting. You should see a message indicating that thediscount.pyfile is conflicting. -
Edit the conflicting file: Open the
discount.pyfile and edit it to resolve the conflict. You can use a merge tool or edit the file manually. -
Stage the changes: Run
git add discount.pyto stage the changes. -
Continue the rebase: Run
git rebase --continueto continue the rebase. -
Check the rebase: Run
git logto see if the rebase was successful. You should see a linear history with your corrected messages.
When this breaks
There are several ways that conflict resolution and rebasing can break. Here are a few common scenarios:
- Failed rebase: If the rebase fails due to a conflict, you'll need to resolve the conflict and continue the rebase.
- Lost changes: If you accidentally discard changes during the conflict resolution process, you can use Git's reflog to recover the lost changes.
- Introduction of bugs: If you introduce bugs during the conflict resolution process, you can use Git's bisect feature to identify the commit that introduced the bug.
To recover from a failed rebase, you can use Git's reflog to reset the branch to a previous state. For example:
git reflog
git reset --hard HEAD@{1}
This will reset the branch to the previous state, allowing you to try the rebase again.
What it costs
Using Git's reflog to recover from mistakes comes with some costs. Here are a few things to consider:
- Time: Recovering from mistakes can take time, especially if you're not familiar with Git's reflog.
- Complexity: Using Git's reflog can add complexity to your workflow, especially if you're working with a large team.
- Risk of data loss: If you're not careful, you can lose changes or introduce bugs during the conflict resolution process.
However, the benefits of using Git's reflog to recover from mistakes far outweigh the costs. By using Git's reflog, you can recover from mistakes quickly and easily, without losing changes or introducing bugs.
In the interview
When interviewing for a junior engineer position, you may be asked questions about conflict resolution and rebasing. Here's an example of how the conversation might go:
Interviewer: "Can you tell me about a time when you had to resolve a conflict during a rebase?"
Weak answer: "Uh, yeah, I've had to do that before. I just, like, edited the file and staged the changes, and then continued the rebase."
Strong answer: "Yes, I've had to resolve conflicts during rebasing several times. One time, I was working on a feature branch and I had to rebase it onto the main branch. There was a conflict in one of the files, so I had to edit it to resolve the conflict. I used Git's reflog to recover from a failed rebase, and I was able to successfully merge my branch into the main branch. I've learned that it's essential to be careful when resolving conflicts and to use Git's tools to recover from mistakes."
Follow-up question: "Can you walk me through your process for resolving conflicts during a rebase?"
Your tasks
To practice conflict resolution and rebasing, try the following exercises:
- Create a new branch and make some changes to a file.
- Rebase the branch onto the main branch, introducing a conflict.
- Resolve the conflict and continue the rebase.
- Use Git's reflog to recover from a failed rebase.
- Repeat the process several times to practice conflict resolution and rebasing.
Remember to verify your success by checking the Git log and status after each exercise. If you get stuck, don't hesitate to ask for help.
Your tasks this week
Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.
Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.
Engineering practice: git, code, tests
Chapter 2 of 12. New chapter every Sunday night.
Next: Code review: what to look for, in what order.
· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe
Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.
Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.
Top comments (0)