- Description
- Prerequisites
- Background
- Git Workflow
- Cherry-Picking a Commit
- Cherry-Picking from a Remote Branch
- Rebasing a Branch
- Rebasing with a Remote Branch
- Resources
This lab focuses on advanced Git functionalities including branching strategies, rebasing, cherry-picking, and handling merge conflicts. You'll learn how to efficiently manage and navigate complex git histories, maintain a clean and functional codebase, and utilize Git workflows like Git Flow for collaborative software development.
Before starting this lab, you should have:
- Basic understanding of Git commands (
git add
,git commit
,git push
,git pull
) - Git installed on your machine
- Access to a Git repository (e.g., GitHub, GitLab) where you can practice these concepts
Git Flow is a branching model that defines a strict branching structure designed around project releases. Key concepts include:
-
The
master
branch stores the official release history. -
The
develop
branch serves as an integration branch for features. -
Feature branches are used to develop new features and are merged back into the
develop
branch. -
Releases are based on the state of the
develop
branch. Once ready,develop
is merged intomaster
and tagged with a release number. -
Hotfixes branches are created from
master
to quickly address issues in production releases. -
Modifications to the codebase are represented as commit objects.
-
The git history is an immutable linked list of commits.
-
Feature branches are merged into develop.
-
Once develop has been tested, develop is merged into master.
-
If there are issues with a feature, we can revert the feature branch in develop.
-
If there are issues with a release, we can revert the merge commit in master.
Here's a git flow diagram of "merge"
For this lab, pull requests would have the diagram of "merge"
For branching, we draw an arrow out of the commit from the branch that we are branching off of, resulting in two separate streams.
You can make a git workflow diagram using software such as: https://www.eraser.io/use-case/git-diagram
You can also practice and get familiar using a simulator called learn git branching: https://learngitbranching.js.org
Cherry-picking allows you to apply changes from specific commits from one branch to another.
-
Find the Commit Hash: Identify the commit you want to apply.
git log --oneline
-
Switch to the Target Branch: Move to the branch where you want the changes.
git checkout targetBranch
-
Cherry-Pick the Commit: Apply the commit.
git cherry-pick <commit-hash>
-
Resolve Conflicts: If there are conflicts, resolve them, then continue the cherry-pick.
git add . git cherry-pick --continue
To abort the operation:
git cherry-pick --abort
To cherry-pick a commit from a remote branch, you first need to make sure your local repository is aware of the remote branch's state.
-
Fetch Remote Changes: Update your local repo's information about remote branches.
git fetch origin
-
Cherry-Pick the Commit: Using the commit hash from the remote branch, apply it to your current branch.
git cherry-pick <commit-hash-from-remote-branch>
-
Handle Conflicts: Similar to local cherry-picking, resolve any conflicts and finalize the cherry-pick.
Remember, when working with remote branches, especially in a team environment, it's crucial to communicate your changes to avoid conflicts and ensure a smooth workflow.
Rebasing updates your branch with changes from another branch, typically used to integrate the latest changes from the main branch into your feature branch.
-
Update the Base Branch: Ensure the branch you're rebasing onto is up-to-date.
git checkout main git pull
-
Checkout Your Branch: Switch to your branch.
git checkout yourBranch
-
Rebase onto the Base Branch: Start rebasing.
git rebase main
-
Resolve Conflicts: Fix any conflicts and continue the rebase.
git add <file> git rebase --continue
To stop the rebase:
git rebase --abort
Rebasing your local branch with changes from a remote branch helps to maintain a linear project history and simplify future merges. This process is particularly useful for incorporating the latest updates from the main or development branch into your feature branch.
-
Fetch the Latest Changes: Before rebasing, you need to ensure your local copy of the remote branch is up-to-date.
git fetch origin
This command updates your local repository's knowledge of the remote branch without merging any changes into your current branch.
-
Checkout Your Feature Branch: Make sure you're working on your local branch that you want to rebase onto the remote branch.
git checkout featureBranch
Replace
featureBranch
with the name of your local branch. -
Rebase onto the Remote Branch: Perform the rebase operation using the updated remote branch reference.
git rebase origin/main
Replace
main
with the name of the remote branch you're rebasing onto. This command re-applies your local branch's commits on top of the latest commits from the remote branch. -
Resolve Any Conflicts: If the rebase process encounters conflicts, it will pause, and you'll need to resolve these conflicts manually. After resolving a conflict in a file:
git add <resolved-file> git rebase --continue
Repeat this process until all conflicts are resolved. If at any point you decide you want to stop the rebase and return to the original state:
git rebase --abort
-
Push Your Changes: After successfully rebasing, you might need to force push your changes to the remote repository, especially if you've already pushed your branch before rebasing.
git push origin featureBranch --force
Caution: Be careful with
git push --force
as it can overwrite history on the remote. It's recommended to use it only on branches where you are the sole contributor or after coordinating with your team.