Git essentials: Get started with Git version control

Learn how Git manages versions and how to install the necessary software to access Git servers where your software project will be stored

1 2 Page 2
Page 2 of 2
  • You have installed the git or git-all software package on your system.
  • You have created an account on a Git hosting service.

The first thing you want to do is perform some basic setup. Whenever you perform a commit operation, your name and email address will be included in the metadata. To set this information, execute the following commands:

ocs@ubuntu:~$ git config --global user.name "Bo Rothwell"
ocs@ubuntu:~$ git config --global user.email "bo@onecoursesource.com"

Obviously you will replace "Bo Rothwell" with your name and "bo@OneCourseSource.com" with your email address. The next step is to clone your project from the Git hosting service. Note that before cloning, only one file is in the user's home directory:

ocs@ubuntu:~$ ls
first.sh

The following cloned a project named ocs:

ocs@ubuntu:~$ git clone https://gitlab.com/borothwell/ocs.gi
Cloning into 'ocs'...
Username for 'https://gitlab.com': borothwell
Password for 'https://borothwell@gitlab.com':
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.

After successful execution, notice a new directory in the user’s home directory:

ocs@ubuntu:~$ ls
first.sh  ocs

If you switch to the new directory, you can see what was cloned from the repository (only one file so far exists in the repository):

ocs@ubuntu:~$ cd ocs
ocs@ubuntu:~/ocs$ ls
README.md

Next, create a new file in the repository directory. You can either create one from scratch or copy a file from another location:

ocs@ubuntu:~/ocs$ cp ../first.sh

Remember, anything placed in this directory is not version controlled because this is the working directory. To put it in the local repository, you first have to add it to the staging area and then you need to commit it to the repository:

ocs@ubuntu:~/ocs$ git add first.sh
ocs@ubuntu:~/ocs$ git commit -m "added first.sh"
[master 3b36054] added first.sh
1 file changed, 5 insertions(+)
create mode 100644 first.sh

The git add command places the file in the staging area. The git commit command takes all the new files in the staging area and commits them to the local repository. You use the -m option to add a message; in this case the reason for the commit was given.

It is important to highlight that no changes have been made to the repository on the server. The git commit command only updates the local repository. You can see that the server repository has not been modified by looking at Figure 7, which shows a screenshot of the web-based interface of the current project. Notice that the original file, README.md, was pushed to the server several days ago, but the new file, first.sh, does not have an entry.

git-essentials ch12fig07 Pearson

Figure 7. Server repository is unchanged after executing the git commit command.

Most likely you would make additional changes to your local project and then check in (push) the changes to the server’s repository:

ocs@ubuntu:~/ocs$ git push -u origin master
Username for 'https://gitlab.com': borothwell
Password for 'https://borothwell@gitlab.com':
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 370 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://gitlab.com/borothwell/ocs.git
   12424f5..3b36054 master -> master
Branch master set up to track remote branch master from origin.

See Figure 8 to verify the push was successful.

git-essentials ch12fig08 Pearson

Figure 8. Server repository is changed after executing the git push command.

At this point, all changes of the files from the staging area have been updated to the local repository and the central server repository.

This story, "Git essentials: Get started with Git version control" was originally published by InfoWorld.

1 2 Page 2
Page 2 of 2