简体   繁体   中英

Alias Multiple Commands in Linux

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?

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM