Git + Shell share a few combinations to enhance the work efficiency tips

Foreword

This article will record what I think the more practical Git + Shell (because I do not Sourcetree and other graphical tools, command line more efficient, but also universal value a little higher) tips, or it can be said that the show operation, First, in order to be your own memorandum, the second is to share with needy students ~

skill

Batch pull all the remote branch

Usually we'll get this all tracked remote branch update

git remote update
git pull --all
复制代码

If you want to get all the remote branches and created locally (Sometimes pulling Github open source project learning, often need to pull all branches), the following command can help you lazy.

for remote in `git branch -r `; do git branch --track $remote; done
复制代码

Fast switching branch

Is there a pain, that is, when switching branch, must write the whole name to write on the switch, if the branch were long and boring, then it ... it can only copy and paste!

But wait a minute! Open the file .gitconfig

open ~/.gitconfig
复制代码

In which enter:

[alias]
  find-branch = !sh -c \"git branch -a | grep -v remotes | grep $1 | head -n 1 | xargs git checkout\"
复制代码

Once saved, this time can be entered directly in the command line the first few letters of the name of a branch on the line (only you ensure uniqueness, otherwise the default first result of the match found)

As shown, input git find-branch ${shortcut}this form, the trend can be quickly switched.

(Determined students found, and imagination in the .bash_profile in the .gitconfig, increase the variety of alias alias, you can quickly improve efficiency)

Git command batch execution on the part of the file

Sometimes we need to carry out some file batch operation Git, and Git itself does not necessarily meet the needs of the time. You can refer to the following command:

git status -s | grep "README\.md" | sed 's/A //' | while read i; do git reset HEAD $i; done
git status -s | grep "README\.md" | sed 's/M //' | while read i; do git checkout --ours $i; done
复制代码

The above command is divided into two steps, first of all in this project README.mdfile restore from scratch to the work area, the second is this project all the README.mdconflicts of the batch file instead retain their own changes.

The above command after a certain modification in fact, meet a lot of scenes! So do not be locked out.

tips

The following excerpt from the Internet, use the specific command before self retrieved ~

xargs

xargs command is passed to a filter parameter of other commands, but also a combination of a plurality of tool commands. It excels converts input data into a standard command line parameters, xargs or stdin pipe capable of processing and converts it into a command specific command parameters.

sh

sh shell command is a command language interpreter, execute the command or read from a file read from the standard input.

grep

grep (global search regular expression (RE) and print out the line, comprehensive search regular expression and print out the line) is a powerful text search tool, you can use a regular expression search text, and print the matching lines come out.

and

sed can be treated in accordance with the instructions of the script, edit the text file. To automatically edit one or more documents, repeated operation of the document to simplify the preparation conversion program

Want to know more can refer to before I wrote this: Shells command-line study notes

Reproduced in: https: //juejin.im/post/5d060eff51882502ec1a76b8

Guess you like

Origin blog.csdn.net/weixin_34067102/article/details/93167314