git 基本命令
For many of us, git and GitHub play a huge role in our development workflows. Whenever we have a tool that we need to use often, the more fine-tuned we can make that tool, the faster we can get things done. The following are five git commands or helpers that can make your developer life much better!
对于我们许多人来说,git和GitHub在我们的开发工作流程中扮演着重要角色。 每当我们拥有需要经常使用的工具时,对它的调整越精细,我们完成工作的速度就越快。 以下是五个git命令或帮助程序,它们可以使您的开发人员的生活更加美好!
快速下拉请求请求
Reviewing code is as valuable or more valuable than writing your own code, as it benefits the coders around you. Instead of adding remotes, use this alias to quickly pull down pull requests:
审查代码比编写自己的代码有价值或有价值,因为它使您周围的编码人员受益。 不用添加遥控器,而是使用该别名快速下拉请求请求 :
git config --global --add alias.pr '!f() { git fetch -fu ${2:-upstream} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'
git config --global --add alias.pr-clean '!git checkout master ; git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'
~/Projects/debugger.html (master) $ git pr 4862
From https://github.com/devtools-html/debugger.html
* [new ref] refs/pull/4862/head -> pr/4862
Switched to branch 'pr/4862'
This is useful for reviewing both colleague and contributor pull requests!
这对于审查同事和贡献者的请求很有用!
在命令行上显示git Branch
Making sure you're on the desired git branch is critical, leading devs to typing git branch
a million times a day. With this trick, you can always have the branch name display in the command prompt:
确保您位于所需的git分支上至关重要,这导致开发人员每天要输入一百万次git branch
。 使用此技巧,您始终可以在命令提示符下显示分支名称:
# Show current git branch in command line
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
![Command Line Branch](https://davidwalsh.name/demo/command-line-git-branch.png)
This is a major qualify of developer life improvement!
这是改善开发人员生活的主要条件!
git分支自动完成
I try to construct my branch names like {bug number}-some-description
-- it makes browsing of branches easy but makes typing them out cumbersome. That's why I set up git autocompletion:
我尝试构造我的分支名称,例如{bug number}-some-description
它使浏览分支变得容易,但使键入它们变得麻烦。 这就是为什么我设置git autocompletion的原因 :
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
Now I can hit the tab key and the branch name will autocomplete!
现在,我可以按Tab键,分支名称将自动完成!
从命令行创建要点
Oftentimes I'll want to get a patch started for a contributor or simple use a few code changes to illustrate a point I'm trying to make. With gist-diff
, I can do that!
通常,我想为贡献者启动补丁程序,或者简单地使用一些代码更改来说明我要提出的观点。 有了 gist-diff
,我可以做到 !
# Install utility
npm install gist-diff
# Send changes to GitHub to create dist
gist-diff
This utility takes some minor setup but it's worth it!
该实用程序需要一些小的设置,但值得!
删除合并的分支
If you like keeping your git branches well organized, here's a fun script to purge branches that have been merged into master:
如果您希望使git分支井井有条,请使用以下有趣的脚本清除已合并到master的分支 :
[alias]
delete-merged-branches = "!f() { git checkout --quiet master && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
Voila! No more unnecessary branches cluttering your branch list!
瞧! 不再有不必要的分支使您的分支列表混乱!
These commands and utilities have made my git and development life much more enjoyable and proficient. Do you have any git tips you'd like to share! Please do so in the comments below!
这些命令和实用程序使我的git和开发生活变得更加愉快和熟练。 您有任何要分享的git提示! 请在下面的评论中这样做!
翻译自: https://davidwalsh.name/5-essential-git-commands-and-utilities
git 基本命令