This chapter belong to Getting Started with Git: A Beginner's Guide Series
In software development, it's common to have multiple branches of code that need to be merged into a main branch. Advanced Git techniques, such as stashing, rebasing, and cherry-picking, help to make these merges smoother and more efficient.
Stashing
Stashing allows developers to temporarily save changes in a branch that are not ready to be committed to the main branch. This can be useful when switching between branches or when pulling in changes from the main branch. To stash changes, use the command:
git stash
To get the stashed changes stack in Git, you can use the git stash list
command. This command will show you a list of all the stashes you have stored in your repository. Each stash is identified by a stash ID, which is a hash of the changes you stashed away. To get more detailed information about a particular stash, you can use the "git stash show" command followed by the stash ID. This command will display the differences between the stash and your current branch.
To apply the changes from a stash, you can use the git stash apply
command followed by the stash ID. This command will restore the changes to your current branch, but it will not remove the stash from your stash stack. If you want to remove the stash after you apply it, you can use the git stash drop
command followed by the stash ID.
If you have multiple stashes, you can apply a specific stash by specifying its stash ID after the "git stash apply" command. For example, if you have 3 stashes and you want to apply the second stash, you can use the following command:
$ git stash apply stash@{1}
You can also use the "git stash pop" command to apply the latest stash and remove it from the stash stack in one command. For example:
$ git stash pop
Rebasing
Rebasing is the process of taking a branch and reapplying it on top of another branch. This can help to make the branch more current and make merging easier. To rebase a branch, use the command:
$ git rebase <branch>
The difference between merge and rebase
Merging involves creating a new merge commit that brings the changes from one branch into another. This results in a linear history, with all the changes being recorded in a single line. Merging is simple and straightforward, and is often used when working with multiple people on the same branch.
Rebasing, on the other hand, involves taking the changes from one branch and reapplying them on top of another branch. This results in a linear history, with the changes appearing as if they were made directly on the other branch. Rebasing is often used when working on a feature branch and you want to keep your code up-to-date with the latest changes in the main branch.
It's important to understand the differences between merging and rebasing, and to choose the appropriate method for your project and workflow. If you're working with multiple people on the same branch, it's generally recommended to use merging. However, if you're working on a feature branch and want to keep your code up-to-date, rebasing is often a better choice.
Cherry-picking
Cherry-picking is the process of taking a specific commit from one branch and applying it to another branch. The cherry-pick command allows you to apply a specific commit from one branch to another branch, without merging the entire branch. To cherry- pick a commit, use the command:
git cherry-pick <commit-hash>
These advanced Git techniques can help make your workflow more efficient and make it easier to collaborate with others on a project. However, it's important to understand the implications of each technique and use them carefully.