DEV Community

Fanny Nyayic
Fanny Nyayic

Posted on • Originally published at fanny.hashnode.dev on

Advanced Git Techniques

As developers progress in their careers, they often need to deepen their understanding of Git, the distributed version control system that has become an integral part of modern software development.

Advanced Git techniques can enhance productivity, improve workflow efficiency, and help maintain a clean project history. In this article, we'll explore some powerful Git commands and concepts that go beyond the basics.

Git Rebase

Git Rebase is a command that allows you to move or combine a sequence of commits to a new base commit. Rebasing is useful for cleaning up commit history before merging changes into another branch. It can also be used to integrate changes from the base branch into a feature branch without creating merge commits.

Interactive Rebasing

Interactive rebasing takes this a step further by allowing you to alter commits as you rebase them. You can squash commits, split them, reword commit messages, or drop them entirely. This is done by using git rebase -i followed by the commit hash or reference from which you want to start the rebase.

git rebase -i HEAD~5
Enter fullscreen mode Exit fullscreen mode

This command starts an interactive rebase session for the last 5 commits, allowing you to reword, edit, squash, or drop commits.

Git Reflog

The Git Reflog is an essential tool for recovery. It keeps a log of where your HEAD and branch references have been. It can help you find lost commits, undo rebase, reset, or other non-fast-forward actions that might seem to have erased your work.

Git Stash

Git Stash is a handy command for saving uncommitted changes in your working directory. You can then switch branches without committing to work in progress. Later, you can apply the stashed changes back onto your working branch.

git stash save "work in progress on feature X"
Enter fullscreen mode Exit fullscreen mode

This stashes your changes with a descriptive message so you can easily identify it later.

Applying a Stash

git stash apply stash@{0}
Enter fullscreen mode Exit fullscreen mode

This applies the changes from the specified stash entry back onto your working directory.

Git Cherry-Pick

Cherry-picking allows you to select specific commits from one branch and apply them to another branch. This is useful when you want to apply bug fixes or features that were developed in a separate branch without merging all the changes from that branch.

git cherry-pick 4a2b3c4d
Enter fullscreen mode Exit fullscreen mode

This applies the commit with the hash 4a2b3c4d onto your current branch.

Git Bisect

When trying to find the source of a bug, git bisect can be a lifesaver. It uses a binary search algorithm to quickly and efficiently find the commit that introduced a bug by marking known good and bad commits.

Git then automatically checks out a commit halfway between the two, allowing you to test whether the issue is present and continue the search.

Starting a Bisect Session

git bisect startgit bisect bad # mark the current commit as badgit bisect good 4a2b3c4d # mark commit 4a2b3c4d as good
Enter fullscreen mode Exit fullscreen mode

Git will then check out a commit for you to test. After testing, mark it

Advanced Searching

Git provides powerful tools for searching through your project's history, which can help you pinpoint when and where changes were made.

Git Grep

git grep allows you to perform text searches across all files in your repository's history. This is particularly useful for finding where a function or variable is used or introduced.

Git Log Searches

git log supports a variety of powerful search options. For instance, you can use --grep to search for commit messages, --author to filter commits by a specific author, or --since and --until to search in a specific date range. Combining these can help you narrow down the list of commits to review.

Worktree Management

Git worktrees allow you to have multiple working trees attached to the same repository. This can be helpful when you want to work on two branches simultaneously without having to switch back and forth.

Submodules and Subtrees

When working with dependencies or libraries that are also Git repositories, you might use Git submodules or subtrees.

Git Submodules

Submodules allow you to include other Git repositories as a subdirectory of your repository. They are useful for separating a project into smaller repositories that can be developed independently.

Git Subtrees

Subtrees are an alternative to submodules that do not require a separate repository. Instead, they allow you to insert any repository as a subdirectory of your current repository, maintaining the ability to make changes to it without leaving your main project.

Refspec and Remote Tracking

Understanding refspec can be crucial when you want to have fine-grained control over what gets pushed or fetched to and from remote repositories. A refspec defines how references should be mapped between the local and remote repositories.

Git Hooks

Git hooks are scripts that run automatically before or after certain Git commands are executed, such as commit, push, and receive. They can be used to enforce code standards, run tests, or integrate with continuous integration systems.

Git Attributes

Git attributes are a way to assign special attributes to paths in a repository. They can be used to customize how Git handles certain files, for instance, by defining merge strategies, diff algorithms, or specifying files that should always be treated as binary.

Git Alias

Creating aliases for frequently used Git commands can save time and keystrokes. You can set up shortcuts for complex commands or sequences of commands that you use regularly.

Advanced Merge

Merging in Git can sometimes lead to conflicts that need to be resolved manually. Advanced users can use a combination of Git tools to facilitate this process.

Merge Strategies

When performing a merge, you can specify a merge strategy with -s or --strategy. For example, the recursive strategy is the default, but you can use ours to always prefer changes from the current branch or theirs to prefer changes from the branch being merged.

git merge feature-branch -s recursive -X ours
Enter fullscreen mode Exit fullscreen mode

Merge Tools

For resolving conflicts, you can use graphical merging tools like kdiff3, meld, or p4merge. You can configure Git to use these tools with:

git config --global merge.tool kdiff3
Enter fullscreen mode Exit fullscreen mode

Manual Conflict Resolution

When a conflict occurs, you'll need to manually edit the files to resolve the issues. Conflicted areas are marked in the file like so:

<<<<<<< HEADyour changes here=======their changes here>>>>>>> feature-branch
Enter fullscreen mode Exit fullscreen mode

You'd edit the file to remove the conflict markers and make the necessary adjustments to integrate the changes from both branches.

Top comments (0)