49

I am working with the latest Git bash for Windows, on my laptop running Windows 7. When I define my aliases like:

$ alias gitc='git commit -a'

Everything works well during the session, but I cannot recover them if I close and open the bash. The command history is preserved, though.

What should I do? What I have missed?

Thanks!

Antoine Lizée
  • 3,743
  • 1
  • 26
  • 36
  • Where do you define the alias? – Anon Dec 05 '12 at 02:03
  • @Anon In the Git bash terminal – Antoine Lizée Dec 05 '12 at 02:45
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jul 16 '18 at 12:27

8 Answers8

76

When you open the git bash type in the command touch .bash_profile. Following this type vim .bash_profile. You can then add your aliases to this file. Save the file and reopen the git bash and your aliases should work as expected.

This method allows you to create aliases for any bash command available in git bash however as others have answered it is also possible to create git specific aliases using git itself.

ctor
  • 5,938
  • 2
  • 26
  • 37
  • @AntoineLizée, to reload the file. Other way is to re-login. – Anon Dec 05 '12 at 03:23
  • 9
    Wot? You shouldn't need the "touch" command. It creates the file -- but `vim .bash_profile` will do the same thing. To reload the file in an already open bash shell you need to do `. .bash_profile` (yes, that's another '.' in front there, with a space) or `source .bash_profile`. – ebneter Dec 05 '12 at 06:43
  • 2
    If the touch is not necessary, I apologise. I never tried to use `vim` to create the file but manually creating the file in Windows Explorer definitely does not work properly. But as ebneter says, it does nothing but create the file. – ctor Dec 05 '12 at 07:47
  • 2
    Instead of restarting the git bash shell, you can use `source .bash_profile` or `. .bash_profile` as [described here](http://stackoverflow.com/a/255416/1174169). You know, in case you had a whole lot of $^*% going on at the time, or are like me and find reopening a window soveryinconvenient. – cod3monk3y Jul 23 '14 at 13:48
  • The answer assumes the cwd of the terminal is /c/users/, but this isn't *necessarily* the case (say when using Git bash from GitExt via Ctrl+G) so the missing part of the answer (IMO) is simply touching .bash_profile under /c/users/ – Shmil The Cat Dec 01 '14 at 21:44
  • I had to exit the git bash and restart it to make the changes work – Mister Verleg Mar 07 '17 at 14:30
21

Instead of modifying your bash_profile you can setup a .gitconfig and add aliases like this:

[alias]
  st = status
  ci = commit
  br = branch
  co = checkout
  df = diff
  lg = log -p
AJ.
  • 1,248
  • 10
  • 27
  • +1, I didn't know of this. I've edited my answer mentioning you can do it through git as others have said :) – ctor Dec 05 '12 at 07:50
  • 1
    Thanks AJ, I saw that on the Git help, and it would definitely work. Nevertheles I wanted do aliases that would not require the 'git' before, for instance gb='git branch -v', or aliases that would cope with non-git functions. – Antoine Lizée Dec 05 '12 at 09:38
11

Create the .bashrc file in the home directory:

touch ~/.bashrc
vim ~/.bashrc

In the file ~/.bashrc add the aliases:

alias gitc='git commit -a'
# -- ... and your other aliases here ...

Save the file (press <ESC>:wq in vim). Reload the file such that bash is aware of the changes made:

source ~/.bashrc

These steps work for me in Win 7/Win 8 with Git bash (MINGW32)

Richard210363
  • 8,342
  • 6
  • 37
  • 63
Sam Su
  • 6,532
  • 8
  • 39
  • 80
5

For Windows Users :
Be sure you're in the home directory , the Simplest way is creating a .bash_profile File and insert your aliases within

note : to edit it with Notepad execute this line first :

git config core.editor notepad

then create the file and add your alias as following :

notepad .bash_profile

now you can add your aliases to the .bash_profile like :

alias yourAlias='your Command Here'
alias AnotherAlias='your Command Here'


save the file by pressing ctrl +s or File>save Menu

Marvin Mustafa
  • 163
  • 1
  • 8
1

You need to put them in your .bash_profile. Then they'll get reset every time a new login shell starts up.

khagler
  • 3,996
  • 29
  • 40
  • It works, thanks a lot. I can't upvote yet (lol) but I chose the other answer which is slightly more accurate and easy to follow for beginners. – Antoine Lizée Dec 05 '12 at 02:52
1

If you have git from here: https://git-scm.com/download/win Go to the installed folder. For example: "c:\Program Files\Git"

And find the aliases.sh file what is usually located here: C:\installation\folder\etc\profile.d\

Now you can add aliases to the existing ones like this:

# Some good standards, which are not used if the user
# creates his/her own .bashrc/.bash_profile

# --show-control-chars: help showing Korean or accented characters
alias ls='ls -F --color=auto --show-control-chars'
alias ll='ls -l'
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'

Restart your bash terminal and now you can use your new aliases in every git project.

Sirsemy
  • 117
  • 1
  • 13
0

I know you've already gotten an answer, but you might want to consider using git's own alias system, which is explained in the git config help page. Then they can be per-repo as well as system-wide or per-user.

siride
  • 200,666
  • 4
  • 41
  • 62
0

I'm very new to powershell. I've been borrowing a laptop from my company for less than a week, and that encompasses my entire experience with windows. However, I may have found a solution that works (at least for me).

(inspired by https://superuser.com/questions/1090141/does-powershell-have-any-sort-of-bashrc-equivalent)

In PowerShell:

New-Item $profile -Type File -Force

It creates a file called Microsoft.PowerShell_profile.ps1 in a folder called WindowsPowerShell under your Documents folder. Then you can open it with a text editor:

notepad $profile

Add $bash = [bash | . ~/.bash_profile] to that file.

Now when entering bash from powershell, your bash_profile should be sourced automatically. I can type env, for example, and all of the local variables specified in my bash_profile are present. I imagine that this would also work with .bashrc, but I haven't tried that yet.

For the record, this is just an extension of Marvin Mustafa's answer. But I'm not sure that his method would fix the problem I was having, which was that I had to resource my bash_profile every time I started a new command prompt in windows.

Hope this is useful.

**** EDIT ****

This solution worked for me but I got complaints from windows from doing certain actions. I think the better solution, although potentially problematic as well, is to edit /etc/bash.bashrc and add source ~/.bash_profile. After doing this my PS1, profile variables, etc. were shown in env. Good Luck.

Christian Meyer
  • 605
  • 8
  • 15