This post is a tutorial for new users to set up git and clone and use the first repository. This post introduces how to start using git for new users. This post does not introduce details of how to use git commands. Please refer to the git manual or other tutorials for how to commit, push, etc.
In this post, we introduce how to set up keys for git, how to configure non-standard SSH port for git, and how to use the first git repository.
Users’ SSH private/public key pairs
Every user of git, administrator or a normal user, need to have a private/public SSH key pairs in ~/.ssh.
New users need to generate the key pairs (if they don’t have one) by executing this command:
$ ssh-keygen -t rsa
After generating the key pairs, the user can give the public key (~/.ssh/id_rsa.pub) to the git server administrator and ask he/she to create a account and repository.
After the git server administrator adds the new user by its public key to the git server, the user can starts to use git.
Non-standard SSH port
This section only for the git servers that use non-stardard SSH port, i.e. other than 22.
If sshd is listening on non-standard port(s) on the git server, for example 22111, the user should set SSH to use the special port. We use example.org:22111 as the example here.
Put these two lines in your ~/.ssh/config file:
Host example.org Port 22111
If this ~/.ssh/config file doesn’t exist, you should create it first and set it’s mod to 744:
$ chmod 744 ~/.ssh/config
Create the new repository
This section only for users to create new repository. If the user is to use an already exist repository, please skip this section. Besides, the steps in this section only need to be done once for one repository.
We use the example that the administrator create repository repo1 for user1 and give user1 write privilege to this repository. We assume the address for this repository is firstname.lastname@example.org:repo1.git . Now we introduce how does user1 create the new repository repo1.
As user1 has the write privilege on the repo1 repository, it can create this repository on it’s local machine first and then push it to the git server. After pushing it to the git server.
These operations are done by user1 on its local machine:
$ mkdir repo1 $ cd repo1 $ git init $ touch README $ git add README $ git commit -a -m 'first commit' $ git remote add origin email@example.com:repo1.git $ git push origin master
If it successes, the new repository is created on the git server.
Use the repository
Please note that step 1 only need to be done for the first time. After getting the repository to a local directory on the user’s local machine, the user only need to follow step 2 to step 5.
1. Clone the repository for the first-time. You only need to do it once for the first time, for the later usage of this repository, you continue use this local directory.
$ git clone firstname.lastname@example.org:repo1.git
A directory named repo1 will be created in the current directory.
If you want to use the repository directory in the previous section, just add these line to the end of .git/config:
[branch "master"] remote = origin merge = refs/heads/master
2. Pull the updates made before by you or the others. Use “git pull” command in the repository’s directory.
$ cd repo1 $ git pull
Then you can work on this updated copy of the repository by editing the files.
If you add a file or directory (for example, add directory a/b and file a/t.txt), you can add it to the repository by:
$ git add a/b a/t.txt
3. Commit the changes made by you with a message
$ git commit -a -m 'update the files'
4. Push it to the git server
$ git push
By now, your changes have been pushed to the git server. When the others use “git pull” command, they will get the new version of the source codes that have changed by you.
You can use “git log” to see the logs of all the commits of this repository.
You may be also interested in more git solutions.