git 别名

As developers, we know Git very well, as it is a very important part of our daily activity. Software developers use it all the time. We can not spend a day without interacting with Git. We can run Git from the terminal or use some third party tools like Sourcetree.

作为开发人员,我们非常了解Git ,因为它是我们日常活动中非常重要的一部分。 软件开发人员一直在使用它。 不与Git互动,我们就无法度过一天。 我们可以从终端运行Git或使用某些第三方工具,例如Sourcetree。

But there are some terminal fans who always love to run Git from the terminal only. So for them, it is sometimes difficult to remember and write those long commands. Ohh no buddy!! It is very much a boring and time-consuming task to write long commands all the time ???.

但是,有些终端爱好者总是喜欢只从终端运行Git。 因此,对于他们而言,有时很难记住和编写那些冗长的命令。 哦,没有哥们! 一直都在编写长命令,这是一个无聊且耗时的任务。

So what should we do now???

那么我们现在该怎么办?

Okay, we should start looking for a shortcut for those long long commands.?‍?‍?‍

好的,我们应该开始为那些冗长的命令寻找快捷方式。

Look what we found: Git Alias. It has come as the rescuer for all.

看看我们发现了什么: Git Alias 。 它已成为所有人的救助者。

We all likely know what an alias is — it means a false name or nickname.

我们都可能知道别名是什么-它表示错误的名称或昵称

So using git alias, we can assign a nickname to a long git command. This is perfect. ?

因此,使用git alias ,我们可以为长git命令分配一个昵称。 太棒了。 ?

Now let’s try to find a place where we can write these nicknames.

现在,让我们尝试找到一个可以写这些昵称的地方。

Searching ? Searching ?Searching ?…

正在搜寻? 正在搜寻?正在搜寻?...

Yes, bash_profile is the place where we can write them.

是的, bash_profile是我们可以编写它们的地方。

怎么打开bash_profile? (How to open bash_profile ?)

From Terminal, we can easily open bash_profile by using the following command:

在Terminal中,我们可以使用以下命令轻松打开bash_profile

vim ~/.bash_profile

vim ~/.bash_profile

Now enter into insert mode in your vim editor by tapping i from the keyboard.✓

现在通过在键盘上点击i进入vim编辑器中的插入模式。

在bash_profile中创建您的第一个别名: (Create your first alias in bash_profile:)

The first program we use to write in any programming language is a Hello Worldprogram. Let’s not break this tradition — we are going to write our very first alias with a simple hello command.

我们用来编写任何编程语言的第一个程序是Hello World 程序。 我们不要破坏这一传统,我们将使用一个简单的hello命令来编写我们的第一个别名

Open bash_profile, and write the following line:

打开bash_profile ,并写以下行:

alias hello="echo Hello Boudhayan!! How are you?"

alias hello="echo Hello Boudhayan!! How are you?"

It says that we have created an alias named helloand assigns the right-hand side as the command to execute. So whenever we write hello in the terminal, it should execute the command assigned to it.

它说我们已经创建了一个名为hello别名 并将右侧指定为要执行的命令。 因此,每当我们在终端中写hello时,它都应执行分配给它的命令。

Save the changes and reload the bash_profile by using the following command:

保存更改,并使用以下命令重新加载bash_profile

source ~/.bash_profile

source ~/.bash_profile

Now if we type hello in the terminal, it prints Hello Boudhayan!! How are you?

现在,如果我们在终端中键入hello ,它将显示Hello Boudhayan!! How are you? Hello Boudhayan!! How are you?

Awesome!! ???

太棒了!! ???

So we have learned how to create an alias command in bash_profile.

因此,我们学习了如何在bash_profile中创建别名命令。

If we look closely, then we can relate to it. We can find some similarities with the variable declaration in any language. Yes, we already know about that, right?

如果我们仔细观察,就可以与之联系起来。 我们可以在任何语言中找到与变量声明的相似之处。 是的,我们已经知道了,对吧?

成为主要话题 (Coming to the main topic)

Now let’s create some git aliases to make our daily life easier and faster.?

现在,让我们创建一些git别名,使我们的日常生活更轻松,更快捷。

git clone

git clone

We use this command for cloning a remote repository to a local system.

我们使用此命令将远程存储库克隆到本地系统。

Though it’s a short command, we want to start learning git aliases by making it even shorter.?‍

尽管这是一个简短的命令,但我们希望通过使其更短来开始学习git别名。

Just like above, open bash_profile, write the below line and reload bash_profile. See the magic.☄️

就像上面一样,打开bash_profile,编写下面的行并重新加载bash_profile 看魔术.☄️

alias gc="git clone"

alias gc="git clone"

So from now on, for cloning a repository, we do not need to write this:

因此,从现在开始,对于克隆存储库,我们无需编写以下代码:

git clone <remote repository url>

git clone <remote repository url>

instead, we will use the below command for cloning purposes:

相反,我们将使用以下命令进行克隆:

gc <remote repository url>

gc <remote repository url>

Boom!! Your remote repository is successfully cloned into your local system.???

繁荣!! 您的远程存储库已成功克隆到本地系统中。

创建更多别名 (Create some more aliases)

We push our local commits to the development or master branch by using the below commands:

我们使用以下命令将本地提交推送到development或master分支:

git push origin developgit push origin master

git push origin develop git push origin master

Now, we can write shorter version like below:

现在,我们可以编写较短的版本,如下所示:

alias gpd="git push origin develop"alias gpm="git push origin master"

alias gpd="git push origin develop" alias gpm="git push origin master"

So from now, we will use gpd and gpmto push local commits to the development and master branch respectively.

所以从现在开始,我们将使用 gpd和gpm 将本地提交分别推送到development和master分支。

?????? Hurrah!! We have made it.??????

?????? 欢呼!! 我们做到了。

I have created some more git aliases which can be really useful in our programming life. Check them out:

我创建了更多的git别名,这些别名对我们的编程生活非常有用。 去看一下:

外壳功能: (Shell Function:)

We can also use the shell function to declare more complex git aliases. But to start with this, we need to know how to write a shell function.?

我们还可以使用shell函数来声明更复杂的 git别名 但是首先,我们需要知道如何编写一个shell函数。

It is very easy to write a shell functionwhich is like a normal C function.?

编写shell函数非常容易 就像普通的C函数。

function function_name() {         command1         command2         .......         commandn
}

Now let’s try this. This function will create a directory in the current path and then immediately move into that directory. We know the below commands already to make it happen:

现在让我们尝试一下。 此函数将在当前路径中创建一个目录,然后立即移入该目录。 我们已经知道以下命令可以实现:

mkdir <directory_name>cd <directory_name>

mkdir <directory_name> cd <directory_name>

We can compress those two commands by creating a simple function in bash_profile like below:

我们可以通过在bash_profile中创建一个简单的函数来压缩这两个命令,如下所示:

function mdm() {   mkdir -p $1   #here $1 is the first parameter to the function.   cd $1}

function mdm() { mkdir -p $1 #here $1 is the first parameter to the function. cd $1 }

Now reload the bash_profile source once and run the following:

现在,重新加载bash_profile源一次并运行以下命令:

mdm test

mdm test

It will create a directory named test in the current path and move to that directory. Cool!!?

它将在当前路径中创建一个名为test的目录,然后移至该目录。 凉!!?

高级Git别名 (Advanced Git Aliases)

To push the code in the remote branch, we need to make a commit with some message. Only then we can push to a branch. So basically this is a combination of two commands (commit and push). But we want to try the same with a single one-line command by writing a shell function for this. ?

为了将代码推送到远程分支中,我们需要使用一些消息进行提交。 只有这样我们才能推送到分支。 因此,基本上这是两个命令(commit和push)的组合。 但是我们想通过编写一个shell函数来用一个单行命令尝试相同的操作。

We can easily do this by writing a simple shell function. Open bash_profile and write the following the function:

我们可以通过编写一个简单的shell函数轻松地做到这一点。 打开bash_profile并编写以下函数:

function gcp() {      git commit -am "$1" && git push }

function gcp() { git commit -am "$1" && git push }

Reload the bash_profile once and use the command like below:

重新加载bash_profile一次,并使用如下命令:

gcp "initial commit"

gcp "initial commit"

Cool!! From now we can use this gcp command to commit and push in one shot.?

凉!! 从现在开始,我们可以使用此gcp命令来提交并推送一张照片。

In a development or feature branch, all the team members push their changes almost every day. So sometimes it is very difficult to find a particular commit among all the commits.

在开发或功能部门中,几乎所有团队成员都每天推送更改。 因此有时很难在所有提交中找到特定的提交。

To easily handle this type of situation, we can write a function which will search the commit logs for a particular message and return the commit.

为了轻松处理这种情况,我们可以编写一个函数,该函数将在提交日志中搜索特定消息并返回提交。

To do this, we will write a function like below:

为此,我们将编写如下的函数:

function gfc() {         git log --all --grep="$1"}

function gfc() { git log --all --grep="$1" }

Occasionally if we want to search for a commit by the commit message, then we can do it by using gfc:

有时,如果我们想通过提交消息来搜索提交,则可以使用gfc

gfc "<commit message>"

gfc "<commit message>"

结论: (Conclusion:)

So we have learned how to use shortcuts for git commands.

因此,我们学习了如何对git命令使用快捷方式。

May these aliases and functions save you from writing those long git commands and make your life easy and smooth. You can add your own aliases, functions and make modifications to them — no one’s permission is required except bash.???

也许这些别名和函数可以使您免于编写那些冗长的git命令,并使您的生活轻松愉快。 您可以添加自己的别名,函数并对其进行修改-除了bash之外,不需要任何人的许可。

??? Cheers!!! Thank you for reading!! ???

??? 干杯!!! 谢谢您的阅读!! ???

翻译自: https://www.freecodecamp.org/news/an-intro-to-git-aliases-a-faster-way-of-working-with-git-b1eda81c7747/

git 别名

git 别名_Git别名简介:使用Git的更快方法相关推荐

  1. Git Alias(git快捷命令别名设置)

    在用户文件夹新建 .bash_profile 文件,编辑如下内容保存.即可享受Git快捷命令 alias gpm='git push origin master' alias ...=../.. al ...

  2. Git和Gitee的简介与使用

    文章目录 一.Git的基础知识 Git 是什么 Git 的安装  初次运行 Git 前的配置 获取 Git 帮助 取得项目的 Git 仓库 如何通过 git clone 克隆仓库/项目 Git 仓库基 ...

  3. git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改

    git 覆盖本地修改 When you learn to code, sooner or later you'll also learn about Version Control Systems. ...

  4. amend用法 git 信息_Git 高级用法,你用过哪些了

    ‍ 文章来自:http://segmentfault.com/a/1190000021643071 作者:yuanchuang 点击加入:PHP自学中心交流③群 商务合作: 请加微信(QQ):2230 ...

  5. Git《一》简介及安装使用

    Git<一>简介及安装使用 零. 目录 简介 集中式VS分布式 安装Git 创建版本库 总结 一. 简介 世界上最先进的分布式版本控制系统 二. 集中式VS分布式 Git为分布式版本控制系 ...

  6. git如何安装aur_git系列:git 简介

    大家好,今天的主题是 git .本文会介绍什么是 git ,以及其的主要功能和实现原理.最后以一个常规的开发流程来举例说明如何正确使用 git . 作者简介: Faith(F君) 多年一线互联网后端开 ...

  7. git简介,git和SVN区别

    1.git简介 ,git与SVN区别                  Git是目前世界上最先进的分布式版本控制系统(没有之一). 那什么是版本控制系统?                       ...

  8. Git的简介与Git详细操作流程

    1.Git的简介 分布式和集中式的区别 git是分布式版本控制器 比较有名的版本控制器还有SVN,SVN是集中式的版本控制器. 集中式:集中式的版本库是集中存放在中央服务器的,每次都要从中央服务器取得 ...

  9. Git技能树(1):Git简介

    Git 技能树(1):Git 简介 什么是"版本控制"?我为什么要关心它呢? 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.在 GIT CODE 中 ...

最新文章

  1. 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
  2. 无人驾驶之车道线检测简易版
  3. 【Python】list 之 extend 和 append 的区别
  4. 写了一个操作XML文件的类
  5. 走近分形与混沌(part7)--三体与混沌
  6. what happened after a template is selected in Create with template button
  7. QEMU虚拟化加速方案 - KVM
  8. 注册表清除桌面垃圾图标
  9. 绝地求生鼠标宏怎么设置?
  10. 637道Java面试题(含答案)
  11. python自己制作视频_你还在为看电影发愁?Python制作全网视频播放工具!
  12. 大三计算机写学术论文,学院大三本科生在高水平国际会议发表学术论文
  13. Github上最热门的Java开源项目
  14. 量化投资03---小市值轮动因子---准备工作01
  15. 无线渗透笔记(二)-《使用Aircrack-ng破解握手包》
  16. 微信小程序-001-抽签功能-002-新建抽签
  17. cv2.destoryAllWindows()无效的解决方法
  18. python import pandas as pd_python – Pandas pd.Series.isin性能与集合与数组
  19. 微信小程序——new Date()显示NAN + 正则表达式
  20. 超简单修改权限破解typora beta版本(备忘)

热门文章

  1. 1218 标签的显示与隐藏
  2. django-验证码
  3. 小程序点击事件改变样式(普通js鼠标点击事件)
  4. Kubernetes 1.9集群使用traefik发布服务
  5. 前后端分离-从MVC到前后端分离
  6. [置顶]别羡慕别人的舒服,静下心来坚持奋斗!!!
  7. Windows Server 2008 R2 域控DOS命令
  8. CCF201509-4 高速公路(100分)
  9. 零基础也可现学苹果Swift语言?太傻太天真
  10. aix Mysql-Rpm puppet puppetAgent