Difference between revisions of "Git/branch"
< Git
Jump to navigation
Jump to search
m (proper sorting for categories) |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
"Branching" is a concept basic to most [[version control system]]s. | "Branching" is a concept basic to most [[version control system]]s. | ||
==Commands== | ==Commands== | ||
− | + | Information: | |
* <code>git branch</code> lists local branches | * <code>git branch</code> lists local branches | ||
* <code>git branch -r</code> lists the remote branches | * <code>git branch -r</code> lists the remote branches | ||
* <code>git branch -a</code> lists both local and remote branches | * <code>git branch -a</code> lists both local and remote branches | ||
+ | Actions: | ||
+ | * <code>git branch {{arg|name}}</code> creates a new branch called "name", pointing at the current {{l/same|HEAD}} revision | ||
+ | * <code>git {{l/same|checkout}} {{arg|name}}</code> moves {{l/same|HEAD}} to point to the branch called "name" | ||
+ | ** Note: <code>git {{l/same|checkout}} -b {{arg|name}}</code> accomplishes both of the above in a single command | ||
+ | * <code>git branch -d {{arg|branch name}}</code> to delete a branch | ||
+ | ** Note: must be in a different branch | ||
+ | |||
+ | The basic sequence of events for creating a new branch called "test", updating it, then reverting back to the original branch: | ||
+ | * <code>git branch test</code> - create the branch | ||
+ | * <code>git checkout test</code> - switch to it | ||
+ | * {{l/sub|stage}} whatever changes you want to include in "test" | ||
+ | ** This can include adding any new files you've created for this branch. | ||
+ | * <code>git commit -a -m 'made a change'</code> - commit the change to "test" | ||
+ | * <code>git checkout master</code> - switch back to the main branch (removes new files, restores any you deleted) | ||
==Links== | ==Links== | ||
===Reference=== | ===Reference=== | ||
− | * [http://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell 3.1 Git Branching - Branches in a Nutshell] | + | * Official: |
+ | ** [https://git-scm.com/docs/git-branch git-branch - List, create, or delete branches] | ||
+ | ** [http://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell 3.1 Git Branching - Branches in a Nutshell] | ||
+ | ===How To=== | ||
+ | * '''2019-10-07''' [https://linuxize.com/post/how-to-rename-local-and-remote-git-branch/ How To Rename a Local and Remote Git Branch] | ||
+ | ** Basically: {{fmt/code|git branch -m {{arg|new_name}}}} renames the branch locally and doesn't mess with anything upstream. It's like you just created {{arg|new_name}} and put stuff in it. | ||
+ | ** Note: if the branch is not in the local repository, then you just need to [[git fetch]] it after the <code>git checkout <old_name></code>. | ||
+ | ** Also: {{fmt/code|git branch -c {{arg|new_name}}}} to copy instead of renaming. |
Latest revision as of 20:42, 11 June 2023
A "branch" is a separate copy (of the files involved in a project) which can be worked on without affecting the original copy.
"Branching" is a concept basic to most version control systems.
Commands
Information:
git branch
lists local branchesgit branch -r
lists the remote branchesgit branch -a
lists both local and remote branches
Actions:
git branch <name>
creates a new branch called "name", pointing at the current HEAD revisiongit checkout <name>
moves HEAD to point to the branch called "name"- Note:
git checkout -b <name>
accomplishes both of the above in a single command
- Note:
git branch -d <branch name>
to delete a branch- Note: must be in a different branch
The basic sequence of events for creating a new branch called "test", updating it, then reverting back to the original branch:
git branch test
- create the branchgit checkout test
- switch to it- stage whatever changes you want to include in "test"
- This can include adding any new files you've created for this branch.
git commit -a -m 'made a change'
- commit the change to "test"git checkout master
- switch back to the main branch (removes new files, restores any you deleted)
Links
Reference
How To
- 2019-10-07 How To Rename a Local and Remote Git Branch
- Basically: «git branch -m <new_name>» renames the branch locally and doesn't mess with anything upstream. It's like you just created <new_name> and put stuff in it.
- Note: if the branch is not in the local repository, then you just need to git fetch it after the
git checkout <old_name>
. - Also: «git branch -c <new_name>» to copy instead of renaming.