如何在git中使用别名?

时间:2022-10-31 03:38:37

I saw a screencast where someone had gotten

我看到有人在屏幕前出现。

git st
git ci

to work. When I do it I get an error asking me if I meant something else.
Being a git newb, I need to know what you have to do to get this done?

去工作。当我这样做时,我得到一个错误,问我是否意味着别的什么。作为一个git newb,我需要知道你要做什么才能完成这个任务?

16 个解决方案

#1


726  

Basically you just need to add lines to ~/.gitconfig

基本上,你只需要添加一行到~/.gitconfig。

[alias]
    st = status
    ci = commit -v

Or you can use the git config alias command:

或者您可以使用git配置别名命令:

$ git config --global alias.st status 

On unix, use single quotes if the alias has a space:

在unix上,如果别名有空格,则使用单引号:

$ git config --global alias.ci 'commit -v'

On windows, use double quotes if the alias has a space or a command line argument:

在windows上,如果别名有空格或命令行参数,则使用双引号:

c:\dev> git config --global alias.ci "commit -v"

The alias command even accepts functions as parameters. Take a look at aliases.

别名命令甚至接受函数作为参数。看看别名。

#2


141  

As others have said the appropriate way to add git aliases is in your global .gitconfig file either by editing ~/.gitconfig or by using the git config --global alias.<alias> <git-command> command

正如其他人所说的,添加git别名的适当方式是在您的全局.gitconfig文件中,通过编辑~/。gitconfig或使用git配置——全局别名。 <别名> < git命令>命令

Below is a copy of the alias section of my ~/.gitconfig file:

下面是我的~/的别名部分的副本。gitconfig文件:

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD

Also, if you're using bash, I would recommend setting up bash completion by copying git-completion.bash to your home directory and sourcing it from your ~/.bashrc. (I believe I learned about this from the Pro Git online book.) On Mac OS X, I accomplished this with the following commands:

另外,如果您使用bash,我建议通过复制git-completion来设置bash完成。bash到您的主目录并从您的~/.bashrc中获取它。(我相信我是在Pro Git的在线书籍中了解到这一点的。)在Mac OS X上,我用以下命令完成了这个任务:

# Copy git-completion.bash to home directory
cp usr/local/git/contrib/completion/git-completion.bash ~/

# Add the following lines to ~/.bashrc
if [ -x /usr/local/git/bin/git ]; then
    source ~/.git-completion.bash
fi

Note: The bash completion will work not only for the standard git commands but also for your git aliases.

注意:bash的完成不仅适用于标准的git命令,也适用于您的git别名。

Finally, to really cut down on the keystrokes, I added the following to my ~/.bash_aliases file, which is sourced from ~/.bashrc:

最后,为了真正减少击键次数,我在~/上增加了以下内容。bash_aliases文件,该文件来自~/.bashrc:

alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcotb='git checkout --track -b'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'

#3


50  

I think the most useful gitconfig is like this,we always use the 20% function in git,you can try the "g ll",it is amazing,the details:

我认为最有用的gitconfig是这样的,我们总是在git中使用20%的函数,你可以试试“g ll”,它很神奇,细节如下:

[user]
    name = my name
    email = me@example.com
[core]  
    editor = vi 
[alias]
    aa = add --all
    bv = branch -vv
    ba = branch -ra
    bd = branch -d
    ca = commit --amend
    cb = checkout -b
    cm = commit -a --amend -C HEAD
    ci = commit -a -v
    co = checkout
    di = diff
    ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
    ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
    ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
    mm = merge --no-ff
    st = status --short --branch
    tg = tag -a 
    pu = push --tags
    un = reset --hard HEAD  
    uh = reset --hard HEAD^
   [color]  
    diff = auto  
    status = auto  
    branch = auto 
   [branch]  
    autosetuprebase = always

#4


16  

You need the git config alias command. Execute the following in a Git repository:

您需要使用git配置别名命令。在Git存储库中执行以下操作:

git config alias.ci commit

For global alias:

为全球别名:

git config --global alias.ci commit

#5


7  

This will create an alias st for status:

这将为状态创建别名st:

git config --add alias.st status

git配置——添加别名。圣的地位

#6


7  

This worked for me:

这工作对我来说:

bco = "!f(){ git branch ${1} && git checkout ${1}; };f"

on:

:

$ git --version

git version 1.7.7.5 (Apple Git-26)

#7


5  

Follwing are the 4 git shortcuts or aliases youc an use to save time.

下面是四个git快捷方式或别名,用来节省时间。

Open the commandline and type these below 4 commands and use the shortcuts after.

打开命令行,在下面4个命令中键入这些命令,然后使用快捷键。

git config --global alias.co checkout  
git config --global alias.ci commit    
git config --global alias.st status    
git config --global alias.br branch  

Now test them!

现在测试!

$ git co              # use git co instead of git checkout
$ git ci              # use git ci instead of git commit
$ git st              # use git st instead of git status
$ git br              # use git br instead of git branch

#8


5  

You can alias both git and non-git commands. It looks like this was added in version 1.5. A snippet from the git config --help page on version 2.5.4 on my Mac shows:

您可以同时别名git和非git命令。它看起来是在1.5版中添加的。git配置的一个片段——在我的Mac上的版本2.5.4上的帮助页:

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

如果别名扩展以一个感叹号作为前缀,那么它将被当作一个shell命令来处理。

For example, in your global .gitconfig file you could have:

例如,在您的全局.gitconfig文件中:

[alias]
    st = status
    hi = !echo 'hello'

And then run them:

然后运行:

$ git hi
hello
$ git st
On branch master

...

#9


4  

$ git update
git: 'update' is not a git command. See 'git --help'.

Did you mean this?
    update-ref

$ git config --global alias.update 'pull -v'

$ git update
From git://git.kernel.org/pub/scm/git/git
 = [up to date]      html       -> origin/html
 = [up to date]      maint      -> origin/maint
 = [up to date]      man        -> origin/man
 = [up to date]      master     -> origin/master
 = [up to date]      next       -> origin/next
 = [up to date]      pu         -> origin/pu
 = [up to date]      todo       -> origin/todo
Already up-to-date.

#10


3  

For those looking to execute shell commands in a git alias, for example:

对于那些希望在git别名中执行shell命令的人,例如:

$ git pof

In my terminal will push force the current branch to my origin repo:

在我的终端将把当前的分支推到我的原点。

[alias]
    pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)

Where the

在哪里

$(git branch | grep \\* | cut -d ' ' -f2)

command returns the current branch.

命令返回当前分支。

So this is a shortcut for manually typing the branch name:

这是手动输入分支名称的快捷方式:

git push origin -f <current-branch>

#11


2  

Just to get the aliases even shorter than the standard git config way mentioned in other answers, I created an npm package mingit (npm install -g mingit) so that most commands would become 2 characters instead of 2 words. Here's the examples:

为了获得比其他答案中提到的标准git配置方法更短的别名,我创建了一个npm包mingit (npm安装-g mingit),这样大多数命令将变成两个字符而不是两个单词。这是例子:

g a .                   // git add .
g b other-branch        // git branch other-branch
g c "made some changes" // git commit -m "made some changes"
g co master             // git checkout master
g d                     // git diff
g f                     // git fetch
g i                     // git init 
g m hotfix              // git merge hotfix
g pll                   // git pull
g psh                   // git push
g s                     // git status

and other commands would be similarly short. This also keeps bash completions. The package adds a bash function to your dotfiles, works on osx, linux, and windows. Also, unlike the other aliases, it aliases git -> g as well as the second parameter.

其他命令也同样简短。这也使bash完成。该包在您的dotfiles中添加了bash函数,在osx、linux和windows上工作。另外,与其他别名不同,它可以别名git -> g和第二个参数。

#12


1  

It is given here Aliases.Even there are great answers here, I added this because it differs in windows and linux

这里给出了别名。即使在这里也有很好的答案,我添加了这个,因为它在windows和linux中是不同的。

#13


1  

You can also chain commands if you use the '!' operator to spawn a shell:

如果您使用“!”,您还可以使用“!”操作员产生一个外壳:

aa = !git add -A && git status

This will both add all files and give you a status report with $ git aa.

这将会添加所有的文件,并给您以$ git的状态报告。

For a handy way to check your aliases, add this alias:

为了方便地检查别名,请添加以下别名:

alias = config --get-regexp ^alias\\.

Then a quick $ git alias gives you your current aliases and what they do.

然后,一个快速的$ git别名可以为您提供当前的别名和它们所做的工作。

#14


1  

You can set custom git aliases using git's config. Here's the syntax:

您可以使用git的配置设置自定义的git别名。这里的语法:

git config --global alias.<aliasName> "<git command>"

For example, if you need an alias to display a list of files which have merge conflicts, run:

例如,如果您需要一个别名来显示包含合并冲突的文件列表,请运行:

git config --global alias.conflicts "diff --name-only --diff-filter=U"

Now you can use the above command only using "conflicts":

现在您可以只使用“冲突”来使用上述命令:

git conflicts
# same as running: git diff --name-only --diff-filter=U

#15


0  

Another possibility for windows would be to have a directory filled with .bat files that have your shortcuts in them. The name of the file is the shortcut to be used. Simply add the directory to your PATH environment variable and you have all the shortcuts to your disposal in the cmd window.

windows的另一种可能性是拥有一个包含了你的快捷方式的.bat文件的目录。文件的名称是要使用的快捷方式。只需将该目录添加到PATH环境变量中,就可以在cmd窗口中使用所有的快捷方式。

For example (gc.bat):

例如(gc.bat):

git commit -m %1

Then you can execute the following command in the console:

然后,您可以在控制台执行以下命令:

gc "changed stuff"

The reason I'm adding this as an answer is because when using this you aren't limited to git ... only commands.

我之所以把这个作为答案,是因为当使用这个时,你并不局限于git…只有命令。

#16


0  

If you want an alternative to the ~/.gitconfig option and open to digging in a little more, another option is to write entirely custom git commands by wrapping them in a global node package.

如果你想要替代~/。gitconfig选项和open将进一步挖掘,另一种选择是通过将它们封装在全局节点包中来编写完全定制的git命令。

In your package.json, you'd define the root command (example: gt), and then filter the specific commands to execute the correct git commands. For example, git checkout my-branch could be gt co mybranch.

在你的包。您将定义根命令(例如:gt),然后过滤特定的命令以执行正确的git命令。例如,git checkout my-branch可以是gt co mybranch。

The "christian-git" package on npm uses this method: https://github.com/alexmacarthur/christian-git

npm的“christian-git”包使用了这个方法:https://github.com/alexmacarthur/christian-git。

#1


726  

Basically you just need to add lines to ~/.gitconfig

基本上,你只需要添加一行到~/.gitconfig。

[alias]
    st = status
    ci = commit -v

Or you can use the git config alias command:

或者您可以使用git配置别名命令:

$ git config --global alias.st status 

On unix, use single quotes if the alias has a space:

在unix上,如果别名有空格,则使用单引号:

$ git config --global alias.ci 'commit -v'

On windows, use double quotes if the alias has a space or a command line argument:

在windows上,如果别名有空格或命令行参数,则使用双引号:

c:\dev> git config --global alias.ci "commit -v"

The alias command even accepts functions as parameters. Take a look at aliases.

别名命令甚至接受函数作为参数。看看别名。

#2


141  

As others have said the appropriate way to add git aliases is in your global .gitconfig file either by editing ~/.gitconfig or by using the git config --global alias.<alias> <git-command> command

正如其他人所说的,添加git别名的适当方式是在您的全局.gitconfig文件中,通过编辑~/。gitconfig或使用git配置——全局别名。 <别名> < git命令>命令

Below is a copy of the alias section of my ~/.gitconfig file:

下面是我的~/的别名部分的副本。gitconfig文件:

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD

Also, if you're using bash, I would recommend setting up bash completion by copying git-completion.bash to your home directory and sourcing it from your ~/.bashrc. (I believe I learned about this from the Pro Git online book.) On Mac OS X, I accomplished this with the following commands:

另外,如果您使用bash,我建议通过复制git-completion来设置bash完成。bash到您的主目录并从您的~/.bashrc中获取它。(我相信我是在Pro Git的在线书籍中了解到这一点的。)在Mac OS X上,我用以下命令完成了这个任务:

# Copy git-completion.bash to home directory
cp usr/local/git/contrib/completion/git-completion.bash ~/

# Add the following lines to ~/.bashrc
if [ -x /usr/local/git/bin/git ]; then
    source ~/.git-completion.bash
fi

Note: The bash completion will work not only for the standard git commands but also for your git aliases.

注意:bash的完成不仅适用于标准的git命令,也适用于您的git别名。

Finally, to really cut down on the keystrokes, I added the following to my ~/.bash_aliases file, which is sourced from ~/.bashrc:

最后,为了真正减少击键次数,我在~/上增加了以下内容。bash_aliases文件,该文件来自~/.bashrc:

alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcotb='git checkout --track -b'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'

#3


50  

I think the most useful gitconfig is like this,we always use the 20% function in git,you can try the "g ll",it is amazing,the details:

我认为最有用的gitconfig是这样的,我们总是在git中使用20%的函数,你可以试试“g ll”,它很神奇,细节如下:

[user]
    name = my name
    email = me@example.com
[core]  
    editor = vi 
[alias]
    aa = add --all
    bv = branch -vv
    ba = branch -ra
    bd = branch -d
    ca = commit --amend
    cb = checkout -b
    cm = commit -a --amend -C HEAD
    ci = commit -a -v
    co = checkout
    di = diff
    ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
    ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
    ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
    mm = merge --no-ff
    st = status --short --branch
    tg = tag -a 
    pu = push --tags
    un = reset --hard HEAD  
    uh = reset --hard HEAD^
   [color]  
    diff = auto  
    status = auto  
    branch = auto 
   [branch]  
    autosetuprebase = always

#4


16  

You need the git config alias command. Execute the following in a Git repository:

您需要使用git配置别名命令。在Git存储库中执行以下操作:

git config alias.ci commit

For global alias:

为全球别名:

git config --global alias.ci commit

#5


7  

This will create an alias st for status:

这将为状态创建别名st:

git config --add alias.st status

git配置——添加别名。圣的地位

#6


7  

This worked for me:

这工作对我来说:

bco = "!f(){ git branch ${1} && git checkout ${1}; };f"

on:

:

$ git --version

git version 1.7.7.5 (Apple Git-26)

#7


5  

Follwing are the 4 git shortcuts or aliases youc an use to save time.

下面是四个git快捷方式或别名,用来节省时间。

Open the commandline and type these below 4 commands and use the shortcuts after.

打开命令行,在下面4个命令中键入这些命令,然后使用快捷键。

git config --global alias.co checkout  
git config --global alias.ci commit    
git config --global alias.st status    
git config --global alias.br branch  

Now test them!

现在测试!

$ git co              # use git co instead of git checkout
$ git ci              # use git ci instead of git commit
$ git st              # use git st instead of git status
$ git br              # use git br instead of git branch

#8


5  

You can alias both git and non-git commands. It looks like this was added in version 1.5. A snippet from the git config --help page on version 2.5.4 on my Mac shows:

您可以同时别名git和非git命令。它看起来是在1.5版中添加的。git配置的一个片段——在我的Mac上的版本2.5.4上的帮助页:

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

如果别名扩展以一个感叹号作为前缀,那么它将被当作一个shell命令来处理。

For example, in your global .gitconfig file you could have:

例如,在您的全局.gitconfig文件中:

[alias]
    st = status
    hi = !echo 'hello'

And then run them:

然后运行:

$ git hi
hello
$ git st
On branch master

...

#9


4  

$ git update
git: 'update' is not a git command. See 'git --help'.

Did you mean this?
    update-ref

$ git config --global alias.update 'pull -v'

$ git update
From git://git.kernel.org/pub/scm/git/git
 = [up to date]      html       -> origin/html
 = [up to date]      maint      -> origin/maint
 = [up to date]      man        -> origin/man
 = [up to date]      master     -> origin/master
 = [up to date]      next       -> origin/next
 = [up to date]      pu         -> origin/pu
 = [up to date]      todo       -> origin/todo
Already up-to-date.

#10


3  

For those looking to execute shell commands in a git alias, for example:

对于那些希望在git别名中执行shell命令的人,例如:

$ git pof

In my terminal will push force the current branch to my origin repo:

在我的终端将把当前的分支推到我的原点。

[alias]
    pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)

Where the

在哪里

$(git branch | grep \\* | cut -d ' ' -f2)

command returns the current branch.

命令返回当前分支。

So this is a shortcut for manually typing the branch name:

这是手动输入分支名称的快捷方式:

git push origin -f <current-branch>

#11


2  

Just to get the aliases even shorter than the standard git config way mentioned in other answers, I created an npm package mingit (npm install -g mingit) so that most commands would become 2 characters instead of 2 words. Here's the examples:

为了获得比其他答案中提到的标准git配置方法更短的别名,我创建了一个npm包mingit (npm安装-g mingit),这样大多数命令将变成两个字符而不是两个单词。这是例子:

g a .                   // git add .
g b other-branch        // git branch other-branch
g c "made some changes" // git commit -m "made some changes"
g co master             // git checkout master
g d                     // git diff
g f                     // git fetch
g i                     // git init 
g m hotfix              // git merge hotfix
g pll                   // git pull
g psh                   // git push
g s                     // git status

and other commands would be similarly short. This also keeps bash completions. The package adds a bash function to your dotfiles, works on osx, linux, and windows. Also, unlike the other aliases, it aliases git -> g as well as the second parameter.

其他命令也同样简短。这也使bash完成。该包在您的dotfiles中添加了bash函数,在osx、linux和windows上工作。另外,与其他别名不同,它可以别名git -> g和第二个参数。

#12


1  

It is given here Aliases.Even there are great answers here, I added this because it differs in windows and linux

这里给出了别名。即使在这里也有很好的答案,我添加了这个,因为它在windows和linux中是不同的。

#13


1  

You can also chain commands if you use the '!' operator to spawn a shell:

如果您使用“!”,您还可以使用“!”操作员产生一个外壳:

aa = !git add -A && git status

This will both add all files and give you a status report with $ git aa.

这将会添加所有的文件,并给您以$ git的状态报告。

For a handy way to check your aliases, add this alias:

为了方便地检查别名,请添加以下别名:

alias = config --get-regexp ^alias\\.

Then a quick $ git alias gives you your current aliases and what they do.

然后,一个快速的$ git别名可以为您提供当前的别名和它们所做的工作。

#14


1  

You can set custom git aliases using git's config. Here's the syntax:

您可以使用git的配置设置自定义的git别名。这里的语法:

git config --global alias.<aliasName> "<git command>"

For example, if you need an alias to display a list of files which have merge conflicts, run:

例如,如果您需要一个别名来显示包含合并冲突的文件列表,请运行:

git config --global alias.conflicts "diff --name-only --diff-filter=U"

Now you can use the above command only using "conflicts":

现在您可以只使用“冲突”来使用上述命令:

git conflicts
# same as running: git diff --name-only --diff-filter=U

#15


0  

Another possibility for windows would be to have a directory filled with .bat files that have your shortcuts in them. The name of the file is the shortcut to be used. Simply add the directory to your PATH environment variable and you have all the shortcuts to your disposal in the cmd window.

windows的另一种可能性是拥有一个包含了你的快捷方式的.bat文件的目录。文件的名称是要使用的快捷方式。只需将该目录添加到PATH环境变量中,就可以在cmd窗口中使用所有的快捷方式。

For example (gc.bat):

例如(gc.bat):

git commit -m %1

Then you can execute the following command in the console:

然后,您可以在控制台执行以下命令:

gc "changed stuff"

The reason I'm adding this as an answer is because when using this you aren't limited to git ... only commands.

我之所以把这个作为答案,是因为当使用这个时,你并不局限于git…只有命令。

#16


0  

If you want an alternative to the ~/.gitconfig option and open to digging in a little more, another option is to write entirely custom git commands by wrapping them in a global node package.

如果你想要替代~/。gitconfig选项和open将进一步挖掘,另一种选择是通过将它们封装在全局节点包中来编写完全定制的git命令。

In your package.json, you'd define the root command (example: gt), and then filter the specific commands to execute the correct git commands. For example, git checkout my-branch could be gt co mybranch.

在你的包。您将定义根命令(例如:gt),然后过滤特定的命令以执行正确的git命令。例如,git checkout my-branch可以是gt co mybranch。

The "christian-git" package on npm uses this method: https://github.com/alexmacarthur/christian-git

npm的“christian-git”包使用了这个方法:https://github.com/alexmacarthur/christian-git。