I am managing a website using git. One of the requirements for the git repository is that bare = true. It uses a post-receive hook to manage pushes from my local computer. The problem is that sometimes I would like to make changes to a WordPress directory on my website using the wp-admin view online. So then I would just ssh into the directory and run git --work-tree="BLAH" add . and git --work-tree="BLAH" commit -m "BLAH". Is there a way to set up an alias, like alias git="git --work-tree=\"BLAH\"" and have that work for all git commands?
- 1,800
- 17
- 30
-
1aliases are only suitable for static values and actions, i.e. `alias workDir='cd $workDir'` . When you want to process arguments you need to use functions. Search here for `[bash] function` and you'll find 100's of examples. Good luck. – shellter Oct 07 '16 at 03:11
-
1To be honest the best way not to screw up deployment is to automatise it, Takes a look at Capistrano for example. – Antzi Oct 07 '16 at 03:16
3 Answers
There are times when alias are a great tool. Then there are times when things start getting too complicated where a shell script is better.
To create a single command that executes other commands just create a file (maybe call it git-add-all) then type the following:
#! /bin/bash
git --work-tree="BLAH" add .
git --work-tree="BLAH" commit -m "BLAH"
Then you can run the script by simply doing:
bash git-add-all
Even better, make the script executable:
chmod +x git-add-all
Then you can use it like any command:
./git-add-all
Advanced tips:
To be able to run the script from any git directory you can copy/move the file to one of the directories in your $PATH. For example /usr/loca/bin. Then you can simply run git-add-all instead of ./git-add-all.
Even better is to create your own personal scripts directory and include it in $PATH. I personally use ~/bin. To add the directory to $PATH you just need to add the following to .bashrc or .profile:
export PATH=/home/username/bin:$PATH
or if you're doing this for the root user:
export PATH=/root/bin:$PATH
- 109,858
- 19
- 140
- 171
In case anyone is curious how I solved it (thanks to shellter's comment), I wrote a bash script then prompted the user for input like so:
#!/bin/bash
function fix {
git --work-tree="PATH_TO_WORKING_TREE" $1
}
echo -n "git "
read -e INPUT
until [ "$INPUT" = "quit" ]; do
fix $INPUT
echo -n "git "
read -e INPUT
done
Running it:
user@server [repo.git] $ git-fix
git status
# On branch master
nothing to commit (working directory clean)
git quit
- 1
- 1
- 1,800
- 17
- 30
There is a .bashrc file in Linux. You can edit it for creating alias for your favorite and frequently used commands.
To create an alias permanently add the alias to your .bashrc file
gedit ~/.bashrc
The alias should look like:
alias al='cmd'
You can read more about it over here.
-
I know about .bashrc and already tried that. When I run `git` it expands it, but if the command has two parts (like `git status`) it won't expand it. – theEpsilon Oct 07 '16 at 03:09
-
try naming your alias other than `git`. For example, ll is an alias of `ls -alF`. It still works if you type `ll -h` or `ll -a`. – abhiarora Oct 07 '16 at 03:45