Here are some notes about the more common and useful git commands.
Git is a distributed source control system. While useful for working with several people on a project it is very useful for projects involving only a single person. While git may be used on one computer a big advantage of using a repository on another system is that once files have been pushed off to the other system you have protection from a disk failure or other event that deletes files.
Some commands are done all the time and easy to remember and some are not done often:
When you connect to a git repository on a remote server such as gitlab or github you can use either the "https" or "ssh" protocol. If you use "https" you will need to repeatedly enter your password for the remote site. For ease of use put your ssh key on the git server and then use the ssh-style commands shown below to create your repository.
This is a "once only" step for each new project.
Create a directory for the project if it does not yet exist (called "newproject") and change to that directory.
Create your initial files if they do not exist. Here is a directory listing after a few files have been created:
main.c sub.c unit_tests
Create a local git repository and add all files.
git add .
Connect to the git server with a web page and create a new project. For example name "test_project".
Associate this local repository to the remote system and push the files to the git server:
git commit -m "initial commit"
git remote add origin firstname.lastname@example.org:dale/test_project
git push -u origin master
You can verify the remote URL with the command "git remove -v":
$ git remote -v
origin email@example.com:dale/test_project (fetch) origin firstname.lastname@example.org:dale/test_project (push)
You can also clone using http or https protocol (depending on what your git sever supports). This is not recommended as you will need to continually enter your password when you execute git commands.
$ git clone http://10.1.1.50/dale/test_project
Cloning into 'test_project'... Username for 'http://10.1.1.50': dale Password for 'http://email@example.com': warning: redirecting to http://10.1.1.50/dale/test_project.git/ remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6/6), done.
You can also create a new project using the server web page and initialize with a "readme" file using the option on the create project page. Then clone this project using the instructions below.
The project page displayed after creating a new project will usually display a "clone" button. On the gitlab sysdtem this is a blue button labeled "clone". If you click that it will show you the command used for both http and ssh used to clone the project.
To delete a project connect to the git server web page and select Settings -> General -> Advanced -> Remove.
This is the way you obtain all the files in an existing project and set up the directory structure to allow you to contribute to a project.
When you execute the "clone" command a new directory having the project name will be created in your current working directory. If you wish to create a new directory to hold this project do so:
Execute the clone command giving the path to the project. This example will clone "test_project" used in the example above:
git clone firstname.lastname@example.org:dale/test_project
When run the output will be:
$ git clone email@example.com:dale/test_projectCloning into 'test_project'... remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 0), reused 0 (delta 0) Receiving objects: 100% (6/6), done.
Note that when the command finishes you will have a new directory called "test_project". All the files from the project will be present:
This is a common and harmless command used to determine if there are any files which need to be checked in. If working on a project with others this shows you if there are files that someone else has placed in the repository making your files potentially out of date.
If the response to this command is "nothing to commit, working tree clean" you know that all your files have been pushed to the repository.