Git Branching:
In Git, branches are an essential part of the version control system, allowing you to work on different features, fixes, or developments concurrently without interfering with each other.
By default, the master branch is present, and it is sometimes referred to as the root branch.
When you first initialize a Git repository, the initial branch created is called "master."
To create a new branch, you can use the following command:
git checkout -b <branch name>
.A new branch is initially a copy of the master branch, which means it contains the same code as the master branch at the time of creation.
Git Revert & Reset
git revert
:
git revert
is used to create a new commit that undoes the changes made in a previous commit. It's a safe way to reverse changes and maintain a linear history in the repository.When you run
git revert <commit>
, Git will create a new commit that effectively cancels out the changes introduced by the specified commit. The original commit remains in the history.This is useful when you want to undo changes in a non-destructive way, preserving the commit history.
Example:
bashCopy codegit revert <commit-SHA>
git reset
:
git reset
is used to move the current branch pointer to a different commit. It can be used to remove commits from the branch's history or to "reset" the working directory to a previous state.There are three modes of
git reset
:--soft
,--mixed
, and--hard
.--soft
only moves the branch pointer, leaving your working directory and staging area unchanged. It's used for uncommitted changes.--mixed
moves the branch pointer and updates the staging area but doesn't modify your working directory. This is useful for uncommitted changes you want to reset.--hard
moves the branch pointer, updates the staging area, and resets the working directory to match the specified commit. It discards changes completely.
Be cautious when using
git reset --hard
because it can result in data loss since it discards changes.
Example:
bashCopy codegit reset <commit-SHA> --soft
git reset <commit-SHA> --mixed
git reset <commit-SHA> --hard
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.
Commands
git branch: To see total branches is a repository
git checkout -b [branch-name]: To make a new branch and switch to new branch
git log –oneline: track the history of commit and check which file is in which branch
git checkout [branch-name]: To switch the branch
git branch -d [branch_name]: To delete local branch
git branch origin --delete [branch name]: To delete a remote branch
git cherry-pick [commit hash]: used to pick only a particular commit file from the sub-branch to master.
git stash: used to save the changes
git pop: used to return the changes from the stash
git revert [commit hash-value]: command is used to revert the commit changes but not to delete from the commit history
git reset --soft [commit hash-value]: Command is used to delete the changes from the commit history and also delete the commit from the history but changes are available in working directory and stagging area.
git reset --hard [commit hash-value]: Commands is used to delete the changes and history from the commit. It will delete from everywhere