code

git에서 별칭 명령을 어떻게 수행합니까?

codestyles 2020. 10. 3. 10:43
반응형

git에서 별칭 명령을 어떻게 수행합니까?


누군가가 얻은 스크린 캐스트를 봤어

git st
git ci

일하다. 내가 그것을 할 때 다른 것을 의미하는지 묻는 오류가 발생합니다.
git newb이기 때문에이 작업을 수행하려면 무엇을해야하는지 알아야합니다.


기본적으로 줄을 추가하면됩니다. ~/.gitconfig

[alias]
    st = status
    ci = commit -v

또는 git config alias 명령을 사용할 수 있습니다.

$ git config --global alias.st status 

Unix에서 별칭에 공백이 있으면 작은 따옴표를 사용합니다.

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

Windows에서 별칭에 공백이나 명령 줄 인수가 있으면 큰 따옴표를 사용합니다.

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

alias 명령은 함수를 매개 변수로 허용합니다. 별칭을 살펴보십시오 .


다른 사람들이 말했듯이 git 별칭을 추가하는 적절한 방법은 .gitconfig편집 ~/.gitconfig하거나 git config --global alias.<alias> <git-command>명령 을 사용하여 전역 파일에 있습니다.

아래는 내 ~/.gitconfig파일 의 별칭 섹션 사본입니다 .

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

또한 bash를 사용하는 경우 git-completion.bash홈 디렉토리 에 복사 하고 .NET에서 소싱하여 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

참고 : bash 완성은 표준 git 명령뿐만 아니라 git 별칭에서도 작동합니다.

마지막으로 키 입력을 줄이기 위해 다음을 내 ~/.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'

가장 유용한 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

git config alias명령 이 필요합니다 . Git 저장소에서 다음을 실행합니다.

git config alias.ci commit

글로벌 별칭의 경우 :

git config --global alias.ci commit

이것은 나를 위해 일했습니다.

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

의 위에:

$ git --version

git version 1.7.7.5 (Apple Git-26)

This will create an alias st for status:

git config --add alias.st status


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

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

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

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:

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

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

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

And then run them:

$ git hi
hello
$ git st
On branch master

...

$ 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.

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

$ 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>

Add the following lines to your ~/.gitconfig in your home directory

[alias]
# one-line log
l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
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

a = add
ap = add -p
c = commit --verbose
ca = commit -a --verbose
cm = commit -m
cam = commit -a -m
m = commit --amend --verbose

d = diff
ds = diff --stat
dc = diff --cached

s = status -s
co = checkout
cob = checkout -b
# list branches sorted by last modified
b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

# list aliases
la = "!git config -l | grep alias | cut -c 7-"

Once that is done, you can do git a instead of git add for example. The same applies to other commands under the alias heading..


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:

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.


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

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

I created alias dog for showing the log graph:

git config --global alias.dog "log --oneline --graph --all --decorate"

And use it as follow:

 git dog

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


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.

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.


PFA screenshot of my .gitconfig file

with the below aliases

[alias]
    cb = checkout branch
    pullb = pull main branch

For me (I'm using mac with terminal) only worked when I added on .bash_profile and opened another tab to load the change:

alias gst="git status"
alias gd="git diff"
alias gl="git log"
alias gco="git commit"
alias gck="git checkout"
alias gl="git pull"
alias gpom="git pull origin master"
alias gp="git push"
alias gb="git branch"

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.

For example (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.


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.

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.

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

참고URL : https://stackoverflow.com/questions/2553786/how-do-i-alias-commands-in-git

반응형