Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Advanced Operations

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:

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.

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:

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

git rebase -i 

Interactive rebasing allows you to edit, delete, or combine commits during the rebase. Common options include:

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