ameng 2020-01-11
This tutorial explains how to use GIT to create a project, add files, commit modifications and upload them in the remote repository at GitHub.
First, you should generate an SSH key. You must have at least one SSH public key to push your git repository to GitHub.
You can check our knowledgebase articles how to generate an SSH key in Windows, MAC OS and Linux.
Next, you should add the key through the GitHub interface. This is done by accessing the Settings page for your account -> SSH and GPG Keys section. On that page, click the New SSH key button.
A new panel will appear in which you should input a Title for the key and the private key itself. Once done, press the Add SSH key button.
Then, open a new Terminal window on your computer. First you should create a new folder in which to develop and manage the repository. This can be done with the following command:
1 | user@user [~]# mkdir GIT |
This will create a folder named GIT in the current working directory. Access that folder with:
1 | user@user [~]# cd GIT |
Then initialize a new GIT repository with:
1 2 | user@user [~/GIT]# git init Initialized empty Git repository in /home/username/GIT/.git/ |
You should create files and folders for your GIT repository. For example, you can create a README file with the following command:
1 | user@user [~/GIT]# touch README |
This will create an empty file named README in the GIT repository folder. However this does not automatically add the file to the GIT repo. You can compare the list of files in the actual GIT repository and the list of current files you have created with:
1 2 3 4 5 6 7 8 9 10 11 | user@user [~/GIT]# git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track) |
The above shows that you have a file named README that is present in the folder, but is not added to GIT repository. So now, you need to add the file to the GIT repository index. This can be done with:
1 | user@user [~/GIT]# git add README |
Then, you can compare again the list of files in the actual GIT repository and the list of current files you have created:
1 2 3 4 5 6 7 8 9 | user@user [~/GIT]# git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README |
The file is now added to the repo index and you need to commit it. By committing a file, you add it to the actual repository. Committing is used when you create new files or modify existing ones and want to "push" your changes to the repository. To commit the README file, you can use the following command:
1 2 3 4 5 | user@user [~/GIT]# git commit -m ‘first commit‘ [master (root-commit) e2a16e0] first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README |
where "-m ‘first commit‘" is a comment which is left in the repository history. Using commits you can describe the changes which are made to the repository files, which will help you better to follow the development of your project.
Now, if you check the status of the git repository, you will see there are no new changes to commit.
1 2 3 4 | user@user [~/GIT]# git status On branch master nothing to commit, working directory clean |
All of these changes were done to a local repository on your machine. If you want to push them to the remote GIT repo you have at GitHub, you first need to add the repo on your machine with:
1 | user@user [~/GIT]# git remote add origin :your_username/name_of_your_repo.git |
Make sure to replace your_username and name_of_your_repo with your actual GitHub username and the name of your repository. The command will "link" the two repositories, so any changes made to the local one can be pushed to the remote one at GitHub.
To push the changes to the GitHub repository now, you can use this command:
1 2 3 4 5 | user@user [~/GIT]# git push origin master Counting objects: 3, done. Writing objects: 100% (3/3), 207 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:user/test.git * [new branch] master -> master |
Now, if you access the GitHub repository, you will see the README file there.