Unboxing GIT Fundamentals: Part 6 of 10

Anand
5 min readJun 10, 2024

--

Part 6: Advanced Git Commands and Techniques

Welcome back to our Git learning series! So far, we’ve covered the basics of Git, branching and merging, remote repositories, and collaboration workflows. In this part, we’ll delve into some advanced Git commands and techniques that can help you manage your code more effectively. By mastering these advanced commands, you’ll be able to handle complex scenarios and streamline your workflow.

Stashing Changes

Stashing allows you to save changes in your working directory temporarily without committing them. This is useful when you need to switch branches or pull in updates but aren’t ready to commit your current changes.

  1. Stash your changes:
git stash

This command saves your changes and reverts your working directory to the last commit.

  1. List stashes:
git stash list

This command displays a list of all stashes.

2. Apply a stash:

git stash apply

This command applies the most recent stash to your working directory. To apply a specific stash, use:

git stash apply stash@{index}

3. Pop a stash:

git stash pop

This command applies the most recent stash and removes it from the stash list.

4. Drop a stash:

git stash drop

This command removes the most recent stash without applying it. To drop a specific stash, use:

git stash drop stash@{index}

Rebasing

Rebasing is a powerful tool for maintaining a clean commit history by moving or combining commits. It’s especially useful for integrating changes from the main branch into your feature branch.

  1. Rebase your branch onto another branch:
git rebase <base-branch>

For example, to rebase your feature-branch onto main:

git checkout feature-branch git rebase main

2. Interactive rebase:

git rebase -i <base-branch>

This command allows you to edit, combine, or reorder commits. For example:

git rebase -i main

This opens an editor where you can specify how each commit should be handled.

3. Continue a rebase after resolving conflicts:

git rebase --continue

Use this command after resolving any conflicts during a rebase.

4. Abort a rebase:

git rebase --abort

This command cancels the rebase operation and returns your branch to its previous state.

Cherry-Picking

Cherry-picking allows you to apply specific commits from one branch to another. This is useful when you want to incorporate particular changes without merging an entire branch.

  1. Cherry-pick a commit:
git cherry-pick <commit-hash>

For example, to cherry-pick a commit with the hash abc123:

git cherry-pick abc123

2. Cherry-pick a range of commits:

git cherry-pick <start-commit-hash>^..<end-commit-hash>

This command applies all commits in the specified range.

Resetting

Resetting allows you to undo changes by moving the HEAD to a specific commit. There are three types of resets: soft, mixed, and hard.

  1. Soft reset:
git reset --soft <commit-hash>

This command moves the HEAD to the specified commit and keeps the changes in the staging area.

  1. Mixed reset (default):
git reset <commit-hash>

This command moves the HEAD to the specified commit and keeps the changes in the working directory but not in the staging area.

2. Hard reset:

git reset --hard <commit-hash>

This command moves the HEAD to the specified commit and discards all changes in the working directory and staging area.

Reflog

The reflog is a log of all the changes made to the HEAD of your repository, including commits, resets, and checkouts. It’s useful for recovering lost commits or changes.

  1. View the reflog:
git reflog

This command displays the history of HEAD changes.

2. Recover a lost commit:

git checkout <reflog-hash>

Use the hash from the reflog to recover a specific commit.

Bisecting

Bisecting is a binary search method for finding the commit that introduced a bug. It helps narrow down the range of commits to identify the problematic one.

  1. Start bisecting:
git bisect start

2. Mark the current commit as bad:

git bisect bad

3. Mark a known good commit:

git bisect good <commit-hash>

4. Git will checkout a commit for testing: Test the code and mark it as good or bad.

git bisect good

or

git bisect bad

5. Repeat until the problematic commit is identified: Once identified, end the bisect process.

git bisect reset

Example Workflow

Here’s an example workflow demonstrating advanced Git commands:

  1. Stash changes:
git stash

2. Pull latest changes:

git pull origin main

3. Rebase your branch:

git rebase main

4. Cherry-pick a specific commit:

git cherry-pick abc123

5. Resolve conflicts during rebase:

# After resolving conflicts 
git add .
git rebase --continue

6. Use reflog to recover a lost commit:

git reflog 
git checkout <reflog-hash>

7. Use bisect to find a bug:

git bisect start 
git bisect bad
git bisect good <commit-hash>
# Test and mark as good or bad until the problematic commit is found
git bisect reset

Best Practices for Advanced Git Usage

To make the most of these advanced Git techniques, follow these best practices:

  • Use stashing for temporary changes: Stash changes when you need to switch contexts or pull updates without committing partial work.
  • Prefer rebasing for a clean history: Rebase instead of merging to maintain a linear commit history, especially for feature branches.
  • Cherry-pick carefully: Use cherry-picking to apply specific changes, but be cautious of dependencies between commits.
  • Reset with caution: Understand the implications of each type of reset to avoid losing important changes.
  • Utilize reflog: Use the reflog to recover lost changes or commits.
  • Bisect effectively: Use bisect to quickly identify problematic commits, especially in large codebases.

Conclusion

In this part of our Git learning series, we’ve explored advanced Git commands and techniques, including stashing, rebasing, cherry-picking, resetting, reflog, and bisecting. These powerful tools can help you manage complex scenarios and maintain a clean and efficient workflow.

In the next part, Unboxing GIT fundamentals: Part 7 of 10, we’ll discuss best practices for managing repositories, including maintaining a clean history, using hooks, and automating workflows. Stay tuned, and happy learning!

Explore All Content of Unboxing GIT Fundamentals

  1. Part 1 of 10: Introduction to Git and Version Control
  2. Part 2 of 10: Basic Git Commands and Operations
  3. Part 3 of 10: Branching and Merging in Git
  4. Part 4 of 10: Remote Repositories in Git
  5. Part 5 of 10: Collaboration Workflows in Git
  6. Part 6 of 10: Advanced Git Commands and Techniques
  7. Part 7 of 10: Git Best Practices for Repository Management
  8. Part 8 of 10: Exploring Git Workflows — Git Flow, GitHub Flow, and GitLab Flow
  9. Part 9 of 10: Integrating Git with CI/CD Pipelines on GitHub and GitLab
  10. Part 10 of 10: Debugging and Troubleshooting Common Git Issues

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Hidden Secrets of Git : That You Never Knew

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

--

--