0

Is it possible to run the following commands all at once:

1. git add .
2. git commit -m "Message"
3. git push orgin/master

I know the the first two can sort of be joined like this:

git commit -am "Message"

I want to push to remote after committing locally without having to go through all these steps.

sethathi
  • 23
  • 6

2 Answers2

5

IMHO This will be possible, only if

  • You are the only programmer contributing
  • You are using only one local repository to push changes to remote

And this takes away the flexibility and power git gives you.

If so,

you can use 'alias'

So something like this can do the job.

$ gitFunction() {
> git add .
> git commit -m "$1"
> git push origin }
$ alias git-all="gitFunction" 
$ git-all 'commit message' //Will do everything for you.

Or

Add this to your .git/config file.

 [alias]
        sync = "!sync() { git add . && git commit -m \"$1\" && git push $2; }; sync"

Later you can use

 $git sync 'commit message' remote_repo

But you shouldn't be doing this!!

Why?

Though off the topic, I would love to quote Principle of sufficient reason

Nothing is without a reason or cause

Git, when designed by Linus had these steps separate because of sufficient reason. If you are abusing it then you won't get the full potential out of it. So its good to have best practices followed.

From what you intend to achieve, you view git as, just a tool to upload your source-code changes into a remote server, which is by the way wrong and takes away all the goodness git gives you.

Why git add shouldn't be automated?

git add helps you to add untracked files/ add modified files into staging area. Which will later go into your commit. Doing git add . will add everything into your staging area. Most times you will have something, that you don't want to commit or to be there in a specific commit.

Why git commit -m "Message" shouldn't be automated?

A commit message explains the changes that happened after the last commit. A commit message helps other programmers collaborating with you to understand the changes you have done. So you shouldn't be writing senselessness in a commit message

Why git push shouldn't be automated?

Even with your present logic, this step should fail if you are not the only programmer contributing to the project. So there will be changes in remote, which you should be pulling. And in best case an automatic merge will happen and you can directly do a git push as next step. But in case of a conflict, you need to resolve the conflicts, then commit the changes, merge and then push (which by the way, is not easy to automate if possible).

Also you won't be pushing into same remote and you won't be pushing the same branch all the time. All these things are variable and needs the developers decision.

So, try understanding Git and then give a second thought on these.

Read Pro Git it is free and best book on git.

egghese
  • 2,193
  • 16
  • 26
  • 1
    You've got the right idea, but you're not setting the alias correctly, and you're not executing it correctly either. Also, it is possible to paramterize the alias to accept a commit message, and you should add that. –  Jul 17 '13 at 19:50
  • @Cupcake: Thanks. I first thought there is only one static commit message. So was more focused on telling what he/she intends for is not a good practice. Updated it now. :) – egghese Jul 17 '13 at 20:17
  • I actually think that it would be quite useful to make an alias that does what the original poster asked for. Maybe modify it to only push the current branch to his remote though, instead of the `master` branch. If he's working on GitHub, it's likely that his `origin` repo is just a fork, in which case it acts as a "private" repo. –  Jul 18 '13 at 01:44
  • Yup. Maybe! It is just my perspective. I am somebody who doesn't quite appreciate even GUI for git. And I just feel combining these three steps just take away the whole taste of git. It might be useful to have git add and commit go at once (git -am already does that). But along with that push happens atomically! something I really can't have my opinion to be with the question. – egghese Jul 18 '13 at 02:48
1

You could define an alias, which takes an argument like it is described in details here

in your case I believe this should work

[alias]
    acp = "!f() { git add . && git commit -m \"$1\" && git push origin/master; }; f"

then you can call it

 git acp "commit message"

Additionally, check out what kind of tricks you can do with an alias in the Git documentation, it's quite useful tool.

Community
  • 1
  • 1
lpiepiora
  • 13,659
  • 1
  • 35
  • 47