Code shortcut for multiple git commands

Asked

Viewed 230 times

6

How do I have only one command run another automatically on git?

Example: When sending git atalho perform the following instructions:

git status
git add Post.txt
git commit -m "aqui coloco uma mensagem"

There is how to do this without the need to write each line separately?

  • Linux or Windows environment?

  • Luiz, if the answer solved your problem please mark the answer as accepted, if you still have any questions ask so that we can clarify. Hug!

3 answers

5


Linux:

Create a alias in the terminal:

$ alias gitcmd = "git status; git add .; git commit -a -m 'mensagem'; git push;";

Then just call the alias created by the name:

$ gitcmd

To leave the permanent command you must include it in the files .bashrc or .bash_profile


Windows:

Create a new file in the text editor and save it as nova-branch.cmd

This will be a batch file that can contain the following commands:

git branch %1 origin/master
git checkout %1

Save him as nova-branch.cmd in a folder of your choice:

C:\Scripts\nova-branch.cmd

Test the batch file by running on the terminal cmd windows:

C:\Scripts\nova-branch.cmd meuprojeto

Branch meuprojetoset up to track remote branch master from origin by rebasing.

Switched to branch 'meuproject' Your branch is up-to-date with 'origin/master'.

If you want to fix in environment variables do:

SETX PATH "%PATH%;C:\Scripts"

Then just call the creation aquivo passing the name of the branch as a parameter:

nova-branch.cmd projetoX

Reference:

5

As placed by another answer, you can use the command alias. However, this approach only allows you to create them in environments that have this command. Windows, for example, would be out.

However, Git also gives you the option to set aliases using its own interface. This means that you still need to use the command git to access them.

For example, to create a alias git ac, that rotates the commands git add . and git commit, you can do:

#           Criamos um alias com nome "ac"
#                         ↓↓
git config --global alias.ac '!git add . && git commit'

It is worth noting that as we did not pass any message to the commit, it will open an editor so that you can insert a message. In addition, the exclamation mark was used in the beginning to make it possible to execute external commands, since by default you can only create aliases for an existing subcommand. For example:

git config --global alias.l 'log --oneline --abbrev'

Note that we don’t even need to have put the git in front of the log.


To learn more, read the documentation. There is also a excellent Gist on the subject, in Portuguese.

2

This answer is not complete because the status

but giving a:

git commit -am "sua mensagem"

You’ll be doing it in summary form

git add .
git commit -m

Which would solve 66.66% of the problem :D

Somehow I don’t see much sense in you having a command of status automatic along with a commit, because even if you see something wrong in status By then it’ll be too late. I’d keep the git status always individual for safety reasons!

But if you want to do it

git add .
git commit -m "sua mensagem"
git push

With a line this will work

git commit -am "sua mensagem" && git push

Even you can concatenate the git status also with that &&, then it would be 100% the way you want it....

git status && git commit -am "sua mensagem

(but be careful not to put anything unwanted in stage)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.