preface

This article will record I feel more practical Git+Shell (because the author does not use Sourcetree and other graphic chemical tools, command line efficiency is higher, at the same time the general value is also a bit higher) small skills, or can also be said to be SAO operation, one is to give their own when the memo, the second is to share to the need of students ~

skills

Batch pull all remote branches

This is how we normally get all traced remote branch updates

git remote update
git pull --all
Copy the code

If you want to get all the remote branches and create them locally (sometimes when you pull the Github open source project, you often need to pull all the branches), the following command can help you get lazy.

for remote in `git branch -r `; do git branch --track $remote; done
Copy the code

Fast branching

Is there a kind of pain, is to switch branches, must write all write right name to switch, if the branch name is smelly and long, then… Then just copy and paste!

But wait! Open the.gitconfig file

open ~/.gitconfig
Copy the code

Enter:

[alias] find-branch = ! sh -c \"git branch -a | grep -v remotes | grep The $1 | head -n 1 | xargs git checkout\"
Copy the code

After saving, you can directly type the first few characters of the branch name on the command line (only if you make it unique, otherwise the default matches the first result found).

Git find-branch ${shortcut} git find-branch ${shortcut}

(For those of you who are interested, use your imagination in.gitConfig and.bash_profile to add aliases to quickly improve your work efficiency.)

Run Git commands on some files in batches

Sometimes you need to batch Git files, and Git itself may not meet this requirement. You can refer to the following commands:

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
Copy the code

This command is divided into two steps: one is to restore all readme. md files in this project from staging to workspace, and the other is to batch change all conflicting readme. md files in this project to keep their own changes.

The above commands can actually meet many scenarios after some modification! So don’t be boxed in.

The little knowledge

The following excerpts from the Internet, command specific use details can be searched ~

xargs

The xargs command is a filter for passing parameters to other commands and a tool for composing multiple commands. While xargs is good at converting standard input data into command line arguments, xargs can process pipes or STdin and convert them into command arguments for a particular command.

sh

The sh command is a shell command language interpreter that executes commands to read from standard input or from a file.

grep

Grep (Global Search Regular expression(RE) and print out the line) is a powerful text search tool. It can use regular expressions to search text and print out matching lines.

sed

Sed processes and edits text files according to script instructions. It is used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs

For more information see my previous post: Notes on Learning the Shells Command Line