Table of contents
Welcome to my Blog. Today we will dig deep into some of the advanced concepts for Git and GitHub and perform the tasks of the given day.
Git Stash
Git stash is a command in Git that allows you to temporarily save changes you've 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 don't want to commit the changes you've made in your current branch yet. Here's an example of how to use git stash
:
Run
git stash
to save your changes.Switch to the other branch with
git checkout
.Make your changes on the bug fix branch.
Switch back to the feature branch with
git checkout
.Retrieve your stashed changes with
git stash pop
orgit stash apply
.
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. The 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 in Git 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. Here's how to use git cherry-pick
:
Create two new branches and make some commits to them.
Use
git cherry-pick <commit_hash>
command to select the specific commits from one branch and apply them to the other.
For example, let's say you have two branches, feature
and bugfix
. You made some changes to the feature
branch that you want to apply to the bugfix
branch. Here's how to do it:
Switch to the
bugfix
branch withgit checkout bugfix
.Use
git cherry-pick <commit_hash>
to select the specific commit from thefeature
branch and apply it to thebugfix
branch.Repeat step 2 for any other commits you want to apply.
This way, you can selectively apply changes from one branch to another without merging the entire branch.
Resolving Conflicts :
When multiple developers work on the same file, Git may detect a conflict when trying to merge their changes. Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. Git cannot automatically determine what is correct in such cases, so it marks the file as being conflicted and halts the merging process. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. To resolve conflicts, developers can use the following methods:
Fetch and merge the latest changes from the remote repository with
git pull
.Identify the conflicting files with
git status
.Open the conflicting file using a text editor.
Resolve the conflict by manually editing the file to combine the conflicting changes.
Add or stage the changes with
git add
.Commit the changes with a comment using
git commit
.
Alternatively, developers can use merge tools or GUIs to resolve conflicts. There are two types of merge conflicts:
Line conflict: When two developers change the same line of code in a file.
Structural conflict: When the changes made by two developers affect the same file but do not conflict with each other line-by-line.
In summary, resolving conflicts in Git involves identifying the conflicting files, manually editing the files to combine the changes, and committing the changes.
Tasks
01)
Create a new branch and make some changes to it.
Use git stash to save the changes without committing them.
You won't be able to use git stash unless you track the file.To include the untracked files you need to modify the command
git stash --include-untracked
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.
02)
03)
Thankyou for reading till the end. Hope you've found something valuable.See you in the next blog.