git bash快捷键_Bash快捷方式可增强您的Git工作流程

git bash快捷键

by Buddy Reno

由Buddy Reno

Bash快捷方式可增强您的Git工作流程 (Bash Shortcuts to Enhance Your Git Workflow)

The more you work with Git, the faster you’ll learn its workflows.

您与Git的合作越多,学习它的工作流程就越快。

Here are some of the tasks that me and my team do daily:

以下是我和我的团队每天执行的一些任务:

  1. Create and name branches

    创建和命名分支
  2. Count commits for squashing

    计数提交以进行压缩
  3. Update master to the latest version, then rebase it onto a branch

    将master更新到最新版本,然后将其重新建立到分支上

But each of these tasks requires multiple steps. This got me thinking: there has to be a better way to do this.

但是这些任务中的每一个都需要多个步骤。 这让我开始思考: 必须有一种更好的方法来做到这一点

Thankfully, there is a better way! By learning a little Bash, you can to create Git aliases that will save you tons of time.

幸运的是,有更好的方法! 通过学习一点Bash,您可以创建Git别名,从而节省大量时间。

首先要做的事情:Git的! 旗 (First Things First: Git’s ! Flag)

Have you ever seen a Git alias that has an exclamation mark at the beginning? For example:

您是否见过开头带有感叹号的Git别名? 例如:

somealias = “!......some code”

According to the Git’s documentation, If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

根据Git的文档如果别名扩展名带有感叹号作为前缀,它将被视为shell命令。

Hey, that’s neat! We can use this to our advantage, and add some intelligence to our aliases. Let’s try a simple example first and build up the complexity further on.

嘿,很干净! 我们可以利用这个优势,并为别名添加一些智能。 让我们先尝试一个简单的示例,然后进一步建立复杂性。

Open up your ~/.gitconfig file in your favorite text editor and add the following alias:

在您喜欢的文本编辑器中打开~/.gitconfig文件,并添加以下别名:

hello = "!echo \"Hello World\""# (the backslashes are for escaping the quotes.)

Now when you run $ git hello in your terminal, you’ll get Hello World as the output. Awesome! Armed with this knowledge, let’s walk through the 3 examples outlined above and the aliases I’ve used to accomplish them.

现在,当您在终端中运行$ git hello时,您将获得Hello World作为输出。 太棒了! 掌握了这些知识之后,我们将逐步介绍上面概述的3个示例以及我用来实现它们的别名。

分支名称一致 (Consistent Branch Names)

Alias:

别名:

newb = "!f() { ticketnum=$1; branchName=$2; git checkout -b \"POD-${ticketnum}/${branchName}\"; }; f"

Usage:

用法:

# Creates a new branch named POD-573/my-new-feature$ git newb 573 my-new-feature

Key Commands:

关键命令:

  • Function: f(){}; f

    函数: f(){}; f f(){}; f

  • Parameters: $1, $2

    参数: $1$2

  • String Interpolation: ${ticketnum}, ${branchName}

    字符串插值: ${ticketnum}${branchName}

On my team, we prepend our branch names to match the card number in our ticketing system. For example: “POD-573/my-new-feature”. This works with a commit hook in the ticketing system to tie things together, so it’s important we stick with this system.

在我的团队中,我们在票务系统的前面加上分支名称以匹配卡号。 例如:“ POD-573 / my-new-feature”。 这可以与票务系统中的提交挂钩配合使用,以将事物捆绑在一起,因此,务必坚持使用此系统。

功能 (Function)

In bash, you can write a function like this: FunctionName(){}; FunctionName. Writing the function name after the function declaration is what runs the function. In my aliases, I’ve shortened the function name to simply f for brevity.

在bash中,您可以编写如下函数: FunctionName(){}; FunctionName FunctionName(){}; FunctionName 。 在函数声明之后写入函数名称是运行函数的原因。 为了方便起见,我以别名的形式将函数名称简称为f

When bash runs f, it will run all the code entered between the curly braces {}. In this case, the function is running git checkout -b “MESSAGE”.

当bash运行f ,它将运行花括号{}之间输入的所有代码。 在这种情况下,该函数正在运行git checkout -b “MESSAGE”

参量 (Parameters)

The parameters are what immediately follow the command. For example, the move command in bash:

参数是紧随命令之后的参数。 例如,bash中的move命令:

$ mv ./file.txt ./folder/file.txt

The first parameter the move command receives is ./file.txt. This parameter is automatically assigned to $1 in bash.

move命令接收的第一个参数是./file.txt 。 此参数将自动分配给bash中的$1

Likewise, ./folder/file.txt is assigned to $2. In the alias function, you can use this knowledge to assign more meaningful variable names for those parameters.

同样,./ ./folder/file.txt也分配给$2 。 在别名功能中,您可以使用此知识为这些参数分配更有意义的变量名称。

# expanded for readability!f() { # much more meaningful! ticketnum=$1;  branchName=$2; git checkout -b \"POD-${ticketnum}/${branchName}\";};f
字符串插值 (String Interpolation)

To use variables in bash, you simply put a dollar sign $ in front of the variable name, like: $ticketnum. In this case, the function is interpolating the variable into a string.

要在bash中使用变量,只需在变量名前加一个美元符号$ ,例如: $ticketnum 。 在这种情况下,函数会将变量插值到字符串中。

While bash does allow users to interpolate by using the variable directly, I prefer to use the same syntax many other programming languages use to do string interpolation ${variable}.

虽然bash允许用户直接使用变量进行插值,但我更喜欢使用许多其他编程语言用于字符串插值${variable}的相同语法。

Using the usage example above, When bash evaluates POD-${ticketnum}/${branchName}, it will expand to POD-573/my-new-feature.

使用上面的用法示例,当bash评估POD-${ticketnum}/${branchName} ,它将扩展为POD-573/my-new-feature

提交计数 (Commit Counting)

Alias:

别名:

count = "!f() { compareBranch=${1-master}; git rev-list --count HEAD ^$compareBranch; }; f"

Usage:

用法:

# Branch has made 5 commits since branching from master.$ git count # returns 5# Pass in a branch name to check instead of master$ git count dev

Key Commands:

关键命令:

  • Parameter Expansion: ${1-DEFAULT}

    参数扩展: ${1-DEFAULT}

  • Counting Revisions: rev-list --count

    计数修订: rev-list --count

参数扩展 (Parameter Expansion)

Similar to the string interpolation, notice that the compareBranch variable is now being assigned using the ${} syntax. This allows you to set a default value if no parameter is passed through to the command. In the alias, compareBranch=${1-master} will use master as the compareBranch if nothing was passed to the command.

与字符串插值类似,请注意,现在使用${}语法分配了compareBranch变量。 如果没有参数传递给命令,这使您可以设置默认值。 在别名中,如果未向命令传递任何内容,则compareBranch=${1-master}将使用master作为compareBranch

# assumes master$ git count# compareBranch is set to dev$ git count dev

You can check out the bash documentation for more on parameter expansion.

您可以查看bash 文档以获取有关参数扩展的更多信息。

计数修订 (Counting Revisions)

By default, Git’s rev-list command will return the SHAs associated with the given branch name. By using the --count flag, it will instead return the total number of commits for that particular branch. Since the goal is to get the number of new commits since branching from master (or some other branch), you need to pass in another branch name with the ^ operator.

默认情况下,Git的rev-list命令将返回与给定分支名称关联的SHA。 通过使用--count标志,它将返回该特定分支的提交总数。 由于目标是获取自master(或其他分支)分支以来的提交次数,因此您需要使用^运算符传递另一个分支名称。

$ git rev-list --count HEAD ^master

This command tells git I want the number of commits that are accessible from HEAD (the current branch) but NOT ACCESSIBLE from master.

这个命令告诉git 我想要可以从HEAD(当前分支)访问但不能从master访问的提交数量。

If you’ve made 5 commits after creating a branch from master, the command would return 5.

如果从master创建分支后进行了5次提交,则命令将返回5。

压扁X提交 (Squashing X Commits)

Alias:

别名:

squashbase = "!f() { branchName=${1-master}; commitCount=$(git count $branchName); git rebase -i HEAD~$commitCount; }; f"

Usage:

用法:

# Get the number of commits to squash# and start an interactive rebase.$ git squashbase# pass in an optional branch name.$ git squashbase dev

Key Command:

按键命令:

  • Command Substitution: $(COMMAND)

    命令替换: $(COMMAND)

命令替换 (Command Substitution)

This one is fun. The script again uses the optional branchName to substitute a branch for master, but the second parameter uses a new syntax $(). This is called “command substitution.” When Bash sees a command within the parenthesis, it will evaluate that statement and use the output as the value of the variable. For example x=$(echo “Hello”) will evaluate to x receiving the value of Hello.

这很有趣。 脚本再次使用可选的branchName代替master的分支,但是第二个参数使用新的语法$() 。 这称为“命令替换”。 当Bash在括号内看到命令时,它将评估该语句并将输出用作变量的值。 例如x=$(echo “Hello”)将求值x接收Hello的值。

In this case, the alias is calling back to the previous count alias to get the number of commits made since master. Assuming the current branch has made 5 commits since branching from master, running $ git squashbase will evaluate to $ git rebase -i HEAD~5. This command starts an interactive rebase with the latest 5 commits, giving you an opportunity to clean up your commits.

在这种情况下,别名将回调到先前的count别名,以获取自master以来的提交次数。 假设当前分支自master分支以来已进行了5次提交,则运行$ git squashbase$ git squashbase $ git rebase -i HEAD~5 $ git squashbase 。 该命令使用最近的5次提交启动交互式rebase,使您有机会清理提交。

更新母版并重新设置分支机构 (Updating Master and Rebasing The Branch)

Alias:

别名:

pullbase = "!f() { branchName=${1-master}; git checkout $branchName && git pull && git checkout - && git rebase -i $branchName; }; f"

Usage:

用法:

# Checkout the branch, pull it, check out the previous branch and rebase.$ git pullbase$ git pullbase dev

Key Commands:

关键命令:

  • Git’s dash shortcut: -

    Git的破折号捷径: -

  • Control command: &&

    控制命令: &&

短跑捷径 (Dash Shortcut)

There isn’t much to explain here. Basically, this will save you a lot of typing. The dash is a reference to the last branch you currently checked out — kind of like the recall button on a TV remote control.

这里没有太多要解释的。 基本上,这将为您节省很多输入时间 。 破折号是对您当前签出的最后一个分支的引用-类似于电视遥控器上的撤回按钮。

Consider the following example:

考虑以下示例:

# currently on branch dev$ git checkout master # now on master branch$ git checkout - # back on dev branch.$ git checkout - # back on master branch.
控制指令 (Control Command)

There are many ways to chain operations together in bash. Using the double ampersand && is one of my favorites.

有很多方法可以将操作链接到bash中。 使用双符号&&是我的最爱之一。

The benefit of this command is that it will stop processing if the previous command failed to complete. If git checks out master but fails pulling down latest, it will stop instead of pressing forward and rebasing potentially stale changes into your branch.

此命令的好处是,如果前一个命令未能完成,它将停止处理。 如果git检出master却未能成功删除最新版本,它将停止而不是向前推送并将潜在的过时更改重新部署到您的分支中。

The above alias will then execute the following steps, stopping if any fail:

上面的别名将执行以下步骤,如果失败则停止:

  1. Checkout the master branch (or given branch).

    检出master分支(或给定分支)。
  2. Update to latest.

    更新到最新。
  3. Checkout the branch you were previously on.

    检出您之前所在的分支。
  4. Interactive rebase the master branch into your current branch.

    交互式将master分支重新设置为当前分支。

My team requires all of our pull requests to be a single commit before they can be merged into master. So I run this command multiple times a day, and it’s saved me a lot of time since I started using it.

我的团队要求我们所有的请求请求都必须是一次提交,然后才能合并到主请求中。 因此,我每天多次运行此命令,自从我开始使用它以来,节省了很多时间。

You can read about other control commands in Bash’s documentation.

您可以在Bash的文档中了解其他控制命令。

等等,为什么要使用别名? (Wait, why use an alias?)

You may be asking, “Shouldn’t I just be writing bash profile scripts?”

您可能会问:“我不应该只是在编写bash配置文件脚本吗?”

Technically, you could do that. The advantage here is context. All of these commands are comprised of various Git commands. A function in your bash profile will run anywhere you type it.

从技术上讲,您可以这样做。 这里的优势是上下文 。 所有这些命令均由各种Git命令组成。 bash配置文件中的功能将在您键入它的任何位置运行。

By creating these scripts as Git aliases, you ensure that the commands will only be run in a git repo. Plus you don’t have to prepend your function names with git-Function or gitFunction just to “namespace” them. If you were going to do that, a Git alias is a better fit.

通过将这些脚本创建为Git别名,可以确保仅在git repo中运行命令。 另外,您不必在函数名称前加上git-FunctiongitFunction即可对它们进行“命名空间”。 如果您要这样做,则更适合使用Git别名。

I hope you’ve found some inspiration from my shortcuts, and learned how you can create some of your own.

希望您从我的快捷方式中找到了一些灵​​感,并了解了如何创建自己的一些东西。

If you’ve made some favorite aliases or other enhancements to your own git workflow, share them in the comments section so we can all explore them and potentially save ourselves lots of time and typing.

如果您对自己的git工作流程做了一些喜欢的别名或其他增强功能,请在评论部分中共享它们,以便我们所有人进行探索,并可能节省大量时间和打字时间。

翻译自: https://www.freecodecamp.org/news/bash-shortcuts-to-enhance-your-git-workflow-5107d64ea0ff/

git bash快捷键

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值