Here’s an example of how useful Git branches are. 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 hurried change that should go live on the site today. You go back to the master branch, make the change and put it live. You can then return to the new feature branch and finish the job. When you’re done, merge the new feature branch into the master branch, and both the new feature and the quick switch are kept!
For
all
commands below The
following commands assume that ‘ go navigated to the folder for the Git repository.
See which branch you are running
this command on: Git Status
List of all
branches
NOTE: The current local branch will be marked with an asterisk (*).
To view local branches, run this command: git branch To view remote branches, run this command: git branch -r To view
- all local and remote branches, run
this
- command: git branch -a Create a new branch Run this command (replacing my-branch-name with
the name you want):
-
-
-
- git checkout -b my-branch-name
- You are now ready to commit with this branch.
Switch to a branch
office in the local
repository Run this command: git checkout my-branch-name Switch to a branch
that came from
a remote repository
- To get a list of all branches of the remote control, run this command: git pull Run this command to switch to the branch : git checkout -track origin/my-branch-name Insert to a branch
-
- If your local branch does not exist on the remote control, run any 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 a branch of the same name on the remote. This saves you from having to Enter the exact name of the branch!
If your
- local branch already exists on the remote control, run this command:
- git push
Merge a
branch
- You’ll want to make sure your worktree is clean and see which branch it is on. Run this command:
- git status
- First, you need to check out the branch into which you want to merge another branch (changes will be merged into this branch). If you are not already in the desired branch, run this command:
- git checkout
- NOTE: Replace master with another branch name as needed.
master
- You can now merge another branch into the current branch. Run this command:
- git merge my-branch-name
- NOTE: When merging, there may be a conflict. See 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 one 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 regardless of its merged state.
Develop your skills
with hands-on classes
Learn Git with hands-on training:
NYC Python Coding Classes NYC Coding Classes Near Me or Online Live Coding Classes Python
- Classes Near Me or
- Live Python
Classes Online