(转)Git Push: Just the Tip

Git Push: Just the Tip

April 18th, 2008 by Rein Henrichs

Today we delve into the world of git push, one of the most often used git tools. git push is typically used to update a remote ref and associated objects based on a local ref – in other words, to push your local changes to an upstream repository – but you can also use it to create or delete remote branches and much, much more! actually, that’s about it.

Just The Tip

The most common use of git push is to push your local changes to your public upstream repository. Assuming that the upstream is a remote named “origin” (the default remote name if your repository is a clone) and the branch to be updated to/from is named “master” (the default branch name), this is done with:

git push origin master

Get used to this. You’ll be doing it a lot.

A Bit Deeper

Git uses the term “refspec” when describing the usage of some of its commands. A refspec is essentially a name that git can resolve to a commit object. This can be the name of a branch or an arbitrary “SHA1 expression” such as master~4, among others. Git gives you a lot of ways to refer to a commit but for most purposes you’ll just use the name of a branch with git push.

The kernel.org manpage for git pull will rather cryptically tell you that “The canonical format of a parameter is +?:”. Most of the time this translates to :. The branch to push from and the colon are optional. If left out, git will push from the local branch to the remote branch of the same name. If no refspec is used at all, git will push all “heads” (f.e. branches) on the local to matching heads that exist on the remote.

In practice, this means that:

  • git push origin will push changes from all local branches to matching branches the origin remote.
  • git push origin master will push changes from the local master branch to the remote master branch.
  • git push origin master:staging will push changes from the local master branch to the remote staging branch if it exists.

Tips and Tricks

Create a Remote Branch

git push origin master:refs/heads/staging will create the branch staging in the origin by copying the local @master@ branch

Delete a Remote Branch

git push origin :staging will delete the branch staging from the origin repository.

Set Up A Branch’s Default Remote

You can use git config to assign a default remote to a given branch. This default remote will be used to push that branch unless otherwise specified.

This is already done for you when you use git clone, allowing you to use git push without any arguments to push the local master branch to update the origin repository’s master branch.

git config branch..remote can be used to specify this manually.

Copied from

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值