First, you create your branch locally:
git checkout -b <branch-name> # Create a new branch and check it out
The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can do:
git push <remote-name> <branch-name>
Where <remote-name>
is typically origin
, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.
Note however that formally, the format is:
git push <remote-name> <local-branch-name>:<remote-branch-name>
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name>
(with the colon), or the remote branch will be deleted!
So that a subsequent git pull
will know what to do, you might instead want to use:
git push --set-upstream <remote-name> <local-branch-name>
As described below, the --set-upstream
option sets up an upstream branch:
For every branch that is up to date or
successfully pushed, add upstream
(tracking) reference, used by
argument-less git-pull(1) and other
commands.
Maybe you just need to commit. I ran into this when I did:
mkdir repo && cd repo
git remote add origin /path/to/origin.git
git add .
Oops! Never committed!
git push -u origin master
error: src refspec master does not match any.
All I had to do was:
git commit -m "initial commit"
git push origin master
Success!
Best Answer
With Git version 1.7.9 and later
Since Git 1.7.9 (released in late January 2012), there is a neat mechanism in Git to avoid having to type your password all the time for HTTP / HTTPS, called credential helpers. (Thanks to dazonic for pointing out this new feature in the comments below.)
With Git 1.7.9 or later, you can just use one of the following credential helpers:
The credential.helper cache value tells Git to keep your password cached in memory for a particular amount of minutes. The default is 15 minutes, you can set a longer timeout with:
Which sets the cache for 1 hour, or:
For 1 day. You can also store your credentials permanently if so desired, see the other answers below.
GitHub's help also suggests that if you're on Mac OS X and used Homebrew to install Git, you can use the native Mac OS X keystore with:
For Windows, there is a helper called Git Credential Manager for Windows or wincred in msysgit.
With Git for Windows 2.7.3+ (March 2016):
For Linux, you would use (in 2011)
gnome-keyring
(or other keyring implementation such as KWallet).Nowadays (2020), that would be (on Linux)
Fedora
Ubuntu
With Git versions before 1.7.9
With versions of Git before 1.7.9, this more secure option is not available, and you'll need to change the URL that your
origin
remote uses to include the password in this fashion:... in other words with
:password
after the username and before the@
.You can set a new URL for your
origin
remote with:Make sure that you use
https
, and you should be aware that if you do this, your GitHub password will be stored in plaintext in your.git
directory, which is obviously undesirable.With any Git version (well, since version 0.99)
An alternative approach is to put your username and password in your
~/.netrc
file, although, as with keeping the password in the remote URL, this means that your password will be stored on the disk in plain text and is thus less secure and not recommended. However, if you want to take this approach, add the following line to your~/.netrc
:... replacing
<hostname>
with the server's hostname, and<username>
and<password>
with your username and password. Also remember to set restrictive file system permissions on that file:Note that on Windows, this file should be called
_netrc
, and you may need to define the %HOME% environment variable - for more details see: