225

How to alias commands in Git Bash for Windows downloaded from git-scm?

I mean Bash commands, not Git.

I’m on Windows 7.


Edit:

Writing aliases in .bashrc file (as suggested by @gturri) not adding it in console.(after system reboot)(I have never wrote alias for ls command so it should be some default alias.)

~/etc $ cat .bashrc
alias ll='ls -l'
alias dupa='ls -l'
~/etc $ dupa
bash: dupa: command not found
~/etc $ ll
total 0
~/etc $ alias
alias ll='ls -l'
alias ls='ls -F --color=auto --show-control-chars'
Guildenstern
  • 2,179
  • 1
  • 17
  • 39
  • 2
    `ls` is not a default alias, it's a command. `ls.exe` is in `/usr/bin` which is to say `C:\Program Files\Git\usr\bin\ls.exe`. You can run it from a `cmd` or `PowerShell` commandline. – Chris F Carroll Oct 21 '17 at 14:46

13 Answers13

227

To configure bash aliases, it's the same as if you were on a Unix platform: put them in a .bashrc in your home:

cd
echo alias ll=\'ls -l\' >> .bashrc

To have this change taken into account you should then either source this file (ie: run source .bashrc) or restart your terminal

(In some cases* you can find equivalent for .bashrc file in C:\Users\<username>\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh. And you should add aliases in aliases.sh.)

(*this case is when you install Git for Windows GUI release from https://git-scm.com/download/win that contains GitBash)

gturri
  • 13,807
  • 9
  • 40
  • 57
  • 3
    yesterday I accepted this answer as it seemed working. I wrote your example line `alias ll=\'ls -l\'` and `ll` command worked. Then today added more aliases and it's not. (so I undo accept the answer). update in my main post. Looks like aliases `ll` and `ls` are default (I have never wrote alias for `ls` command - I think so) – Sruj_2ndAccountForStupidQtions May 09 '16 at 08:44
  • weird: I tested in on a Windows 7 and it works fine (I tested with `echo alias toto=\'ls -l\' >> .bashrc` to avoid interactions with default aliases). Can you check that the line is indeed in your `.bashrc`? (also, remember that it should be tested with a new window, since the `.bashrc` is sourced when the shell is launched – gturri May 09 '16 at 09:54
  • Does Your etc directory existed by default or did you created it manually? I dont remember but it seems i created this directory manually yesterday (in my windows user account) as in other windows (admin) account, where I also have git bash, etc directory not exist. (Again, I downloaded and installed git bash from official git site.) – Sruj_2ndAccountForStupidQtions May 09 '16 at 11:21
  • I never paid attention to this `/etc` directory. It seems to actually match `C:\Users\\AppData\Local\Programs\Git\etc`. I didn't created it, so it must have been created automatically – gturri May 09 '16 at 11:35
  • oh, I found a file C:\Users\\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh. And this is my equivalent for .bashrc file – Sruj_2ndAccountForStupidQtions May 09 '16 at 11:45
  • 9
    As of Git Bash 2.13 / Windows 10 the directory was located at `C:\Users\csmith\AppData\Local\Programs\Git\etc\` – QueueHammer May 18 '17 at 12:51
  • In my case, there are exactly some alias script files under `C:\Program Files\Git\etc\profile.d` provided originally when git bash installed, including common alias settings like `ll` and `winpty` wrapping. – 千木郷 Sep 22 '18 at 16:03
  • Just mentionning : for some reason some git-bash installations starts consoles by default in the root directory / rather than the home directory. Thus if you don't see the .bashrc file just do a 'cd' to jump to your home directory. – Wasabi Oct 30 '20 at 13:50
  • Don't forget to run `source ~/.bashrc` – LetsGoBrandon May 20 '22 at 08:03
  • Use `echo \alias ll=\'ls -l\' >> .bashrc` to put a new line before the alias. Therefore it makes sure that you do not litter another line by accident. – Péter Szilvási Jul 05 '23 at 13:29
118

I had the same problem, I can't figured out how to find the aliases used by Git Bash on Windows. After searching for a while, I found the aliases.sh file under C:\Program Files\Git\etc\profile.d\aliases.sh.

This is the path under windows 7, maybe can be different in other installation.

Just open it with your preferred editor in admin mode. After save it, reload your command prompt.

I hope this can help!

Maurizio
  • 1,189
  • 1
  • 7
  • 6
  • 1
    I have 2 computers running Windows 10. In one, the first solution worked, in the other, only your solution worked! Thanks, @Maurizio! – matthiku Sep 12 '17 at 10:26
  • 16
    This `aliases.sh` file is overwritten when you install a new git version. I recommend to rather create a `.bashrc` in your "home" directory (I mean `C:\users\`) – tobi42 Nov 30 '17 at 08:52
  • 1
    I use `echo "source ~/.bashrc" >> /etc/profile.d/aliases.sh` - then I keep my aliases in my home folder and after a git update I just restore the pointer in etc to use my aliases also – rafalmag Aug 01 '18 at 09:11
  • 1
    Be careful to secure this file somewhere. I lost mine due to an update of Git. – fose Oct 23 '20 at 13:13
  • It technically works. But it requires that you have writing permission there, and as pointed out by others it will be undone if you upgrade git. – Kim Oct 24 '22 at 07:35
98

Follow below steps:

  1. Open the file .bashrc which is found in location C:\Users\USERNAME\.bashrc

    If file .bashrc not exist then create it using below steps:

    1. Open Command Prompt and goto C:\Users\USERNAME\.
    2. Type command notepad ~/.bashrc
      It generates the .bashrc file.
  2. Add below sample commands of WP CLI, Git, Grunt & PHPCS etc.


# ----------------------
# Git Command Aliases
# ----------------------
alias ga='git add'
alias gaa='git add .'
alias gaaa='git add --all'

# ----------------------
# WP CLI
# ----------------------
alias wpthl='wp theme list'
alias wppll='wp plugin list'

Now you can use the commands:

  • ga instead of git add .
  • wpthl instead of wp theme list

Eg. I have used wpthl for the WP CLI command wp theme list.

Yum@M MINGW64 /c/xampp/htdocs/dev.test
$ wpthl
+------------------------+----------+-----------+----------+
| name                   | status   | update    | version  |
+------------------------+----------+-----------+----------+
| twentyeleven           | inactive | none      | 2.8      |
| twentyfifteen          | inactive | none      | 2.0      |
| twentyfourteen         | inactive | none      | 2.2      |
| twentyseventeen        | inactive | available | 1.6      |
| twentysixteen          | inactive | none      | 1.5      |
| twentyten              | inactive | none      | 2.5      |
| twentythirteen         | inactive | none      | 2.4      |
| twentytwelve           | inactive | none      | 2.5      |

For more details read the article Keyboard shortcut/aliases for the WP CLI, Git, Grunt & PHPCS commands for windows

Ali Ben Messaoud
  • 11,690
  • 8
  • 54
  • 87
maheshwaghmare
  • 2,082
  • 19
  • 23
24

You can add it manually in the .gitconfig file

[alias]
    cm = "commit -m"

Or using the script:

git config --global alias.cm "commit -m"

Here is a screenshot of the .gitconfig

enter image description here

StudioTime
  • 22,603
  • 38
  • 120
  • 207
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
11

To Add a Temporary Alias:

  1. Goto Terminal (I'm using git bash for windows).
  2. Type $ alias gpuom='git push origin master'
  3. To See a List of All the aliases type $ alias hit Enter.

To Add a Permanent Alias:

  1. Goto Terminal (I'm using git bash for windows).
  2. Type $ vim ~/.bashrc and hit Enter (I'm guessing you are familiar with vim).
  3. Add your new aliases (For reference look at the snippet below).
    #My custom aliases  
    alias gpuom='git push origin master' 
    alias gplom='git pull origin master'
    
  4. Save and Exit (Press Esc then type :wq).
  5. To See a List of All the aliases type $ alias hit Enter.
shubhamr238
  • 1,226
  • 14
  • 22
10
  1. Go to C:\Program Files\Git\etc\profile.d

  2. Open as aliases.sh with your text editor as Administrator.

  3. Add your new aliases and save

  4. Restart git bash

Source: https://dev.to/mhjaafar/git-bash-on-windows-adding-a-permanent-alias-198g

tripleee
  • 175,061
  • 34
  • 275
  • 318
Thinh Le
  • 603
  • 1
  • 7
  • 14
  • 1
    Apparently found here: https://dev.to/mhjaafar/git-bash-on-windows-adding-a-permanent-alias-198g – albert Aug 03 '21 at 10:28
  • 1
    This is good answer; actually if you use `alias` and you see all aliases are defined in this script. One observation isthat Gitbash has its `bash.bashrc` and `bash.bash_logout` under `/etc`, and adding things into `bash.bashrc` works, but creating a `bash.bash_aliases` does not work. I have to resort to the aliases scripts. – WesternGun Nov 20 '22 at 12:26
8

There is two easy way to set the alias.

  1. Using Bash
  2. Updating .gitconfig file

Using Bash

Open bash terminal and type git command. For instance:

$ git config --global alias.a add
$ git config --global alias.aa 'add .'
$ git config --global alias.cm 'commit -m'
$ git config --global alias.s status
---
---

It will eventually add those aliases on .gitconfig file.

Updating .gitconfig file

Open .gitconfig file located at 'C:\Users\username\.gitconfig' in Windows environment. Then add following lines:

[alias]  
a = add  
aa = add . 
cm = commit -m 
gau = add --update 
au = add --update
b = branch
---
---
npcoder
  • 414
  • 1
  • 5
  • 13
  • 1
    The second one is the best answer. It does not require to generate a new `.bashrc` file under the home, that under windows seems at least a bit strange, and uses a standard `.gitconfig` instead. Thank you! – Andrea Mar 16 '20 at 11:04
  • for me it wasn't in c:/users/myusername/... which is not my home. However, it was in `$HOME/.gitconfig` – Juh_ Sep 15 '20 at 13:26
4

Using Windows and MINGW64 GitBash (mintty 3.2.0), I found the file under:

%LocalAppData%\Programs\Git\etc\profile.d\aliases.sh

Just added the alias there and it worked for me.

kelvin
  • 1,421
  • 13
  • 28
tturbo
  • 680
  • 5
  • 16
2
  • Go to: C:\Users\ [youruserdirectory] \bash_profile

  • In your bash_profile file type - alias desk='cd " [DIRECTORY LOCATION] "'

  • Refresh your User directory where the bash_profile file exists then reopen your CMD or Git Bash window

Type in desk to see if you get to the Desktop location or the location you want in the "DIRECTORY LOCATION" area above

Note: [ desk ] can be what ever name that you choose and should get you to the location you want to get to when typed in the CMD window.

1

For people who don't have admin privileges to make the changes mentioned in other solutions can do this easy trick.

  1. Create a file .bash_profile with all the aliases under the folder C:\Users\<user-profile>
  2. Now restart your git bash and your shortcuts should work.

Note: I am using windows 10

Andy
  • 5,433
  • 6
  • 31
  • 38
0

I would prefer a zero-setup solution, but i found none yet. Once i Pull a repository, how can automagically have some standard alias available, so Bash shell for Git Extension can recognize it ? It seem that the C:\User<youruser>.bashrd is recognized, and also C:\Program Files\Git\etc\profile.d\aliases.sh, but these locations are outside the pulled project, and need to be manually configured.

Do exist some way to "autoconfigure" aliases for bash shell (for windows git extensions) just dropping a file in the project root, and not somewhere into user settings ?

0
  1. Go to C:\Program Files\Git\etc
  2. Edit the bash file with the RC extension on your notepad
  3. At the end of the file add this command for example: alias ab = 'cd'
  4. Restart Git Bash
  • 1
    It's not usually a good idea to modify files that ship with a program. Editing a user-centric config file is safer and less likely to break, e.g. when you update Git and it overwrites your changes. – ChrisGPT was on strike May 01 '23 at 18:37
-1

These commands work for WSL for Ubuntu:

  1. Open .bashrc file: vim ~/.bashrc
  2. Add the following commands:
    alias gt="git status"
    alias ll='"ls -la"
    alias gp="git pull"
  3. Save and exit the file: :wq
  4. And then source the file: source ~/.bashrc
RiveN
  • 2,595
  • 11
  • 13
  • 26