3

In this question Git command to commit all changes including files removed or created the answer given allows me to add, commit and push all changes made to my master branch in one command with:

git commitall "a message describing what you did"

where commitall is the user defined command:

commitall = "!func(){ git add -A && git commit -am \"$1\" && git push origin master; }; func"

stored in the file ~\.gitconfig under the section [alias].

The problem is that this command only works when I'm positioned in the master branch. How could I generalize this command so it will check in which branch I'm currently positioned and push the changes to that branch?

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

3

If you have done (see "git - push current vs. push upstream (tracking)"):

git config push.default simple
# or at least
git config push.default current

Then your git push origin (no branch specified) will always push only the current branch.

If not, replace the git push origin master in your alias with:

git push -u origin \"$(git rev-parse --abbrev-ref HEAD)\"

More at "Git alias on current branch".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have not done `git config push.default simple`, I don't even know what that is. Could you expand a bit please? – Gabriel Dec 06 '13 at 12:40
  • `git config push.default current`? – Alexander Yancharuk Dec 06 '13 at 12:41
  • I was also wondering what the difference between simple and current was. – crea1 Dec 06 '13 at 12:42
  • @AlexanderYancharuk was that a question to me or VonC? If it was directed at me, I have not done `git config push.default current` and I also don't know what it means. Sorry, brand new to `git` here. _Edit_: oh I see you were asking VonC. – Gabriel Dec 06 '13 at 12:42
  • @VonC Thanks, Im still on 1.7.10 so even didn't knew about `simple` parameter. – Alexander Yancharuk Dec 06 '13 at 12:47
  • @AlexanderYancharuk yes, `current` can work too. I have edited my answer. – VonC Dec 06 '13 at 12:47
  • @VonC so if I do a `git config push.default current` (I'm using `v1.7.9.5` so no `simple` command) it will tell `git` to always push to the current branch I'm in when I run `git push origin`, thus I don't need to specify the branch anymore. Did I understand that correctly? – Gabriel Dec 06 '13 at 12:52
  • @AlexanderYancharuk 1.7.10? April 2012? That seems so old ;) There are much more recent packages out there: http://superuser.com/q/644586/141 – VonC Dec 06 '13 at 12:55
  • @VonC Thanks! Got 1.8.4 from backports. It has `simple` param. +1 from me when I run out of votelimit :) – Alexander Yancharuk Dec 06 '13 at 13:24