OSDN Git Service

Regular updates
[twpd/master.git] / git-branch.md
1 ---
2 title: Git branches
3 category: Git
4 layout: 2017/sheet
5 updated: 2020-02-13
6 ---
7
8 ## Working with branches
9 {: .-three-column}
10
11 ### Creating
12
13 ```bash
14 git checkout -b $branchname
15 git push origin $branchname --set-upstream
16 ```
17
18 Creates a new branch locally then pushes it.
19
20 ### Getting from remote
21
22 ```bash
23 git fetch origin
24 git checkout --track origin/$branchname
25 ```
26
27 Gets a branch in a remote.
28
29 ### Delete local remote-tracking branches
30
31 ```bash
32 git remote prune origin
33 ```
34
35 Deletes `origin/*` branches in your local copy. Doesn't affect the remote.
36
37 ### List existing branches
38
39 ```bash
40 git branch --list
41 ```
42
43 Existing branches are listed. Current branch will be highlighted with an asterisk.
44
45 ### List merged branches
46
47 ```bash
48 git branch -a --merged
49 ```
50
51 List outdated branches that have been merged into the current one.
52
53 ### Delete a local branch
54
55 ```bash
56 git branch -d $branchname
57 ```
58
59 Deletes the branch only if the changes have been pushed and merged with remote.
60
61 ### Delete branch forcefully
62
63 ```bash
64 git branch -D $branchname
65 ```
66
67 ```bash
68 git branch -d $branchname
69 ```
70
71 > Note: You can also use the -D flag which is synonymous with --delete --force instead of -d. This will delete the branch regardless of its merge status.
72 > Delete a branch irrespective of its merged status.
73
74 ### Delete remote branch
75
76 ```bash
77 git push origin --delete :$branchname
78 ```
79
80 Works for tags, too!
81
82 ### Get current sha1
83
84 ```bash
85 git show-ref HEAD -s
86 ```
87 ### Reset branch and remove all changes
88
89 ```bash
90 git reset --hard
91 ```
92
93 ### Undo commits to a specific commit
94
95 ```bash
96 git reset --hard $commit_id
97
98 # Now push safely to your branch
99 git push --force-with-lease
100
101 # Or push brutally to your branch
102 git push --force
103 ```