git 命令 显示 分支_从命令行显示git分支

git 命令 显示 分支

Whether it's simply submitting pull requests or being snobby enough to use vim as a text editor, web developers and designers spend an awful lot of time working from command line.  If you do work with git, you know it's important to keep track of your branches, especially when it comes to knowing which branch you're currently on.

无论是简单地提交拉取请求,还是为了使用vim作为文本编辑器而之以鼻,Web开发人员和设计人员都花费大量时间在命令行上工作。 如果您确实使用git,那么就知道跟踪分支非常重要,尤其是要知道您当前在哪个分支上时。

You could frequently execute git branch to ensure you're on the branch you'd prefer to be on, but that's a lot of unnecessary repetition.  After all, with the amount of work we do from command line, there should be a way to always display that information...and there is.  Let me show you how to always show the current checked out branch within the command line display!

您可以经常执行git branch以确保您位于希望加入的分支上,但这是很多不必要的重复。 毕竟,通过我们在命令行中完成的大量工作,应该有一种始终显示该信息的方法。 让我向您展示如何始终在命令行显示中显示当前已检出的分支!

Start by opening your .bash_profile file -- this file is typically used to create command line aliases and set environment variables.  Add the following to the .bash_profile file:

首先打开.bash_profile文件-该文件通常用于创建命令行别名和设置环境变量。 将以下内容添加到.bash_profile文件中:


# Slightly modified from: https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

# 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\] $ "


PS1 represents the leading text display before each line execute in the command line.  With the directive above your command line will display like:

PS1表示在命令行中每行执行之前的前导文本显示。 使用上面的指令,命令行将显示如下:

git 命令 显示 分支_从命令行显示git分支_第1张图片

Always seeing the current branch name (if any) is a time saver and blanket of security for those of use who use git for our projects.  Do yourself the favor of adding this tiny snippet to your .bash_profile and thank me later!

对于那些在我们的项目中使用git的用户而言,始终看到当前的分支名称(如果有的话)可以节省时间和安全。 帮自己把这个小片段添加到您的.bash_profile ,稍后再感谢我!

Are you a Windows user? Check out posh-git to achieve the same functionality!

您是Windows用户吗? 查看posh-git以实现相同的功能!

翻译自: https://davidwalsh.name/show-git-branch-command-line

git 命令 显示 分支

你可能感兴趣的:(git 命令 显示 分支_从命令行显示git分支)