Git:Branch
lastmod 2025-02-15created 2014-03-0515 edits
Description of the Git branche command
Frequently used Git Branch command options
-
git branch - Lists the existing branches.
-
git branch -r - Lists the branches of the remote repository.
-
git branch<Branch B><Branch A> - Creates a new branch, Branch B, from Branch A. (In git, the default branch is named master.)
-
git branch<Branch name> - Creates a new branch with the given branch name. (Does not check it out.)
-
git fetch --all --prune - Applies branches added to or deleted from the remote repository to my Local
-
git branch -d<Branch name> - Deletes the branch.
-
git push origin --delete<Branch name> - Deletes the remote branch.
-
git branch -m<Branch name><New Branch name> - Renames an existing branch to the new branch. If a branch with that name already exists, an error occurs; with the
-Moption, the existing branch is overwritten.
-
git tag - Shows the list of existing tags.
-
git tag<Tag name><Branch name> - Attaches a tag with the tag name to the current point of the branch.
-
git checkout<name> - Switches the working tree to that branch or tag.
-
git checkout -b<Branch B><Branch A> - Creates a new branch, Branch B, from Branch A and checks it out.
-
git rebase 브랜치명 - Applies the changes of the named branch to the current branch.
-
git cherry-pick 커밋명 - Picks only the specified commit and makes it a commit on the current branch. With the
-noption, the changes are merged into the working tree but not committed, so you can combine several commits into one.
Pushing a branch
-
git push [원격저장소명] [로컬브랜치명] -
git push [원격저장소명] [로컬브랜치명:원격브랜치명]
These commands push a local branch to the remote. To use the same branch name on the remote, use the first command; to use a different name on the remote, use the second.
The main remote repository is usually named origin, so git push origin [브랜치명] as above applies the branch to the remote repository as well. (Branches used only locally don't need to be pushed.)
Linking a new local branch to an existing remote branch
For example, if there is no local a4y-2.0 branch but one exists on the origin remote server, and you want to link the a4y-2.0 branch to origin/a4y-2.0, use branch --set-upstream-to.
Linking a local branch to a new remote branch
For example, if there is a local a4y-2.0 branch but none on the origin remote server, and you want to link the a4y-2.0 branch to origin/a4y-2.0, use branch --set-upstream.
Move master to branch
Within the directory of the repository, in the command line type the following:
Default remote/branch
When using git from the command line, typing the remote or branch name every time gets tedious. In that case it helps to set a default remote or branch. You can pass the --track option on the first checkout, or edit the .git/config file directly. Alternatively, you can set it with the branch --set-upstream-to option.
Tracking branch
Tracking branch information is the link between a local branch and a remote branch, and it is stored in the config.
Remote Tracking branch
A Remote Tracking branch is a branch such as origin/master. It points to exactly the commit that the master branch in the origin repository points to. It is updated when you Fetch the branches of the remote repository.
Remote Tracking branches have the following characteristics.
- The user cannot modify this branch directly.
- It can only be updated with git fetch and git pull.
Tracking branch
A Tracking branch is more complex than a Remote Tracking branch (hereafter remote branch), but it is not difficult.
A Tracking branch is a local branch: a local branch that Tracks a remote branch.
Tracking characteristics are as follows:
- The user can modify this branch directly.
- Its information is not updated by git fetch. git fetch only creates the Remote Tracking branches that Track the branches of the remote repository.
Think of a Tracking branch as a kind of helper. Not knowing about it is no great inconvenience.
Clone all remote branches
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
For reference, for TAGs use git pull --tags.
Syncing remote branches
Sometimes branches added to or deleted from the remote repository are not reflected in my Local.
or