Table of contents
- What is Git Stash?
- What is Cherry-pick?
- Resolving Conflicts:
- Let's do some Tasks
- 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.
- Rebase the production branch to the master branch
What is Git Stash?
Git stash is a Git command that allows developers to temporarily save changes made to a working directory without committing them.
The stash command works by saving the changes to a new stash, which can be reapplied later when needed. This is useful when a developer needs to switch to another branch or work on another task but doesn't want to commit the changes yet.
Stashing can be done with the following command:
git stash
To apply the stash later, use the following command:
git stash apply
Stashes can also be named to make it easier to identify and apply them later:
git stash save "My stash"
git stash apply stash@{0}
Git stash can also be used to create a new branch with the stashed changes:
git stash branch <newbranchname>
This command creates a new branch from the commit where the stashed changes were made and applies the stash to the new branch.
In summary, Git stash is a command that allows developers to temporarily save changes made to a working directory without committing them. It's useful for switching to another branch or task and coming back to the changes later.
What is Cherry-pick?
Git cherry-pick is a command that allows developers to apply a specific commit from one branch to another branch.
The cherry-pick command works by taking the changes made in a single commit and applying them to the current branch. This can be useful when a developer needs to apply a specific change or feature from one branch to another without merging the entire branch.
To cherry-pick a commit, the following command can be used:
git cherry-pick <commit-hash>
This command applies the changes made in the specified commit to the current branch. If there are any conflicts between the changes made in the commit and the changes in the current branch, Git will prompt the user to resolve the conflicts before completing the cherry-pick.
Cherry-picking can be useful for applying bug fixes or new features that have been made in a separate branch to the current branch without merging the entire branch. However, it's important to use cherry-picking carefully and to make sure that the changes being made are fully understood before executing the command, as it can have significant consequences on the code.
In summary, Git cherry-pick is a command that allows developers to apply a specific commit from one branch to another branch. It can be useful for applying specific changes or features without merging the entire branch, but it should be used carefully and with a full understanding of the changes being made.
Resolving Conflicts:
When merging or rebasing branches that have diverged, it's common to encounter conflicts. Conflicts occur when two branches have made changes to the same part of a file, and Git is unable to automatically determine which version of the changes to keep.
To resolve conflicts, you will need to manually edit the affected files to merge the conflicting changes. Here are the steps to follow:
Start by running the merge or rebase command:
git merge <branchtomerge>
or
git rebase <branchtorebaseon>
If Git encounters conflicts, it will stop and notify you which files have conflicts. You can use the following command to see the files with conflicts:
git status
Open each file with conflicts in a text editor and look for the sections marked with conflict markers (<<<<<<<, =======, and >>>>>>>).
Edit the file to keep the changes you want, remove the conflict markers, and save the file.
After editing all the files with conflicts, use the following command to stage the changes:
git add <edited-file>
Once all changes have been staged, use the following command to complete the merge or rebase:
git merge --continue
or
git rebase --continue
If you run into any issues during the conflict resolution process, you can use the following command to abort the merge or rebase and start over:
git merge --abort
or
git rebase --abort
In summary, resolving conflicts that occur during merging or rebasing involves manually editing files to merge the conflicting changes, staging the changes, and completing the merge or rebase. It's important to carefully review and understand the changes being made to avoid introducing unintended consequences into the code.
Let's do some Tasks
Create a new branch and make some changes to it.
Step 1: Log in to your EC2 Instance
Step 2: Clone the remote repository to your local
git clone <repository-url>
Step 3: Change the directory to the newly added one from GitHub.
cd <directory_name>
Step 4: Create a new branch.
git branch production
You can check all the branches by using the following command:
git branch
Step 5: Switch the branch
git checkout production
Step 6: Make a new file in the production branch:
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.
Rebase the production branch to the master branch
Thank you for reading! I hope you found value from the blog.
Happy learning!