Day11 of #90daysofdevops | Git stashing & resolving conflicts (Advancing with Git)

Day11 of #90daysofdevops | Git stashing & resolving conflicts (Advancing with Git)

ยท

3 min read

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. You can apply these changes later. git stash list command shows the list of stashed changes.

You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.

Cherry-pick:

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.

Resolving Conflicts:

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.

Task

  • Create a new branch and make some changes to it.

  • Use git stash to save the changes without committing them.

  • Switch to a different branch, make some changes and commit them.

  • Use git stash pop to bring the changes back and apply them on top of the new commits.

    Creating a new branch called branch1 from the master branch, at this stage everything gets copied to the new branch created, as shown in the below image

    Now since we are in the new branch as branch1, we will modify the file and will add it to the staging area but we are not committing the changes.

    Now we will send this newly added content to the temporary storage which we call stash. So we will use the git stash command and check the stash status. Also, we will now switch back to the master branch and will modify the same file from there and since we have not committed from branch1, we won't see any content there. Will add and commit from the master now.

    Now since we modified the file from the master, we are seeing a conflict here (We will resolve the conflict manually by editing the file and making desired changes) - when we do it, we will see the content file as what is changed from the master branch and what was added from the stash side.
    git stash list is to check the stash content
    git stash clear is to remove the stash area

    Git reset --->

  • git reset . command is used for removing files from the staging area that is added from the workstation.

  • git reset --hard command is used for removing files from staging as well as from the workstation.

    Thanks for reading the blog & do share them with someone in need :)

    Please share your views and suggestions, they are always welcome.

    See you then in the next blog.

    Happy learning :)

ย