Mastering Git and GitHub - 2: Advanced Techniques for Streamlined Version Control
⭐Git Stash
Why do you need Git Stash?
Sometimes you want to switch the branches, but you are working on an incomplete part of your current project.
You don’t want to make a commit of half-done work. Git stash allows you to do so.
The git stash command enables you to switch branches without committing to the current branch.
Let's start with practical:
I made modifications in two separate files from two different branches. I'm in a ruin, and I haven't finished editing any files yet. So I'd like to temporarily save it for future usage. We can save it in its current state by stashing it.
Let's take a peek at the repository's present state before stashing. Run the git status command to see the repository's current status. The git status command is used in the following ways:
The above output shows that there are two untracked files available in the repository: file2.txt and file3.txt. We can use the git stash command to save it temporarily. The git stash command is used to do the following:
$ git stash
The work is saved with the git stash command in the output. We can check the repository's status.
git status
Note: my work is just stashed in its current position. Now, the directory is cleaned. At this point, you can switch between branches and work on them.
👉Git Stash Save (Saving Stashes with the message)
git stash save "<Stashing Message>"
Changes in Git can be tagged with a message. Run the following command to save a change with a message
👉Git Stash List (Check the Stored Stashes)
To check the stored stashes, run the below command:
git stash list
If we have more than one stash, then It will display all the stashes respectively with different stash id.
👉Git Stash Apply
Using the git stash command, you can re-apply the modifications you just saved(Edited). Use the git stash command, followed by the apply option, to apply the commit. It is employed as
git stash apply
👉Git Stash Drop (Unstash)
To remove a stash from the queue, use the git stash drop command. In most cases, it deletes the most recent stash. Precautions should be required before applying the stash drop command, as it is difficult to reverse once executed.
The only way to undo it is to leave the terminal open after deleting the stash. The stash drop command will be executed as follows
git stash drop
We can also remove a specific stash from the queue. Pass the stash id in stash drop to eliminate a specific stash from the available stashes. It will be handled as follows
git stash drop <stash id>
👉Git Stash Pop (Reapplying Stashed Changes)
The git stash pop command allows the user to re-apply earlier commits. Using the popping option, you can remove modifications from stash and apply them to your working file.
The git stash pop command is quite similar to the git stash apply command. The key distinction between these two operations is that stash pop deletes the stash from the stack after it is applied.
git stash pop
⭐Cherry Pick
Why do you need a cherry-pick?
Selective Changes: Cherry-picking allows you to include specific changes or commits from one branch into another, rather than merging the entire branch.
Bug Fixes: It's useful for quickly applying bug fixes from one branch to another without bringing in unrelated changes.
Feature Management: Cherry-picking helps manage the inclusion of completed features into the main branch before others are ready.
Synchronization: You can keep multiple branches in sync by cherry-picking specific commits, simplifying the process.
Clean History: Cherry-picking can help maintain a cleaner, more linear commit history by avoiding unnecessary merge commits
Let's start with practical:
Create a master branch & test branch and commit it
Create a test branch,
git branch <BranchName>
git log --oneline
git checkout <branchname>
Note down the Commit ids.
Now, I want to specific commits on my master branch i.e. c & e so by using cherry-pick the following way i have achieved that.
git cherry-pick <Commit id>
Note: Here you can on the master branch that commits are copied not moved.
⭐Resolving Conflicts
Conflicts can occur while merging or rebasing diverged branches, and you must manually address the conflicts before git can proceed with the merge/rebase.
The git status command displays conflicting files, the git diff command displays the difference between conflicting versions, and the git add command is used to add the resolved files.
Congratulations 🎉 ! You learn about git stash & git cherry-pick with a practical approach.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊