Practice advanced Git operations and techniques
Challenge yourself with advanced Git scenarios and complex workflow exercises.
These exercises will help you master advanced Git concepts through practical scenarios. You'll work with complex repository manipulations and learn to handle sophisticated Git operations.
These exercises involve history-rewriting operations. Always practice these on a test repository first, as they can permanently alter your Git history.
Clean up a feature branch's commit history before merging it into the main branch. You'll need to combine commits, edit commit messages, and reorder commits.
file.txt
$ git add file.txt $ git commit -m "Initial commit"
$ echo "Feature part 1" >> file.txt $ git commit -am "Add feature part 1"
$ echo "Fix typo" >> file.txt $ git commit -am "Fix typo in part 1"
$ echo "Feature part 2" >> file.txt $ git commit -am "Add feature part 2"
$ echo "Another fix" >> file.txt $ git commit -am "Fix another typo"`} />
- Combine the typo fixes into their related feature commits
- Edit commit messages to be more descriptive
- Ensure the history is clean and logical
Apply specific changes from one branch to another without merging the entire branch. Handle conflicts and maintain proper commit attribution.
feature-a.txt
$ git add feature-a.txt $ git commit -m "Add Feature A"
$ echo "Feature B" > feature-b.txt $ git add feature-b.txt $ git commit -m "Add Feature B"
$ git checkout main $ git cherry-pick feature/complex~1
$ git cherry-pick feature/complex
$ git cherry-pick --continue
$ git cherry-pick -n commit-hash
$ git commit -m "Combined changes"`} />
Practice using Git's reflog to recover from various "disaster" scenarios, including lost commits and accidentally deleted branches.
important.txt
$ git add important.txt $ git commit -m "Important changes" $ git checkout main $ git branch -D feature/important
$ git reflog $ git checkout -b feature/important HEAD@
$ git reset --hard HEAD~3
$ git reflog $ git reset --hard HEAD@
$ git reflog $ git cherry-pick HEAD@`} />
Perform complex history rewriting operations including changing author information, splitting commits, and filtering repository history.
"
$ git rebase --continue
$ git reset HEAD^ $ git add --patch $ git commit -m "First part of split" $ git add . $ git commit -m "Second part of split"
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
$ git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d $ git reflog expire --expire=now --all $ git gc --prune=now`} />
- Always create a backup branch before complex operations
- Use
--dry-run
when available to preview changes
- Keep reflog entries for longer on important repositories
- Document complex operations in commit messages