TopCoder
TopCoder

Reputation: 4296

What does this GIT command do?

I'm trying to understand what this GIT command does:

git push origin master:release_candidate --force

I know git push origin master pushes the local master to the remote origin.

I would like to know the :release_candidate --force part.

Upvotes: 2

Views: 123

Answers (2)

axiac
axiac

Reputation: 72177

Let's read the documentation and extract the pieces that explain your command:

git push [-f | --force] [<repository> [<refspec>…​]]

(I omitted the myriad of options not present in the question)

OPTIONS

<repository>

The "remote" repository that is destination of a push operation. This parameter can be either a URL or the name of a remote. refspec>…

Specify what destination ref to update with what source object. The format of a <refspec> parameter is an optional plus +, followed by the source object <src>, followed by a colon :, followed by the destination ref <dst>.

The <src> is often the name of the branch you would want to push, but it can be any arbitrary "SHA-1 expression", such as master~4 or HEAD

The <dst> tells which ref on the remote side is updated with this push. Arbitrary expressions cannot be used here, an actual ref must be named.

The object referenced by <src> is used to update the <dst> reference on the remote side. By default this is only allowed if <dst> is not a tag (annotated or lightweight), and then only if it can fast-forward <dst>.

...

-f
--force

Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. [...]

This flag disables these checks, and can cause the remote repository to lose commits; use it with care.


To sum everything up, your command:

git push origin master:release_candidate --force

uses the local branch master to update the branch release_candidate of the origin remote. If the local branch master is a descendant of the remote branch origin/release_candidate then the --force flag is not needed. Otherwise, without --force the push is rejected by the remote server.

For a detailed discussion (with examples) about what happens when you use --force, please read the section Notes about fast forwards from the documentation.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992717

The :release_candidate part is a remote refspec. What that does is push your branch called master to the remote branch called release_candidate. Normally, without :, the local branch is pushed to the remote branch with the same name.

The --force option pushes even if doing so would lose commits at the remote. This is fine if what you want to do is forcibly overwrite whatever was in the release_candidate branch before.

Upvotes: 4

Related Questions