Git is a crucial collaborative tool that virtually every developer uses. Understanding Git isn't just a technical proficiency—it's a key to fostering successful collaboration in a development environment. By mastering Git, you position yourself as an invaluable team member capable of navigating collaborative projects with ease.
Git is a distributed version control system that enables multiple developers to collaboratively work on projects, track changes, and manage code efficiently. Sometimes using Git goes beyond the regular commit, pull, or push commands most developers know. In this article, you’re going to learn more about Git and upgrade your skills so that you can collaborate with your team like a pro!
Prerequisites
Basic knowledge of Git, as this article is focused on advanced concepts.
Advanced Git Commands
In this section, we will explore more advanced Git commands. These commands are used for different functionalities while using git with your team.
Rebasing
This is one of the two main Git techniques used to integrate changes while working off a main branch. Rebasing is a very powerful technique that every developer should know because it helps to maintain a clean, linear commit history and incorporate upstream changes effectively. Let's explore how.
Rebasing is a way to integrate changes by moving your entire branch to a new starting point, called a base commit. Imagine you're on a team, and you've branched off from the main project to work on your own changes. Meanwhile, your teammates added more features to the main branch of the project. Now, you want to include those new features in your work.
Rebasing is like adjusting your work to fit in with the latest changes. It's like saying, “Let me place my work right where these new things are, so everything aligns nicely.” Essentially, you're moving your branch to a new starting point, where all those relevant new additions have been included.
Below is a diagrammatic representation of rebasing:
1. Starting point: Assume you have a main branch and a feature branch created from it.
2. Rebasing the feature into main: when you perform a rebase on the feature branch with the main branch as the new base, it replays each of your feature branch commits on top of the main branch.
Each commit (X, Y , Z ) from your feature branch is applied on top of the latest commit in the main branch, creating new commits (X’ , Y’ , Z’ ).
There are two main types of rebasing in Git:
- Regular (Non-Interactive) Rebasing: This method applies your changes to the specified base commit, making it look as if you had created your branch from that different commit.
-
Interactive Rebasing: This version allows you to interactively choose how individual commits are applied. It opens an interactive text editor where you can
pick
,edit
,squash
,reorder
, and more during the rebase process. This gives you fine-grained control over your commit history.
Regular rebasing is more straightforward, where you move your entire branch to a new base commit, while interactive rebasing provides a more flexible and controlled environment where you can manipulate individual commits during the process. Both types of rebasing are useful in different scenarios depending on your needs for maintaining a clean and organized commit history.
Git Rebase vs Git Merge
Now, let's explore merging, which is the other method of integrating changes into the main branch when using Git. Merging is a process of incorporating changes from a feature branch into the main branch, resulting in an inevitable merge commit. Below is a diagrammatic explanation of merging:
1. Initial State: We have a main branch and a feature branch starting from the same commit point.
2. Direct Merging: To merge changes from feature branch into main branch, use the following commands.
Git performs a direct merge creating a new merge commit (M) that combines the changes from both branches.
3. Fast-forward Merging: If there was no divergence in the branches (if all the commits in the feature branch are already in the main branch), a fast-forward merge occurs, moving the main branch pointer forward.
Then when the merge happens a more linear history is gotten and this is the result.
Advantages of Using Rebase over Direct Merging
While merging is a good method for integrating changes and collaborating in version control systems, there are potential disadvantages associated with it.
- Cluttered History: Merging creates additional merge commits, cluttering the project history.
- Potential for Conflicts: Merge conflicts can arise, requiring manual resolution and time.
- Less Predictable History: Frequent merging can result in a less linear and more complex project history.
Rebasing can be a better option if you aim for a cleaner and more customizable project history. It adds each commit one by one to the specified base, making it easier to track project changes over time. This aids in debugging, allowing you to easily pinpoint the source of issues. In contrast, merging combines two branches in one merge commit, making it harder to spot changes in the project history.
It is recommended to rebase before merging to achieve a Fast Forward merge, which avoids creating a merge commit.
Note: Do not use rebasing on a public branch. Rebasing also rewrites your project history by changing the base commit origin. Ensure that this aligns with your project's requirements.
Cherry Picking
Cherry-picking is a Git feature that allows you to select and apply specific commits from one branch to another. It's like plucking individual cherries (commits) from one tree (branch) and placing them onto another.
How Cherry Picking Works
- Identify the Target and Source Branches: Decide which branch you want to apply the specific commit(s) to (target branch), and identify the commit(s) you want to cherry-pick from another branch (source branch).
-
Find the Commit Hash: Locate the commit hash of the specific commit you want to cherry-pick. This can be obtained from the commit history using
git log
. -
Navigate to the Target Branch and Cherry-pick the Commit: Use the
git cherry-pick
command followed by the commit hash on the target branch. Git creates a new commit with the changes from the cherry-picked commit. If there are no conflicts, the process is straightforward.
Cherry Picking Tips
- Commit ID Change: The cherry-picked commit gets a new commit ID, as it's essentially a new commit with the same changes.
- Avoid Cherry Picking Merge Commits: Cherry picking merge commits can lead to unexpected results, as it might not capture the entire context of the merge.
- Avoid Over-usage: Don't use cherry picking in place of rebasing or merging, as it could lead to duplicate commits
Recovering Lost Commits with Reflog
Git reflog
pays close attention to changes in branch references. In Git, a branch reference is a pointer or label that points to a specific commit in your project's history. It's like a name given to the latest commit in a branch. When you create a new branch, Git generates a reference for that branch. As you make new commits, the branch reference is continually updated to point to the latest commit.
Branch references are dynamic and advance as you make new commits. They are essential for keeping track of different lines of development in your project. When you switch between branches or create new ones, Git updates these references to reflect the current state of each branch.
The primary purpose of reflog
is to act as a safety net. Suppose you delete a branch, and later realize you shouldn't have. Git reflog
will show you a list of every time your branch pointer was moved (i.e., every time your reference was changed or updated), even if it's no longer visible in the current view. You can then easily recover the branch by going back to a specific commit.
Use Cases of Reflog and Commands
-
Recovering Lost Commits: If you accidentally reset a branch to an earlier commit and lose commits,
reflog
can help recover them.
(2) is used to reset your project to the desired commit by providing a specific commit reference. Your working directory will match the state of your project at that specified commit. Replace n
with the number corresponding to the desired reflog
entry.
-
Recovering Deleted Branches: If a branch is deleted, its
reflog
entries remain for some time. You can recover the branch usingreflog
.
-
Inspecting History: The
reflog
provides a detailed history of branch movements and changes. This is helpful for auditing and understanding the project's evolution.
More on Reflog
-
Limited Time Window: Reflog entries are kept for a limited time. The default is 90 days, though you can adjust this through the
reflogExpireUnreachable
configuration option. This option determines how long Git retains reflog entries before they become eligible for garbage collection. - Local History: Reflog is local to your repository and is not shared with others when you push changes.
Stashing
Stashing is a process of saving uncommitted changes. Typically, when working with Git, you need to stage your files and then commit them. However, if you need to switch to another branch to fix a bug while your current branch isn't ready for a commit, you can use stashing to save your changes temporarily. This allows you to switch branches, address the bug, and then later reapply your saved changes when you return to your original branch.You use the git stash
command to save changes on the current branch to the stash.
How to Reapply Saved Changes while Stashing
- Git stash apply: This applies the changes saved to the stash onto the current working directory and leaves the stash intact.
- Git stash pop: This applies the changes saved to the stash onto the current working directory and deletes the stash.
Stashing is useful when you want to work on multiple tasks concurrently, need to switch branches, or want to temporarily set aside changes without committing them. It's a handy feature to maintain a clean and organized commit history.
Worktrees
Git Worktrees are a feature that enables you to work on different branches simultaneously. While traditional branching methods exist, they may not offer the same level of simultaneous flexibility. Git Worktrees, as the name suggests, resembles a tree structure, with each branch represented in a separate file directory within your project repository.
Consider a scenario where you're actively developing a feature branch, and suddenly, you need to address a bug on a branch your teammate was working on. If your feature branch isn't ready for a new commit, the typical approach involves using git stash
to save uncommitted changes temporarily. This allows you to switch to the branch requiring the bug fix. However, the process of stashing and unstashing can become cumbersome over time, especially when frequently switching between branches.
Git Worktrees presents a solution to this challenge by allowing you to work on different branches simultaneously. This means you can seamlessly pick up your work wherever you left off without the need for repetitive stashing and unstashing. It simplifies the workflow, offering a more efficient way to manage multiple branches concurrently.
How Git Worktrees Work
-
Creation of Worktrees: You can create a new worktree using the
git worktree add
command. This command requires specifying the path for the new worktree and the branch you want to work on. - Independence of Worktrees: Each worktree operates independently of the others. This means you can switch branches in one worktree without affecting the state of other worktrees.
- Simultaneous Work on Different Branches: With worktrees, you can work on one branch in the main working directory and simultaneously work on a different branch in a separate worktree. This is beneficial when you need to make changes to multiple branches concurrently.
- Shared Repository: All worktrees share the same Git repository and object database. This means they have access to the same commit history, objects, and configurations. Changes made in one worktree are reflected in others.
- Preventing Branch Conflicts: Git worktrees are designed to prevent conflicts between worktrees. For example, if you attempt to create a new worktree on a branch that is already checked out in another worktree, Git will prevent it to avoid potential conflicts.
Git Worktree Commands
1. Create a Worktree
2. List Worktrees
3. Remove a Worktree
Git worktrees offer a powerful solution for managing multiple branches concurrently within a single repository. They support testing changes across branches, building different project versions, and facilitating branch reviews. However, users should be aware of shared repository overhead impacting disk space and potential limitations on certain filesystems due to reliance on symbolic links.
Git Bisect
Git bisect is a command that helps you find a specific commit in your project's history that introduced a bug or regression. It uses a binary search algorithm to efficiently narrow down the range of commits where the issue was introduced.
How Git Bisect Works
- Start Bisecting: Begin the bisecting process by running the command below.
-
Specify Bad and Good Commits: Mark the current commit as "bad" using
git bisect bad
. This identifies the commit where the bug is present. Mark an earlier commit (a point without the bug) as "good" usinggit bisect good <commit-hash>
. This sets a reference point for the bisect algorithm.
Testing and Marking: Git will automatically check out a commit for you to test. After testing, mark the commit as either "good" or "bad" using
git bisect good
orgit bisect bad
. Repeat this process until Git identifies the specific commit introducing the bug.Finish Bisecting: When you've found the problematic commit, end the bisecting process with
git bisect reset
.
Git bisect is a powerful tool for efficiently identifying the commit where a bug or regression was introduced. By systematically narrowing down the range of commits, it helps pinpoint the specific change responsible for the issue.
Advanced Git Concepts
In this section we will explore some advanced git concepts that can streamline your workflow.
Git Submodules
Git submodules are a feature in Git that allows you to include one Git repository as a subdirectory within another Git repository. This is useful when you want to include an external project or library in your own project while maintaining a separation of their version control history.
Here's a breakdown of how Git submodules work:
-
Adding a Submodule: To add a submodule to your repository, you use the
git submodule add
command followed by the URL of the external repository and the path where you want the submodule to be placed.
-
Initializing and Updating Submodules: After adding a submodule, you need to initialize it. This is done with the
git submodule init
command. It initializes the submodule and sets up the necessary configuration. To fetch the submodule content, you usegit submodule update
. This can be done in one command, it fetches the content of the submodule and checks out the commit specified in your main repository.
-
Working with Submodules: When you clone a repository with submodules or pull changes that include submodule updates, you often need to use the
--recursive
option to ensure that the submodules are also fetched and updated.
- Updating Submodules: If you make changes within a submodule or want to update it to the latest commit, you navigate into the submodule directory and pull the changes.
Then, you go back to the main repository and commit the submodule's updated state.
Git submodules allow you to incorporate external Git repositories into your project, maintaining a clear separation of their version control histories. They provide a way to include specific commits of external projects within your own repository, making it easier to manage dependencies and track changes across multiple projects.
Git Hooks
Git hooks are scripts that Git executes at specific points in the Git workflow. They allow you to automate or customize actions in response to certain events, enhancing your version control process. Git provides both client-side and server-side hooks.
Key points on Git Hooks
- Client-Side Hooks: They run on the developer’s machine and they include:
- Pre-commit: Executed just before committing changes. Useful for running code checks, linting, or ensuring commit message conventions.
- Prepare-commit-msg: Triggered after the pre-commit hook but before the commit message is finalized. It allows you to manipulate or pre-fill commit messages.
- Commit-msg: Invoked to check or manipulate the commit message after it's been written.
- Pre-receive: Executed on the server before updates are accepted. Useful for enforcing policies on the server side.
- Post-receive: Triggered on the server after updates have been accepted. Useful for notifications or triggering deployment scripts.
- Server-Side Hooks: They run on the Git server and they include:
- Pre-receive: Similar to the client-side pre-receive hook but executed on the server. It can enforce policies before accepting pushes.
- Update: Triggered for each branch that is being updated. Useful for performing checks on each branch update.
- Post-receive: Executed on the server after all updates have been accepted. It's commonly used for deployment or notification scripts.
Writing Git Hooks
Steps to writing Git hooks: Using Git hooks involves creating executable scripts that Git will automatically run at specific points in the Git workflow. Here's a step-by-step guide on how to use Git hooks:
Locate the
.git/hooks
Directory: In your Git repository, navigate to the.git/hooks
directory. This directory contains sample hook scripts, and you can create your own scripts there.Create or Customize a Hook Script: Create a new file with the name of the hook you want to use (e.g.,
pre-commit
,post-commit
, etc.). Make sure the file is executable by runningchmod +x <filename>
.
Open the script in a text editor and add your custom logic.
Customize Git Hooks for Your Workflow: Git hooks can be written in any scripting language (bash, Python, Ruby, etc.). Customize the hooks based on your team's workflow and requirements.
Test Your Hooks: Test your hooks by attempting to perform a Git operation that triggers the hook. For example, try making a commit to see if your
pre-commit
script runs.Hooks Lifecycle: Familiarize yourself with the lifecycle of Git hooks. For example,
pre-commit
runs before a commit is finalized, allowing you to perform checks or modifications.Distribute Hooks to Your Team: Share your hooks with your team. You can include them in the repository or provide instructions on how to set them up. Remember that hooks need to be executable, so you can use the
chmod +x
command to make them executable.
Note: Git hooks are local to each repository, so each team member needs to set up their hooks independently. It's essential to communicate and document the use of hooks in your development workflow to ensure consistency across the team.
Customizing Workflow with Git Hooks
Git hooks allow you to customize your Git workflow to fit your team's requirements. For example, you can enforce coding standards, prevent certain commits, or trigger automatic testing and deployment.
Uses of Git Hooks
- Preventing commits with debugger statements.
- Enforcing a specific commit message format.
- Running automated tests before accepting pushes.
- Automatically triggering a deployment process after a successful push.
Git hooks provide a powerful way to enhance collaboration, enforce policies, and automate tasks throughout the development lifecycle. They enable you to integrate Git seamlessly into your team's workflow and ensure consistency and quality in your version control process.
Resolving Git Conflicts
Resolving Git conflicts is a common task when working on collaborative projects. Conflicts occur when Git detects changes in the same part of a file from different branches, and it requires manual intervention to reconcile these differences. Here's a step-by-step guide on resolving Git conflicts:
Identify Conflicts: Git will notify you when a conflict occurs during operations like merging or pulling. You'll see a message indicating which files have conflicts.
Open the Conflicted File: Open the conflicted file in your code editor. Git marks the conflicting sections with special markers like
<<<<<<<
,=======
, and>>>>>>>
.Understand Conflict Markers: Git conflict markers, like
<<<<<<< HEAD
,=======
, and>>>>>>> incoming-branch
, designate conflicting changes between the current branch and another branch. The content between<<<<<<< HEAD
and=======
is from the local branch, while the content between=======
and>>>>>>> incoming-branch
is from the incoming branch. Manual resolution involves editing the file to decide how to integrate the conflicting changes, and after resolution, the markers are removed, and the file is staged for completion of the merge or rebase.Mark as Resolved: After resolving conflicts, mark the file as resolved. Use the
git add path/to/conflicted/file
command.Complete the Merge or Rebase: If you were in the process of merging, continue and complete the merge with the
git merge --continue
, If you were rebasing, continue the rebase with thegit rebase --continue
command.Commit the Changes and Verify the Resolution: After marking the file as resolved, commit the changes with
git commit -m "Resolve conflicts"
. Double-check that the conflicts are resolved by reviewing the changes and ensuring that the file now reflects the intended modifications.
Resolving Git conflicts requires careful consideration of the changes from different branches and making decisions on how to merge them. The key is to manually edit the conflicted files, removing conflict markers and ensuring the final result aligns with the desired changes.
Conclusion
We've explored a lot of advanced Git strategies, and at this point, you should have augmented your Git toolbox with more tools. Feel free to incorporate these strategies into your team collaboration where they fit. You can also check out the Git documentation for a deeper dive into each of these strategies. Keep an eye on our blog for more useful posts aimed at streamlining your workflow and enhancing productivity.