Git Branches: List, Create, Switch to, Merge, Push, & Delete

Git lets you branch out from the master code base. This lets you more easily influence with other developers, and gives you a batch of tractability in your work flow .
git branches merge
here ‘s an example of how Git branches are utilitarian. Let ‘s say you need to work on a new feature for a web site. You create a newly branch and start working. You have n’t finished your fresh feature, but you get a request to make a bang change that needs to go live on the locate today. You switch back to the master arm, make the exchange, and push it be. then you can switch back to your raw sport ramify and finish your workplace. When you ‘re done, you merge the newly feature branch into the overlord branch, and both the new sport and rush change are kept !

For All the Commands Below

The commands below assume you ‘ve navigated to the booklet for the Git repo.

See What Branch You’re On

  • Run this command:
    • git status

List All Branches

NOTE: The current local anesthetic arm will be marked with an asterisk ( * ) .

  • To see local branches, run this command:
    • git branch
  • To see remote branches, run this command:
    • git branch -r
  • To see all local and remote branches, run this command:
    • git branch -a

Create a New Branch

  • Run this command (replacing my-branch-name with whatever name you want):
    • git checkout -b my-branch-name
  • You’re now ready to commit to this branch.

Switch to a Branch In Your Local Repo

  • Run this command:
    • git checkout my-branch-name

Switch to a Branch That Came From a Remote Repo

  1. To get a list of all branches from the remote, run this command:
    • git pull
  2. Run this command to switch to the branch:
    • git checkout –track origin/my-branch-name

Push to a Branch

  • If your local branch does not exist on the remote, run either of these commands:
    • git push -u origin my-branch-name
    • git push -u origin HEAD

NOTE: HEAD is a reference to the top of the current outgrowth, so it ‘s an easy room to push to a branch of the lapp name on the outside. This saves you from having to type out the demand name of the arm !

  • If your local branch already exists on the remote, run this command:
    • git push

Merge a Branch

  1. You’ll want to make sure your working tree is clean and see what branch you’re on. Run this command:
    • git status
  2. First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch). If you’re not already on the desired branch, run this command:
    • git checkout master
    • NOTE: Replace master with another branch name as needed.
  3. Now you can merge another branch into the current branch. Run this command:
    • git merge my-branch-name
    • NOTE: When you merge, there may be a conflict. Refer to Handling Merge Conflicts (the next exercise) to learn what to do.

Delete Branches

  • To delete a remote branch, run this command:
    • git push origin –delete my-branch-name
  • To delete a local branch, run either of these commands:
    • git branch -d my-branch-name
    • git branch -D my-branch-name
  • NOTE: The -d option only deletes the branch if it has already been merged. The -D option is a shortcut for –delete –force, which deletes the branch irrespective of its merged status.

Grow Your Skills With Hands-on Classes

Learn Git with hands-on train :

generator :
Category : Tech

About admin

I am the owner of the website, my purpose is to bring all the most useful information to users.

Check Also


Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.