Day 11 of #90DaysOfDevOps
Git Stash
Git stash is a command that allows you to temporarily save changes you have made in your working directory, without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.
To use Git stash, you first create a new branch and make some changes to it. Then you can use the command git stash to save those changes. This will remove the changes from your working directory and record them in a new stash.
git stash
git stash list command shows the list of stashed changes.
git stash list
You can apply these changes later. It doesn't delete the stash.
git stash apply stash@{n}
You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.
git stash drop
– this will delete the latest stash
git stash drop stash@{n}
– this will delete n'th stash
git stash clear
– will clear all the stashes
git stash pop
-- The command restores the changes and schedules the stash for deletion.
Cherry-pick
In Git, cherry-picking is taking a single commit from one branch and adding it as the latest commit on another branch.
Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.
To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.
git cherry-pick <commit_hash>
git cherry-pick <commit_hash> --no-commit
-- add the commit's changes to your Working Copy - without directly committing them:
Resolving Conflicts:
Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file.
Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before git can proceed with the merge/rebase. git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files.
There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.
Step 1: The easiest way to resolve a conflicted file is to open it and make any necessary changes.
Step 2: After editing the file, we can use the git add a command to stage the new merged content.
Step 3: The final step is to create a new commit with the help of the git commit command.
Step 4: Git will create a new merge commit to finalize the merge.
We can use git merge tool for viewing the changes made to files and fix them. Command for merge tool is
git mergetool
Demonstration of git stash
Demonstration of branching strategy, reset, revert, merge and rebase.
Demonstration of git cherry-pick
Subscribe to my newsletter
Read articles from Basavaraj Teli directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Basavaraj Teli
Basavaraj Teli
Aspiring DevOps engineer, working on DevOps projects to gain practical knowledge. I write technical blog post on DevOps to share my knowledge with fellow tech enthusiasts.