Create your first repository, then add and commit files
At the command line, first verify that you have Git
installed:
On all Operating Systems:
git --version
On UNIX-like Operating Systems, you can also run this command:
which git
If nothing is returned, or it complains about the command not being recognized, you may have to install Git
on your system by downloading and running the installer. See the Git homepage for installation instructions.
After installing git, it may be a good time to set up a username and email address: see Setting your username and email for instructions. You'll have to do this before making a commit.
Once Git
is installed, navigate to the directory that you want to place under version control and create an empty Git repository:
git init
This creates a hidden folder, .git
, which contains the plumbing needed for Git
to work. Next, tell Git
that you want to place all files from the current directory under version control:
git add .
This will "stage" all files to be added to version control, making them ready to be committed in your maiden commit.
If you don't want all the files to be added to version control, create a file named .gitignore
and edit it before running the add
command so that these files will not be included.
Commit all the files that have been added, along with a commit message:
git commit -m "initial commit"
This creates a new commit with the given message. A commit is a bit like a save or snapshot of your entire project. You can now push, or upload, it to a repository now, and later, you can jump back to it if necessary.
Clone a repository
The git clone
command is used to copy an existing Git repository from a server to the local machine.
For example, to clone a GitHub project:
git clone https://github.com/username/projectname.git
To clone a Bitbucket project:
git clone https://[email protected]/username/projectname.git
This creates a directory called projectname
on the local machine, containing all the files in the remote Git repository. This includes source files for the project, as well as a .git
sub-directory which contains the entire history and configuration for the project.
To clone the repository into a specified directory, such as MyFolder
:
git clone https://github.com/username/projectname.git MyFolder
Note:
- When cloning to a specified directory, the directory must be empty or non-existent.
- You can also use the
ssh
version of the command:
git clone [email protected]:username/projectname.git
The https
version and the ssh
version are equivalent. However, some hosting services such as GitHub recommend that you use https
rather than ssh
.
Sharing code
You wish to share your code. You first create an accessible repository on a remote server to which you will copy your local repository. To minimise the use of space on the remote server you create a bare repository: one which has only the .git objects and doesn't create a working copy in the filesystem. As a bonus you set this remote as an upstream server to easily share updates with other programmers.
On the remote server:
git init --bare /path/to/repo.git
On the local machine:
git remote add origin ssh://username@server:/path/to/repo.git
(Note that ssh: is just one possible way of accessing the remote repository.)
Now copy your local repository to the remote:
git push --set-upstream origin master
Adding --set-upstream
(or -u
) created an upstream (tracking) reference which is used by argument-less git commands, e.g. git pull
.
Learning about a command
To get more information about any git command – i.e. details about what the command does, available options and other documentation – use the --help
option or the help
command.
For example, to get all available information about the git diff
command, use:
git diff --help
git help diff
Similarly, to get all available information about the status
command, use:
git status --help
git help status
If you only want a quick help showing you the meaning of the most used command line flags, use -h
:
git checkout -h
Setting your user name and email
You need to set who you are before creating any commit.
That will allow commits to have the right author name and email associated to them.
It has nothing to do with authentication when pushing to a remote repository
(e.g. when pushing to a remote repository using your GitHub or BitBucket or GitLab account)
To declare that identity for all repositories, use git config --global
This will store the setting in your user's .gitconfig
file: e.g. $HOME/.gitconfig
or for Windows, %USERPROFILE%\.gitconfig
.
git config --global user.name "Your Name"
git config --global user.email [email protected]
To declare an identity for a single repository, use git config
inside a repo.
This will store the setting inside the individual repositry, in the file $GIT_DIR/config
. e.g. /path/to/your/repo/.git/config
.
cd /path/to/my/repo
git config user.name "Your Login At Work"
git config user.email [email protected]
Settings stored in a repositorie's config file will take precedence over the global config when you use that repository.
Tips: if you have different identities (one for open-source project, one at work, one for private repos, ...), and you don't want to forget to set the right one for each different repos you are working on:
-
Remove a global identity
git config --global --remove-section user.name git config --global --remove-section user.email
-
To force git to look for your identity only within a repository's settings, not in the global config:
git config --global user.useConfigOnly true
That way, if you forget to set your user.name
and user.email
for a given repository and try to make a commit, you will see:
no name was given and auto-detection is disabled
no email was given and auto-detection is disabled
Setting up the upstream remote
If you have cloned a fork (e.g. an open source project on Github) you may not have push access to the upstream repository, so you need both your fork but be able to fetch the upstream repository.
First check the remote names:
$ git remote -v
origin https://github.com/myusername/repo.git (fetch)
origin https://github.com/myusername/repo.git (push)
upstream # this line may or may not be here
If upstream
is there already (it is on some git versions) you need to set the url (currently it's empty):
$ git remote set-url upstream https://github.com/projectusername/repo.git
If the upstream is NOT there, or if you also want to add a friend/colleague's fork (currently they do not exist):
$ git remote add upstream https://github.com/projectusername/repo.git
$ git remote add dave https://github.com/dave/repo.git
Sign up or log in
Save edit as a guest
Join Stack Overflow
We recognize you from another Stack Exchange Network site!
Join and Save Draft