Generic rule for gitHub commands

Hi campers,

I am quite new to git. I have just noticed you can delete a branch (let us call it test) using git branch test -d or git branch -d test. I have seen a number of commands with flags for example git push origin master --force. Are the position of these flags interchangeable for all commands. For example is git push origin master --force the same as git push origin --force master? Is there some kind of generic rule to follow for the position of these flags in commands?

(disclaimer I’m not a git guru, only a daily user)

So git has a specific command structure where flags are applied:
git <command> <flags-and-args>

as long as you pass the flags after the command (push, pull, commit) git will associate that flag with that command. So at that point you can basically put the flag however you want, and git will figure it out.

Its very possible this rule doesn’t apply to every single thing you can do with git, as git has a ton of features, but generally for things you will be doing day to day, it will hold.

I personally find I don’t follow a pattern with the order of flags and extra arguments most of the time, so just use what you feel makes sense at the time :slight_smile:

2 Likes

Well that makes sense. Let us take git push origin master --force. I know --force is a flag and push is the command. What about origin and master? Are they arguments?

Yes, in this situation origin and master can be considered “args”, and their order does matter, as both can be named anything. In this situation origin is the name of your remote, and master is the name of your branch your pushing to. If you mix in flags before, after or even between git will know your non flags* are your remote+branch.

If your really unsure, the usual of “checking the docs” can be used. The command and what they accept are documented via man pages like this one.

However deciphering the docs can sometimes be really confusing. Luckily most well written doc pages provide some examples which could be gone over to at least get an idea of how to use the commands.

1 Like