118

I'm using Git Bash on Windows 7 and would like to set up Bash profile aliases such as alias gs='git status' to make my life easier. How can I do this?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Brian
  • 1,401
  • 3
  • 13
  • 17

6 Answers6

151

When you open up your Git Bash, you should be in your home directory by default. Now create the .bashrc file (if on Windows 7 the file should be named .bashrc.).

If you're not in the home directory, change into it by typing:

cd

and pressing Enter. cd, without any other parameters listed after, will always return the home directory.

You can create the file by typing:

touch .bashrc

Then edit it with Vim or you could try doing it with some Windows editor, but I don't recommend it, because of some text formatting issues.

vim .bashrc

Change to Insert Mode by hitting the i key.

Add your alias by typing:

alias gs='git status'

Exit the insert mode by hitting the Esc key.

Save and close your file by typing the following :wqEnter.

:wEnter will only save your file.

:q!Enter will quit the editor without saving your file.

Finally, update the file to use your new changes by typing:

source .bashrc

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
wottis
  • 1,686
  • 1
  • 11
  • 7
  • Thanks for your answer. I didn't know about .bashrc before, so your answer lead me to several sources, including http://superuser.com/questions/183870/difference-between-bashrc-and-bash-profile, that agree that .bashrc is a better place to put aliases than .bash_profile. – Brian Jun 03 '13 at 15:07
  • This really helped! I love that you also supplied the additional commands to use VIM. It's not really the most friendliest editor. Much appreciated! – cbloss793 Aug 04 '16 at 21:14
  • 15
    This gives me `WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile. This looks like an incorrect setup. A ~/.bash_profile that loads ~/.bashrc will be created for you.` (Git-2.11.0-64-bit) – aliopi Jan 19 '17 at 07:53
  • How can I add a path in that file I tried: ´alias app='cd c:\mypath'´ – utdev Mar 30 '17 at 07:53
  • FYI, no need to `touch` the file, `vi` will create it for you directly. Prevents you one line ;) – Olivier Feb 28 '18 at 13:19
  • 1
    only the first time you open the git bash @aliopi. It's doing what it says, creating a .bash_profile file for you. – Pablo Recalde Jan 14 '22 at 09:19
39

You can put .bash_profile in your user directory: C:\Users\<username>.

You can also create some git-only aliases so you can do just git st for git status by adding these lines to C:\Users\<username>\.gitconfig:

[alias]
st = status

Some other useful aliases:

cm = commit -m
cma = commit -a -m
br = branch
co = checkout
df = diff
ls = ls-files
sh = stash
sha = stash apply
shp = stash pop
shl = stash list
mg = merge
ph = push -u
gronostaj
  • 55,965
  • 20
  • 120
  • 179
  • 3
    If aliases for Git commands are needed, editing the `.gitconfig` file is usually sufficient. – Isxek Jun 02 '13 at 21:45
  • Thanks for your answer! I wasn't expecting to get two different but correct answers. – Brian Jun 03 '13 at 15:11
  • 2
    For me, `.bash_profile` *wasn't* in my windows home dir. but my roaming home dir (network admin imposed). The "correct" answer is "put `.bash_profile` in your *git bash* home dir", which you can find by going to `cd ~` then `pwd` – Bohemian Jan 18 '18 at 19:44
5

My git version is git version 2.18.0.windows.1 It took me a while to figure out where the .bashrc was C:\Program Files\Git\etc ---> bash.bashrc hope it helps

SalFie
  • 51
  • 1
  • 1
2

Simply (if you have .bashrc you will add aliases to the end of file):

cat >> ~/.bashrc

Paste or type a list of aliases. Press Ctrl + D and finally run:

source ~/.bashrc
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
1

In your Git Bash home directory, there should be a .gitconfig file. In this file you can add your aliases by adding [alias]. It should be something like below:

[alias]
st = status
co = checkout
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
  • 2
    Welcome to Super User! This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill Jul 05 '16 at 09:32
1

If you can't find your ~/.bashrc file, you can add all aliases to your ~/.bash_profile file.

For instance, to add an alias for a Git command (git status) simply add:

alias gs="git status"

In the same way you can add an alias for a Bash command (change directory path):

alias myd="cd ~/path to my directory"
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Otti
  • 111
  • 4