Linux See git Create branch, delete local branch, view remote branch, local branch example

Source: Internet
Author: User


1 View Remote Branch

$ git branch-a

* br-2.1.2.2

Master

Remotes/origin/head-> Origin/master

remotes/origin/br-2.1.2.1

remotes/origin/br-2.1.2.2

remotes/origin/br-2.1.3

Remotes/origin/master

2 View Local Branch

$ git Branch

* br-2.1.2.2

Master

3 Creating a Branch


Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git Branch test

$ git Branch

* br-2.1.2.2

Master

Test

4 Switch Branch to test


Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git Branch

* br-2.1.2.2

Master

Test

Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git checkout test

M Jingwei-server/src/main/java/com/taobao/jingwei/server/service/cmd/getcustomertarcmd.java

M Jingwei-server/src/main/java/com/taobao/jingwei/server/util/serverutil.java

Switched to branch ' test '

Shuohailhl@shuohailhl-pc/f/ggg/jingwei (Test)

$ git Branch

br-2.1.2.2

Master

* Test

5 Delete local branch git branch-d xxxxx


$ git checkout br-2.1.2.2

M Jingwei-server/src/main/java/com/taobao/jingwei/server/service/cmd/getcustomertarcmd.java

M Jingwei-server/src/main/java/com/taobao/jingwei/server/util/serverutil.java

Switched to branch ' br-2.1.2.2 '

Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git BR

* br-2.1.2.2

Master

Test

Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git br-d test

Deleted Branch Test (was 17d28d9).

Shuohailhl@shuohailhl-pc/f/ggg/jingwei (br-2.1.2.2)

$ git BR

* br-2.1.2.2

Master

6 view local and remote branch-a. The branch of your current working directory on behalf of the front with the * number


Shuohailhl@shuohailhl-pc/f/ggg/jingwei (Test)

$ git branch-a

br-2.1.2.2

Master

* Test

Remotes/origin/head-> Origin/master

remotes/origin/br-2.1.2.1

remotes/origin/br-2.1.2.2

remotes/origin/br-2.1.3

Remotes/origin/master


Bulk Delete Git branch

Primary Magic:

To delete a local, consider the following three points first

List all local branches
Search for target branches such as: all branches containing ' dev '
Pass the results of the search to the delete function
So we can get:

git br |grep ' dev ' |xargs git br-d
Pipeline

Frequently used for communication between commands, such as where you can pass the results of git br to grep ' Dev ', which is fairly simple to use as long as ' | ' Connect two commands and a similar PS-EF | grep ' Uwsgi ' 's command combination to find Uwsgi's town

Grep

grep can be used to find matching results in a file or stream, such as grep ' abc ' **/**.py find all files containing ' ABC ' strings in the current directory and print out the results

Xargs

Passes the execution result of the previous command as an argument to the latter command. Here, we pass all the branch names with the ' dev ' character to git br-d

Advanced Magic:

To remove a remote branch git command is: Git push origin:d EV equivalent to push an empty branch covers a remote branch so can remote reference local deletion to do? --The general direction is right, not just get the branch name, pass to delete function to execute

Git br-r| grep ' Dev ' |xargs git push origin:
But the results of the execution were clearly inconsistent with my expectations, and the system's last command was similar to this:

Git push origin Origin/dev/fix:
Obviously Xargs is passing the parameters to the previous one, not the last one we expected. The solution is?

Xargs-i {} func: {}
Use {} to indicate the location of the passed parameter fill so we have:

Git br-r | grep ' Dev ' | Xargs-i {} Git push origin: {}

Done? --not naturally. System execution is: Git push origin:origin/dev/fix originally grep out of the results although it is the branch we need, but it contains origin, obviously git command is not so smart to recognize so we can only work harder to remove the origin in front. For a long time, the command to meet the requirements of sed and awk, let's try awk here.

Awk

Awk is an ancient artifact. Its usage, can write a book ... Because of the complexity of the grammar, took a little temporary learning. Our purpose is simply to delete the origin and see how to kill the chicken with a sledgehammer awk-f ' [/] '/1\/master/{printf '%s/%s/%s\n ', $2,$3,$4} '-F, set separator to/, match all 1/ The branch name of master (note escape/), formatted output Origin/1/master/fix split, and origin,$3 represents master and so on. So format the output in printf

Well around so many laps, now finally can put all together, and then happy to delete the remote that more than 20 branches

git br-r |awk-f ' [/] '/1\/master/{printf '%s/%s/%s\n ', $2,$3,$4} ' |xargs-i {} Git push origin: {}
Note: BR is the abbreviation of branch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.