git 别名
Git is an intricate part of our development workflow. There are handful of commands that you keep repeating day in and day out. I always relied on the command suggestions, or packages on top of my shell that gave access to handy git aliases.
Git是我们开发工作流程中复杂的一部分。 您每天都会重复执行一些命令。 我始终依靠命令建议或外壳程序之上的可访问方便的git别名的软件包。
But usually you had to stick with the aliases decided by the package creators . Although most of the aliases included are unofficially globally accepted like ga
for git add
and so on.
但是通常,您必须坚持由包创建者决定的别名。 尽管其中包括的大多数别名在全球范围内都是非官方接受的,例如ga
for git add
等。
But guess what? You don’t have to rely on any third party packages ; you can create your own with the aliases you prefer!
但猜猜怎么了? 您不必依靠任何第三方软件包; 您可以使用自己喜欢的别名创建自己的别名!
有两种创建Git别名的方法:✌ (There are two ways of creating Git aliases: ✌)
使用git config实用程序💻 (with the git config utility 💻)
This is the preferred way of adding aliases as git
gives you an option to do so. Suppose you feel tired of repeating the commit command every now and then. It would be nice if we could create an alias to write our commits faster.
这是添加别名的首选方法,因为git
提供了这样做的选项。 假设您不时地重复执行commit命令。 如果我们可以创建一个别名来更快地编写提交,那将是很好的。
The above command follows the following syntax:
上面的命令遵循以下语法:
Now we can use the c
alias to represent commit -m
.
现在我们可以使用c
别名来表示commit -m
。
git c "Update readme with social links"
编辑.gitconfig文件📝 (editing the .gitconfig file 📝)
It would be tedious to add multiple aliases with the git config
command, so there's an easier alternate approach.
使用git config
命令添加多个别名会很麻烦,因此有一种更简便的替代方法。
All the alias you create is saved to the .gitconfig
file sitting in your home directory. We can open the file and add our aliases following the TOML formatting. Make sure you do not modify anything in the file apart from adding the [alias]
table and its contents.
您创建的所有别名都会保存到主目录中的.gitconfig
文件中。 我们可以打开文件并按照TOML格式添加别名。 确保除了添加[alias]
表及其内容外,不要修改文件中的任何内容。
Open the file using your favorite editor.
使用您喜欢的编辑器打开文件。
vim ~/.gitconfig # or code ~/.gitconfig
Start adding your alias ✍️
开始添加别名✍️
You can use the above aliases as follows:
您可以按以下方式使用上述别名:
git st # git status
git c "hello world" # git commit -m "hello world"
git a hallucination.py # git add hallucination.py
git cb multi-stage-build # git checkout -b multi-stage-build
There’s also a handy command: git config --list
to view the contents of the file including other git configurations.
还有一个方便的命令: git config --list
可以查看文件的内容,包括其他git配置。
奖金🛸 (Bonus 🛸)
使用参数的Git别名! (Git alias with parameters!)
We can take our alias a step further by using shell script to add parameters.
通过使用shell脚本添加参数,我们可以使别名更进一步。
Where do we benefit from the parameters? Let’s take an example of adding a git remote to our local repository. There are basically two variables in the command that I can pass as a parameter, namely, the remote name and project name.
我们从哪里受益? 让我们以向本地存储库添加git远程为例。 我可以在命令中传递两个基本变量作为参数,即远程名称和项目名称。
git remote add <remote-name> git@github.com:yankeexe/<project-name>.git
We can represent the above abstraction using an anonymous bash function f()
as follows:
我们可以使用匿名bash函数f()
表示上述抽象,如下所示:
The $1
and $2
is the order in which the parameter will be used by the function. !
represents a shell script; our anonymous function is f()
which we have invoked immediately at the end. Let's use our alias:
$1
和$2
是函数将使用参数的顺序。 !
代表一个shell脚本; 我们的匿名函数是f()
,我们在最后立即调用了它。 让我们使用别名:
git ra origin demo-project
Here origin will be used in $1
since it's the first parameter we are passing and $2
will be demo-project. The above command will translate to:
这里的origin将在$1
使用,因为它是我们传递的第一个参数,而$2
将是demo-project 。 上面的命令将转换为:
git remote add origin git@github.com:yankeexe/demo-project.git
结论🚀 (Conclusion 🚀)
I hope this article has been of help in improving your Git workflow. If you have any queries or suggestions, let’s discuss in the comment section.
希望本文对改善您的Git工作流程有所帮助。 如果您有任何疑问或建议,请在评论部分进行讨论。
Originally published at https://dev.to on July 25, 2020.
最初于 2020年7月25日 在 https://dev.to 上 发布 。
翻译自: https://medium.com/@yankee.exe/faster-git-workflow-with-git-aliases-93d2400e9c28
git 别名