T0 Master Git: Branching, Reverting, Resetting, Squashing, Stashing, Conflict Resolution, and Cherry-Picking
Table of contents
The diagram above visualises a repository with two isolated lines of development for a little feature and one for a longer-running feature. By developing their branches it is not only possible to work on both of them in parallel, but it also keeps the main master branch free from error.
Each task has one separate branch.
After being done with the code, merge other branches to master.
This concept is useful for parallel development.
you can create any number of branches.
changes are personal to that particular branch.
The default branch is called Master.
Files created in the workspace will be visible in any of the branch workspaces until you commit. Once you commit, then that file belongs to that particular branch New.
when creating a new branch, data from the existing branch is copied to a new branch.
#to see available branches
git branch
#creating a branch
git checkout -b <Branch name>
#Switiching a branch
git checkout <branch name>
#delete a branch
git branch -d <branch name>
#to merge a branch
git merge <branch name>
git revert
The reward commit helps you undo an existing commit.
- She does not delete any data in this process. Instead, rather, it creates a new commit with an included file reverted to its previous state So your version control history moves forward, while the state of your file moves backwards.
# code for revert
git revert <commit id>
git reset
Git reset is a powerful command that is used to undo local changes to the state of the gate repo.
#To reset staging area
Get reset <filename>
To reset the changes from both the staging area and the working directory at the same time
Get reset --hard
Git Stashing
Suppose you are implementing a new feature for your product. Your code is in progress, and suddenly a customer escalation comes. Because of this, you have to keep aside your new feature work for hours. You cannot commit your partial code. And you also cannot throw away your changes. So you need some temporary storage where you can store your partial changes and later commit them.
# To stash an item (Only applies to modified files, not to new files)
# if new file, then first commit it once, then the future changes in it can be stashed
git stash
# To see stashed item
git stash list
# To apply stashed items
git stash apply stash@{0}
# if you want to apply the stash and remove it from the stack in one step
git stash pop
# To clear stash items
git stash clear
Git conflict
When the same file has different content in different branches and if you try to merge them, conflict occurs (Resolve conflicts then add and commit.
Conflict occurs when you merge branches.
Git squash
Git squash is used to combine multiple commits into a single cohesive commit. It allows us to condense the commit history and make it easier to understand. But sometimes this business can make your work difficult because condensing the commit history into one will not allow you to navigate to commit that occurred in between.
The term "squash" refers to the act of compressing or squashing several commits into one.
Git cherry-pick
It is a git command that allows you to apply changes introduced by your specific commit from one branch to another.
Here is how one can do it:
# Identify the commit you want to cherry pick from the source branch in to the current branch
# Switch to current branch
git checkout current-branch
# Cherry-pick the desired commit from source-branch
git cherry-pick <commit-id>
# Git will apply the changes from the specified commit to the current branch,
# creating a new commit with the same changes
In the next and final blog on Git, I will talk about Merging and Rebase and the difference between them in detail. I decided to do this because adding them to this article would make it too big and rebase is a topic that needs a separate blog for itself.