Git Advanced Operations

After mastering the basics of Git, advanced operations can help you manage and optimize your codebase more efficiently.

Here are some common advanced operations and their detailed explanations:

  • Interactive staging: Select changes to stage block by block for fine-grained control over commits.
  • Git Stash: Temporarily save work progress for easy task switching.
  • Git Rebase: Move changes from one branch onto another while keeping commit history linear.
  • Git Cherry-Pick: Select a specific commit and apply it to the current branch.

1. Interactive Staging

git addThe command can selectively add files or parts of files to the staging area, which is very useful when dealing with complex changes.

  • Usagegit add -p: Select changes to stage hunk by hunk.
git add -p

After running this command, Git displays file changes hunk by hunk, allowing you to choose whether to stage each hunk. Common options include:

  • yStash current hunk
  • nSkip current hunk
  • sSplit current hunk
  • e: Manually edit the current hunk
  • q: Unstage

2. Git Stash: Temporarily save work progress

git stashThe command allows you to temporarily save changes in your current working directory so you can switch to another branch or handle other tasks.

Save the current work progress:

git stash

View stored progress:

git stash list

Apply the most recently stashed progress:

git stash apply

Apply and delete the most recently stashed progress:

git stash pop

Delete specific stash:

git stash drop stash@{n}

Clear all stashes:

git stash clear

3. Git Rebase: Rebasing

git rebaseThe command is used to move changes from one branch onto another branch. It helps keep the commit history linear and reduce conflicts during merging.

Rebase the current branch onto the specified branch:

git rebase <branchname>

For example, rebase the current branch ontomainbranch:

git rebase main
  • Interactive rebase:
git rebase -i <commit>

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

  • pick: Keep commit
  • rewordAmend commit message
  • edit: Edit commit
  • squash: Squash the current commit with the previous commit
  • fixup: Merge the current commit with the previous commit without preserving the commit message
  • drop: Delete commit

4. Git Cherry-Pick: Picking Commits

git cherry-pickThe command allows you to select a specific commit and apply it to the current branch. It is useful when you need to port specific changes from one branch to another.

Cherry-pick:

git cherry-pick <commit>

For example,abc123Apply the commit to the current branch:

git cherry-pick abc123

Resolve cherry-pick conflicts: If conflicts occur during cherry-pick, use this after resolving themgit cherry-pick --continueContinue cherry-picking.

Example operation

Below is a comprehensive example showing how to use these advanced operations:

Interactive staging:

git add -p

Save work progress:

git stash

View stored progress:

git stash list

Apply stored progress:

git stash apply

Rebase current branch ontomainbranch:

git rebase main

Interactive rebase to edit commit history:

git rebase -i HEAD~3

Edit commit history, such as squashing and renaming commits.

pickfeaturea specific commit on the branch tomainbranch:

git checkout main
git cherry-pick abc123
other extensions