Git Advanced Operations
After mastering the basic operations of Git, advanced operations can help you manage and optimize your code repository more efficiently.
The following are some common advanced operations and their detailed descriptions:
- Interactive Staging: Select changes to stage hunk by hunk, giving fine-grained control over commit content.
- Git Stash: Temporarily save work in progress to make it easy to switch tasks.
- Git Rebase: Move changes from one branch on top of another, keeping the commit history linear.
- Git Cherry-Pick: Select specific commits and apply them to the current branch.
1. Interactive Staging
git add The command can selectively add a file or part of a file to the staging area, which is very useful when dealing with complex changes.
- Use
git add -p: Select changes to stage hunk by hunk.
git add -p
After running this command, Git displays the file changes hunk by hunk, and you can choose whether to stage each hunk. Common options include:
y: Stage the current hunkn: Skip the current hunks: Split the current hunke: Manually edit the current hunkq: Exit staging
2. Git Stash: Temporarily Saving Work in Progress
git stash The command allows you to temporarily save changes in the current working directory so that you can switch to other branches or work on other tasks.
Save the current work in progress:
git stash
View the stored progress:
git stash list
Apply the most recently stored progress:
git stash apply
Apply and remove the most recently stored progress:
git stash pop
Delete a specific stash:
git stash drop stash@{n}
Clear all stashes:
git stash clear
3. Git Rebase: Rebasing
git rebase The command is used to move changes from one branch on top of another. It helps keep the commit history linear and reduces conflicts when merging.
Rebase the current branch onto a specified branch:
git rebase
For example, rebase the current branch onto the main branch:
git rebase main
- Interactive rebase:
git rebase -i
Interactive rebasing allows you to edit, delete, or combine commits during the rebase. Common options include:
pick: Keep the commitreword: Edit the commit messageedit: Edit the commitsquash: Combine the current commit with the previous commitfixup: Combine the current commit with the previous commit without keeping the commit messagedrop: Delete the commit
4. Git Cherry-Pick: Picking Commits
git cherry-pick The command allows you to select specific commits and apply them to the current branch. It is very useful when you need to port specific changes from one branch to another.
Pick a commit:
git cherry-pick
For example, apply the abc123 commit to the current branch:
git cherry-pick abc123
Handling Cherry-Pick Conflicts: If a conflict occurs during cherry-picking, after resolving the conflict use git cherry-pick --continue to continue cherry-picking.
Example Operations
The following is a comprehensive example showing how to use these advanced operations:
Interactive Staging:
git add -p
Save Work in Progress:
git stash
View the stored progress:
git stash list
Apply the stored progress:
git stash apply
Rebase the current branch onto main Branch:
git rebase main
Interactive rebase to edit the commit history:
git rebase -i HEAD~3
Edit the commit history, such as combining and renaming commits.
Cherry-pick feature a specific commit from the main Branch:
git checkout main
git cherry-pick abc123