Skip to main content
Knowledge Hub

Advanced Git Techniques

Learn advanced Git commands and workflows

Advanced Git Techniques

Master advanced Git features to become more productive and handle complex scenarios.

Stashing Changes

What is Stashing?

Stashing allows you to temporarily save changes without committing them.

Basic Stash

git stash

Stash with Message

git stash save "Work in progress on feature X"

List Stashes

git stash list

Apply Stash

git stash apply

Or apply and remove:

git stash pop

Rebasing

What is Rebasing?

Rebasing rewrites commit history by moving or combining commits.

Interactive Rebase

git rebase -i HEAD~3

This allows you to:

  • Reword commit messages
  • Squash commits together
  • Reorder commits
  • Drop commits

Rebase vs Merge

  • Rebase: Creates a linear history
  • Merge: Preserves branch structure

Cherry-picking

Pick Specific Commits

git cherry-pick <commit-hash>

Cherry-pick Multiple Commits

git cherry-pick <commit1> <commit2> <commit3>

Tags

Creating Tags

git tag v1.0.0

Annotated Tags

git tag -a v1.0.0 -m "Release version 1.0.0"

Pushing Tags

git push origin v1.0.0

Or push all tags:

git push origin --tags

Hooks

What are Git Hooks?

Hooks are scripts that run automatically at certain points in the Git workflow.

Pre-commit Hook

Create .git/hooks/pre-commit to run checks before commits.

Post-commit Hook

Runs after a commit is made.

Submodules

Adding a Submodule

git submodule add https://github.com/user/repo.git path/to/submodule

Updating Submodules

git submodule update --init --recursive

Reflog

View Reflog

git reflog

The reflog shows a history of all HEAD movements, allowing you to recover “lost” commits.

Recover Lost Commits

git reflog
git checkout <commit-hash>

Bisect

Find the Bug

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

Git will help you find which commit introduced a bug.

Worktrees

Create a Worktree

git worktree add ../project-feature feature-branch

This allows you to have multiple working directories for the same repository.

Advanced Merging

Merge Strategies

git merge -s ours branch-name
git merge -s theirs branch-name

Squash Merge

git merge --squash feature-branch

Combines all commits from the branch into a single commit.

Conclusion

These advanced techniques will help you handle complex scenarios and work more efficiently with Git. Practice these commands to become a Git power user!