Reputation: 648
I made a shell alias that I intend to use in conjunction with other commands:
this = "!f() { git rev-parse --abbrev-ref HEAD; }; f"
The idea is that it will output the current git branch I'm at, and use it like so:
git push -u origin this
When I just run the alias it outputs the current branch correctly, but when I try to use it like the example above it gives me an error:
error: src refspec this does not match any.
error: failed to push some refs to 'myrepo.git'
If I try the same command but actually writing the branch name (git push -u origin <branch>
) it works.
What am I doing wrong? Can't it unroll my this
alias when I use it in conjunction with another command?
Upvotes: 2
Views: 407
Reputation: 81052
git doesn't expand aliases at random points in the command line (that would require a lot of scanning and work at all times and potentially break any usage of an aliased word in a branch ref/etc.)
The closest you can get to what you want is git push -u origin "$(git this)"
.
That all being said unless you know you have a reason to specify the remote and branch name manually you probably don't need to.
The man page for git push says:
When the command line does not specify where to push with the argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin.
When the command line does not specify what to push with ... arguments or --all, --mirror, --tags options, the command finds the default by consulting remote.*.push configuration, and if it is not found, honors push.default configuration to decide what to push.
The man page for git config says:
remote..push
The default set of "refspec" for git-push[1]. See git-push[1].
and
push.default
Defines the action git push should take if no refspec is explicitly given. Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want. Possible values are:
nothing - do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.
current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.
upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).
simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.
When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners.
This mode has become the default in Git 2.0.
- matching - push all branches having the same name on both ends. This makes the repository you are pushing to remember the set of branches that will be pushed out (e.g. if you always push maint and master there and no other branches, the repository you push to will have these two branches, and your local maint and master will be pushed there).
To use this mode effectively, you have to make sure all the branches you would push out are ready to be pushed out before running git push, as the whole point of this mode is to allow you to push all of the branches in one go. If you usually finish work on only one branch and push out the result, while other branches are unfinished, this mode is not for you. Also this mode is not suitable for pushing into a shared central repository, as other people may add new branches there, or update the tip of existing branches outside your control.
This used to be the default, but not since Git 2.0 (simple is the new default).
So set whichever push.default
value matches your desired behaviour of git push
(simple
is the best choice but if your git is too old for that then upstream
is good as is current
) and then you can just use git push
(try git push -n
first to confirm what will happen if you want to be safe).
Upvotes: 2