2014-12-05 42 views
2

我製成的殼的別名,我打算在一起使用與其他命令:使用git殼別名

this = "!f() { git rev-parse --abbrev-ref HEAD; }; f" 

的想法是,它會輸出電流的git分支我在,和使用它像這樣:

git push -u origin this 

當我只需運行別名它正確輸出的電流分支,但是當我嘗試使用它像上面的例子中它給了我一個錯誤:

error: src refspec this does not match any. 
error: failed to push some refs to 'myrepo.git' 

如果我嘗試使用相同的命令,但實際上是在編寫分支名稱(git push -u origin <branch>),它可以正常工作。

我在做什麼錯?當我將它與另一個命令結合使用時,它不能展開我的this別名嗎?

+0

git不會在命令行隨機點擴展別名(這將需要大量掃描並始終工作)。你可以得到最接近的是'git push -u origin「$(git this)」'但我不知道我看到這個別名的使用(並且它不需要封裝函數)。 – 2014-12-05 11:07:39

+0

我們使用長分支名稱,有時100多個字符。將'this'映射到當前分支名稱會很好,這樣您就不必輸入分支名稱。函數環繞僅僅是因爲我對此很陌生...... – user1021726 2014-12-05 11:09:49

+0

通過將別名映射到shell自己的別名,可以使它更短,如:'git push -u origin $(this)'...這不是完美的解決方案,但它相當短 – user1021726 2014-12-05 11:49:52

回答

2

混帳不擴大在命令行中隨機點的別名(這將需要大量的掃描和工作在任何時候都可能會破壞一個別字的任何使用在一個分支REF /等)

最接近你可以得到你想要的是git push -u origin "$(git this)"

所有的說法,除非你知道你有一個理由手動指定遠程和分支名稱,你可能不需要。

git push手冊頁說:

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.

git config手冊頁說:

remote..push

The default set of "refspec" for git-push[1]. See git-push[1].

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).

所以設置爲準push.default值匹配您的期望的行爲git pushsimple是最好的選擇,但如果你的git太舊,那麼upstream就像current一樣好),然後你可以使用git push(首先嚐試git push -n以確認如果你想要安全會發生什麼)。