8 ## Working with branches
14 git checkout -b $branchname
15 git push origin $branchname --set-upstream
18 Creates a new branch locally then pushes it.
20 ### Getting from remote
24 git checkout --track origin/$branchname
27 Gets a branch in a remote.
29 ### Delete local remote-tracking branches
32 git remote prune origin
35 Deletes `origin/*` branches in your local copy. Doesn't affect the remote.
37 ### List existing branches
43 Existing branches are listed. Current branch will be highlighted with an asterisk.
45 ### List merged branches
48 git branch -a --merged
51 List outdated branches that have been merged into the current one.
53 ### Delete a local branch
56 git branch -d $branchname
59 Deletes the branch only if the changes have been pushed and merged with remote.
61 ### Delete branch forcefully
64 git branch -D $branchname
68 git branch -d $branchname
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.
74 ### Delete remote branch
77 git push origin --delete :$branchname
87 ### Reset branch and remove all changes
93 ### Undo commits to a specific commit
96 git reset --hard $commit_id
98 # Now push safely to your branch
99 git push --force-with-lease
101 # Or push brutally to your branch