Anthony Graule
Anthony Graule

Reputation: 33

Is it possible to create an alias commit+checkout for git?

I want to create an alias for git to realize a commit before a checkout in one command. Is it possible?

T tried this but failed:

git config alias.changebranch 'commit -a --allow-empty-message -m "" checkout'

Response for this command:

$ git changebranch hello
fatal : paths with -a does not make sense

Upvotes: 0

Views: 53

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

That isn't an alias that performs two commands, it's just an alias that performs one command, with "checkout" as the last argument to that command.

You want something like:

git config alias.changebranch '!git commit -a --allow-empty-message -m ""  && git checkout' 

The ! means the alias is run by the shell, not just treated as a git command, and the checkout will only run if the commit succeeds.

Upvotes: 3

FrankTheTank_12345
FrankTheTank_12345

Reputation: 580

Basically you just need to add lines to ~/.gitconfig

[alias]
    st = status
    ci = commit -v

Or you can use the git config alias command:

$ git config --global alias.st status 

On unix, use single quotes if the alias has a space:

$ git config --global alias.ci 'commit -v'

On windows, use double quotes if the alias has a space or a command line argument:

c:\dev> git config --global alias.ci "commit -v"

The alias command even accepts functions as parameters. Take a look at aliases -> https://git.wiki.kernel.org/index.php/Aliases#Aliases

Upvotes: -1

Related Questions