This bash script aims to simplify your workflow within Git by offering a convenient way to manage branches.
Setup
- Download the script in https://github.com/ivanyu199012/14-gsw/blob/master/gsw.sh
- Run
source gsw.sh
in the terminal of your linux machine,
Feature
1. gsw
--> Change git branch using no.
- Type
gsw
and it lists all available local branches and ask you to input the branch no., which you would like to switch to. Note that branch marked with * is your current branch.
$ gsw
1. master
2. * test-branch-1
3. test-branch-2
4. test-branch-3
5. test-branch-4
6. test-branch-5
Enter branch no.:
- Enter the branch no., e.g.
4
fortest-branch-3
, and pressenter
. Then it switches to the branchtest-branch-3
;
$ gsw
1. master
2. * test-branch-1
3. test-branch-2
4. test-branch-3
5. test-branch-4
6. test-branch-5
Enter branch no.: 4
Switched to branch 'test-branch-3'
$ git branch
master
test-branch-1
test-branch-2
* test-branch-3
test-branch-4
test-branch-5
2. gsw -m
--> Fast switch back to master
branch
- use
gsw -m
to switch back to master branch directly
$ gsw -m
Switched to branch 'master'
$ git branch
* master
test-branch-1
test-branch-2
test-branch-3
test-branch-4
test-branch-5
3. gsw -c <branch name>
--> create new branch
- use
gsw -c <branch name>
to create new branch
$ gsw -c test-new-branch
Switched to a new branch 'test-new-branch'
$ git branch
master
test-branch-1
test-branch-2
test-branch-3
test-branch-4
test-branch-5
* test-new-branch
Summary
Command | Description |
---|---|
gsw + no. |
Change git branch using no. |
gsw -m |
Switch to master branch |
gsw -c <branch name> |
Create a branch with name <branch name>
|
Reason I wrote this blog
How do you switch git branch? Most of you may follow below steps:
- Use
git branch
to list all the branches - Find your desired branch and copy the name
- Type
git checkout
orgit checkout
and then paste the name
Before developed this script, I did above steps a lot of times every day and found it very troublesome. I asked myself:
- Why do I type 2 commands and use my mouse to copy the branch name for just switching branch?
- Can I switch branch using number instead of name?
As a result, the script was made and hope that it can help you to streamline git branch management.🙂
Top comments (0)