So far, we have covered the basics of Git and GitHub, including installation, setup, basic commands, branching, merging, remote repositories, collaboration workflows, and GitHub features. But Git offers powerful advanced features that can improve our workflow, fix mistakes, automate tasks, and enhance security.
Interactive rebase (git rebase -i) lets us rewrite commit history by combining, splitting, editing, and deleting commits. It provides a flexible way to clean up the commit history before sharing it with others.
It is helpful for:
- Editing commit messages
- Reordering commits
- Combining multiple commits (squash)
- Deleting unnecessary commits
- Cleaning up commit history before pushing to GitHub
- Fixing commit messages for better clarity Merging multiple small commits into one.
-
Start Interactive rebase
git rebase -i HEAD~3
This open an editor showing the last 3 commits.
- Modify commits using these commands :
| Commands | Description |
|---|---|
pick |
keep the commit as is |
reword |
Use commit but edit the message |
edit |
modify the commit content |
squash |
Combine commit with the previous one |
fixup |
Combine commit with the previous one, discarding the message |
drop |
Remove commit |
- Apply changes and continue
After making changes, save and close the editor. If we selected edit or reword, Git will pause at that commit. We can make changes, amend the commit, and continue the rebase.
git add .
git commit --amend
git rebase --continue- Push the modified history
git push origin <branch-name> --forceNote: Be cautious while rewriting commit history, especially on shared branches. It can cause conflicts and confusion for collaborators.
Stashing lets you save uncommitted changes without committing them. It's useful when you :
- Need to switch branches but don't want to commit yet
- Want to temporarily save changes and come back later.
-
Save uncommitted changes
git stash
-
List saved stashes
git stash list
-
Apply the most recent stash
git stash apply
-
Apply a specific stash
git stash apply stash@{1} -
Remove stash after applying
git stash pop
-
Clear all stashes
git stash clear
A Submodule is a Git repository inside another repository. It's useful when:
- You want to include another project as a dependency
- You need multiple repositories in a single project
-
Add a submodules
git submodule add <repo-link> submodule-folder
-
Clone a repo with Submodules
git clone --recursive <repo-link>
-
Update submodules
git submodule update --init --recursive
-
Remove a submodule
git submodule deinit submodule-folder rm -rf submodule-folder git rm submodule-folder
Git hooks let you run scripts automatically before or after Git commands.
-
Create a pre-commit hook
nano .git /hooks/pre-commit
-
Add this script
#!/bin/sh if ! git log -1 --pretty=%B | grep -q '[^[:space:]]; then echo "Commit message cannot be empty!" exit 1 fi
-
**Make it executable
chmod +x .git/hooks/pre-commt
Now if someone tries to commit without a message, Git will stop them!
Cherry-picking allows you to apply a single commit from one branch to another without merging the entire branch.
-
Find the commit hash
git log --online
-
Apply the commit to another branch
git checkout target-branch git cherry-pick <commit-hash>
Reflog helps recover lost commits by tracking every Git action.
-
View reflog history
git reflog
-
Restore a lost commt
git checkout <commit-hash>
-
Reset branch to a previous state
git reset --hard <commit-hash>
Git bisect helps find the commit that introduced a big using a binary search.
-
Start bisecting
git bisect start git bisect bad # Mark the current commit as bad git bisect good <commit-hash> # Mark an earlier commit as good
-
Test the commit and make it as good/bas
git bisect good # if the commit is working git bisect bad # if the commit contains the bug
-
Continue until Git finds the faulty commit
-
Exit bisect mode
git bisect reset
- Enable Two-Factor Authentication (2FA)
- Use SSH keys instead of passwords
- Scan repositories for secrets
Now you have a powerful Git toolkit for:
- Rewrite history (rebase)
- Saving work temporarily (stash)
- Managing multiple repositories (submodules)
- Automating tasks (hooks)
- Applying selective changes (cherry-pick)
- Recovering lost commits (reflog)
- Finding bugs quickly (bisect)
- Enhancing security