Skip to main content

Advanced Git Concepts

Welcome to the Advanced Git Concepts tutorial β€” where you’ll move beyond basic commands and learn how professionals handle complex workflows, cleaner histories, and recovery techniques. By the end of this section, you'll confidently use commands like rebase, cherry-pick, tag, reflog, and more to manage any situation in Git.

1. Rebasing β€” Cleaner Commit History​

Rebase helps you rewrite commit history by placing your commits on top of another branch’s latest changes.

Example:​

# Switch to your feature branch
git checkout feature-branch

# Rebase it with the main branch
git rebase main

Instead of merging (which creates an extra merge commit), rebasing keeps history linear β€” perfect for clean commit timelines.


warning

Never rebase public branches that others are working on β€” it rewrites commit history.

2. Cherry-Picking Commits​

Cherry-pick allows you to apply specific commits from one branch to another without merging everything.

Example:​

# Copy a commit from another branch
git cherry-pick <commit-hash>

Use this when you want to move a single fix or feature to another branch without merging unrelated changes.

3. Tagging Versions (Releases)​

Tags are references that mark specific commits β€” often used for version releases.

Create a lightweight tag:​

git tag v1.0.0

Create an annotated tag:​

git tag -a v1.0.0 -m "Initial stable release"

Push tags to remote:​

git push origin --tags

Tags are ideal for marking milestones in projects or generating release versions on GitHub.

4. Submodules β€” Managing Nested Repositories​

Submodules allow you to embed one Git repository inside another β€” useful for managing libraries or dependencies.

Example:​

# Add a submodule
git submodule add https://github.com/user/library.git lib/

# Initialize and update
git submodule init
git submodule update

Use submodules when you need an external repository but want to control its version manually.

5. Squashing Commits​

Squash combines multiple commits into one β€” keeping your history neat and readable before merging.

Example (Interactive Rebase):​

git rebase -i HEAD~3

This opens an editor β€” change pick to squash for the commits you want to merge.

tip

Ideal for cleaning up β€œWIP” commits before submitting a pull request.

6. Git Hooks β€” Automate Actions​

Git Hooks are scripts that automatically run when certain Git events occur (like committing or pushing).

Example:​

  • .git/hooks/pre-commit β€” runs before each commit.
  • .git/hooks/post-merge β€” runs after a merge.

You can use hooks to:

  • Run tests before committing
  • Format code automatically
  • Enforce commit message rules

Try tools like Husky to manage Git hooks in JavaScript projects easily.

7. Reflog β€” Recover Lost Commits​

Reflog records every change to your HEAD β€” perfect for recovering commits after resets or mistakes.

Example:​

git reflog

Find the commit hash and restore it:

git checkout <commit-hash>

Lifesaver

Even if you accidentally delete branches or commits, reflog can bring them back.

Use git bisect to find which commit introduced a bug.

Example:​

git bisect start
git bisect bad
git bisect good <commit-hash>

Git checks out commits one by one until it identifies the problematic one.

Perfect for debugging large projects efficiently.

9. Amend Last Commit​

Fix small mistakes (like typos or missed files) without creating a new commit.

git add .
git commit --amend -m "Updated commit message"

Keep commit history clean without unnecessary β€œfix” commits.

10. Worktrees β€” Multiple Working Directories​

Worktrees allow you to work on multiple branches simultaneously without switching context.

git worktree add ../feature feature-branch

Now you can work on feature-branch in another folder while keeping main active elsewhere.

Advanced Git Flow Tips​

TechniqueUse Case
RebaseClean, linear history
Cherry-pickMove specific commits
SquashCombine multiple commits
HooksAutomate tasks
ReflogRecover deleted commits
WorktreeManage multiple branches easily

Example Workflow​

Here’s how professionals maintain a clean project history:

  1. Create feature branch

    git checkout -b feature/login
  2. Make several commits

  3. Squash and rebase before merging

    git rebase -i main
  4. Push clean history to remote

    git push origin feature/login
Git Tips
  • Use Git aliases for faster workflows:

    git config --global alias.st status
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.cm "commit -m"
  • Add color output for better readability:

    git config --global color.ui auto
  • Use git log --oneline --graph to visualize your history beautifully.

Coming Next​

Now that you’ve mastered the advanced Git toolkit, move on to: πŸ‘‰ GitHub Actions β€” Automate Your Workflows

πŸ“š Additional Resources​

Summary​

ConceptDescription
RebaseReapply commits for clean history.
Cherry-pickApply specific commits from another branch.
SquashCombine multiple commits.
SubmodulesManage external repositories.
HooksAutomate Git actions.
ReflogRecover lost commits.
WorktreesWork on multiple branches at once.

πŸ’™ This tutorial is part of the CodeHarborHub Git & GitHub learning series β€” helping you build professional version control skills used in real-world development.