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.