Git Branching explained in detailed |
Prerequisites to this you should be aware git basics and to experiment with these command examples should have git installed on your Mac/Linux or git bash on Windows system.
[This is best option to delete because it preserves the recent changes made on the local branch]
9. Deleting the remote branch
This is delicated operation, double check before you run this command.
What is the Branch in Git?
A Git branch is a separate line of work, where your work will not disturb other DevOps team members work.
Branch is a simply a movable pointer to commit, this we can observe using git branch command output wherever you see that '*' prefixed there the pointer pointing. Default branch is master or main (new trend) but it is optional we can name anything when we initiate the git repository.
Main branch is the FIRST branch of your project that means once you run the 'git init' then 'git branch' you can observe this.
On the branch we can have many changes until find the solution and accordingly you need to do multiple commits.
Each branch created for different purpose for example new feature development we name them as 'feature'. similarly we can also have planned releases where we name them as 'release1.0' or 'dev-release'. Again it is flexible to the SCM manager decision.
Why we need to create branch in Git?
The Major reasons for Git branching are:
- Any new features development could BREAK the code, to save this.
- No disturbance between two developer codes that means this git branching simplify the collaboration work which gives more agility to the project
Gitting Start Branching
Here I'm with an interesting story about SCM Git branching that helps to work in collaborative environments where DevOps is a culture.
Let me share with you guys about the steps that I've executed in this are realtime requirements.
Let me share with you guys about the steps that I've executed in this are realtime requirements.
Top 10 Examples on git branch sub-command
1. How to create a branch and check using git sub-commands?
To create a new branch from the existing branch.
Here you should be on one of the branch. Generally we start from main or master branch.
git branch newbranch git branch
2. How to jump onto the new branch directly when it is created?
Create a branch and switch to it in single command
git checkout -b feature git branch
3. How to switch between the git branches?
Switch to branch
We have two command options for switching between branches.
a. Regular subcommand for switching to a branch
git checkout newbranch git branchb. New way to switch between the branch
git switch master git branch
5. How can I view all local repo branches?
To view all branches which are created in the local repository no option will do or else we can use status with -b to see in single-line output
git branch git status -b -s #alternative option
6. How can I see remote branches on my local laptop/local repo?
To view all the remote branches only you need to use -r or --remote as options
This is command will help you to get more info about all the branches are available in the remote git server such as GitHub or BitBucket etc.
git branch -r git branch --remote
git branch remote to view your github or bitbucket branches |
7. How can I see all local and remote repositories together?
To view all branches including remote branches use -a or --all options
git branch -a git branch --all
Observe the different colors for each repos.
- The green color indicates current working tree(branch)
- The red indicates remote repo branches
- The white color indicate local branches
git branch all command example |
8. Is there any way to delete a git branch in local repo?
Delete a branch can be allowed when we use the -d or --delete options on a target branch.
Note : You cannot delete the branch on which you are working. Switch to anyother branch then run this.
git branch -d newbranch git branch --delete newbranch
Removing a branch |
git branch -D myremote-branch
Compare -d and -D example for git branch removal |
9. How to delete a remote branch in git command line ?
Better option is to use delete option with push command
git push origin --delete myremote-branch
10. How to rename a branch on git?
Move a branch/Rename
a. Linux move command used to rename same way here a branch can be renamed and its reflog
b. Rename forcefully
git branch -m newbranch dev git branch --move newbranch dev
Git branch move or renaming |
Even though your target branch exist the git move will happen forcefully when we use -M option.
Hope you enjoy this story. Connect with our earlier technical articles might give some new tricks or tips for your project growth and more productive.
Cheers!!
git branch -M newbranch dev
Hope you enjoy this story. Connect with our earlier technical articles might give some new tricks or tips for your project growth and more productive.
Cheers!!
No comments:
Post a Comment