0% found this document useful (0 votes)
36 views

Git Branches - List, Create, Switch To, Merge, Push, & Delete

The document discusses Git branches, including how to list, create, switch to, merge, push, and delete branches. It provides commands for each action, such as "git branch" to list branches, "git checkout -b my-branch-name" to create a new branch, and "git merge my-branch-name" to merge branches. Maintaining branches allows flexible collaboration and workflow when working with code.

Uploaded by

Carlos Alfonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Git Branches - List, Create, Switch To, Merge, Push, & Delete

The document discusses Git branches, including how to list, create, switch to, merge, push, and delete branches. It provides commands for each action, such as "git branch" to list branches, "git checkout -b my-branch-name" to create a new branch, and "git merge my-branch-name" to merge branches. Maintaining branches allows flexible collaboration and workflow when working with code.

Uploaded by

Carlos Alfonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Classes are running in-person (socially distanced) and live online. Secure your seat today

Search...

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


Delete
Git Tips & Commands

October 6, 2021
Read more in Git, Front-End Web Development, Web Development

Share

Git lets you branch out from the original code base. This lets you more easily work with other
developers, and gives you a lot of flexibility in your workflow.

https://www.nobledesktop.com/learn/git/git-branches 1/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Here's an example of how Git branches are useful. Let's say you need to work on a new feature for
a website. You create a new branch and start working. You haven't finished your new feature, but
you get a request to make a rush change that needs to go live on the site today. You switch back to
the master branch, make the change, and push it live. Then you can switch back to your new
feature branch and finish your work. When you're done, you merge the new feature branch into
the master branch, and both the new feature and rush change are kept!

For All the Commands Below


The commands below assume you've navigated to the folder for the Git repo.

See What Branch You're On


Run this command:

git status

List All Branches


NOTE: The current local branch 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.

https://www.nobledesktop.com/learn/git/git-branches 2/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

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 branch, so it's an easy way to push to a
branch of the same name on the remote. This saves you from having to type out the exact name of
the branch!

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:

https://www.nobledesktop.com/learn/git/git-branches 3/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

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 training:

Coding Classes NYC


Python Classes NYC
Coding Classes Near Me or Coding Classes Live Online
Python Classes Near Me or Python Classes Live Online

https://www.nobledesktop.com/learn/git/git-branches 4/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Web Development
Web Design Classes
Courses

Front-End Web
Development Certificate
108 hours $3,495

Web Design Certificate


162 hours $4,995

How to Learn Git


Master Git with hands-on training. Git is a free, open-source version control system that allows
developers to track the changes they make to code.

Full-Stack Web Development Certificate at Noble Desktop: live, instructor-led course available
in NYC or live online

https://www.nobledesktop.com/learn/git/git-branches 5/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Find Web Development Classes Near You: Search & compare dozens of available courses in-
person
Attend a web development class live online (remote/virtual training) from anywhere
Find & compare the best online web development classes (on-demand) from the top
providers and platforms
Train your staff with corporate and onsite web development training

Related Resources

How to Create a Git Repository: git init


A Git repository (or repo for short) contains all of the project les and the entire revision
history. Learn the Git command to make a repository.

Push to a Remote Repository: git push


After you have a remote repository set up, you upload (push) your files and revision history
to it.

https://www.nobledesktop.com/learn/git/git-branches 6/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Pull From a Remote Repository: git pull & git fetch


After someone else makes changes to a remote repo, you can download (pull) their
changes into your local repo.

Contact Us Location

Office Hours: In-Person in NYC


9am–6pm, Mon–Fri 185 Madison Avenue 3rd Floor
New York, NY 10016
(212) 226-4149
[email protected] Campus Info

Live Online from Anywhere


Live Online Info

Noble Desktop is today’s primary center for learning and career development.
Since 1990, our project-based classes and certificate programs have given
professionals the tools to pursue creative careers in design, coding, and beyond.
Noble Desktop is licensed by the New York State Education Department.

Win a Free Class!

Enter your email to get tips, free giveaways, and more in our weekly newsletter.

If you are a human, ignore this field

https://www.nobledesktop.com/learn/git/git-branches 7/8
21/2/23, 11:35 Git Branches: List, Create, Switch to, Merge, Push, & Delete

Enter your email Sign Up

© 1998–2023 Noble Desktop - Privacy & Terms

https://www.nobledesktop.com/learn/git/git-branches 8/8

You might also like