Introduction:
Git is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows. Git is commonly used for source code management (SCM) and has become more used than old VCS systems like SVN.
GitHub is a Web-based Git repository hosting service, which offers all of the distributed revision control. Unlike Git, which is strictly a command-line tool, GitHub provides a Web-based graphical interface. GitHub is used for ‘version control’. This means that GitHub is used for software development projects when more than a person(or a group) is working on it. What GitHub does is that it creates a cloud-based centralized repository for everyone working in the group and allows everyone working on the project to update the information.
Installing Git on Ubuntu Linux
# apt-get install git Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn The following NEW packages will be installed: git 0 upgraded, 1 newly installed, 0 to remove and 720 not upgraded. Need to get 0 B/3,176 kB of archives. After this operation, 24.1 MB of additional disk space will be used. Selecting previously unselected package git. (Reading database ... 210076 files and directories currently installed.) Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.6_amd64.deb ... Unpacking git (1:2.7.4-0ubuntu1.6) ... Setting up git (1:2.7.4-0ubuntu1.6) ...
Goto www.github.com and sign into your account. If you’re a new user, you can simply sign-up.
You’ll have a username from here. Let us say that it’s your_username
github-homepage
Configuring Git:
Go back to the terminal and type this to configure git
# git config --global user.name asifkhanofficial
Now type this to link your email too.
# git config --global user.email asifkhanofficial@hotmail.com
We will define our default editor. We are using gedit for this.
# git config --global core.editor gedit # git config --list user.name=asifkhanofficial user.email=asifkhanofficial@hotmail.com core.editor=gedit
Describe –global flag
Multiple repositories can be created in a single machine. If you use the –global flag, the setting will applied to all the repositories of the machine. If you want this identity only for specific repository, then do not use –global flag.
Using Git:
Go to your github account and create a repository with a name(lets say name of your project). We are creating a repository with the name myproject.
github-new-repository
Git Repository
Git repository is a folder where all the data of our project will be stored. It can be located on the local machine or on a remote machine.
Difference between normal folder and Git repository
Normal folder contains only files and directories.
Git repository contains files and directories along with their complete history.
Make a folder with the name of your project and change your current directory to that directory.
# mkdir myproject # cd myproject/ # git init Initialized empty Git repository in /root/myproject/.git/
Now we will set up the remote, which tells git where the repository is located.
git remote add origin https://github.com/asifkhanofficial/myproject.git
We have now configured and installed git and, created and configured a repository. Lets say we have a simple html files in the myproject folder for demonstration and we want it to share it with a friend who is working on the same project.
# gedit mypage.html &
<html>
<head>
<title> Main Title </title>
</head>
<body>
<h1> Type heading here </h1>
</body>
</html>
To add this file we will type.
# git add mypage.html
Or if we have a lot of files to be transferred from the folder to our git account, then we can use the command.
# git add .
# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: mypage.html
So we will make some changes in the same file and will check the result.
<html>
<head>
<title> Main Title </title>
</head>
<body>
<h1> Type heading here </h1>
We are making some changes to test the git commands.
</body>
</html>
# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: mypage.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: mypage.html
Git Staging Area:
Staging area is a file that stores information of the changes that need to be committed. The file contents should be added to the staging area, before committing them. Older git versions used the term index instead of staging area.
We will again run below mentioned command to add changes in staging area.
# git add mypage.html
Now file changes are ready to be committed.
# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: mypage.html
When we attain a particular stage, we can save our work in the repository. This is called commit. Each commit is saved with the information of username, email-id, date, time and commit message.
# git commit -m "Initial Commit" [master (root-commit) 4ddd661] Initial Commit 1 file changed, 14 insertions(+) create mode 100644 mypage.html
Now we will use git another useful command as shown below.
# git log
commit 4ddd661fdfebabab31659f4e1ce23eecaa0badc4
Author: asifkhanofficial <asifkhanofficial@hotmail.com>
Date: Tue Feb 19 12:59:41 2019 -0800
Initial Commit
After commit we will have an ID which is a unique ID of 40 alpha-numeric characters. Git stores all the information in its database by the hash value. Git commits are identified by the SHA-1 hash.
Next, we need to push the commit that we just made on to the repository at github
# git push origin master
It would automatically ask you for your username and password for github. After entering the details, go to github and refresh. The files would get added there.
Username for 'https://github.com': asifkhanofficial Password for 'https://asifkhanofficial@github.com': ************** Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 334 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/asifkhanofficial/myproject.git * [new branch] master -> master
We have successfully transferred a file on your github account. Now lets add one more file aboutme.txt and edit our file mypage.html. Following the same procedure we will first add the files, commit and then push them to the github account.
# git add .
# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: aboutme.txt
modified: mypage.html
# git commit -m 'This is second commit'
[master 368c5a4] This is second commit
2 files changed, 1 insertion(+)
create mode 100644 aboutme.txt
# git push origin master
Username for 'https://github.com': asifkhanofficial
Password for 'https://asifkhanofficial@github.com':
Counting objects: 4, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 381 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/asifkhanofficial/myproject.git
4ddd661..368c5a4 master -> master
# git status
On branch master
nothing to commit, working directory clean
When we would go to our github account, we would see the entire hierarchy of the modification of the file. Here, we would see the changes we made to the mypage.html file in the respective commits.
github-second-commit
Now, Lets say one of the co-worker of the project needs to work on mypage.html. After making some changes, he wants to update the file on github.
First he would have to download the whole repository in which the file mypage.html is present into his system.
git clone https://github.com/asifkhanofficial/myproject.git
A folder named myproject gets downloaded with all the files in it. The necessary changes are made and then the file is similarly added, committed and pushed similarly as above.
# git clone https://github.com/asifkhanofficial/myproject.git Cloning into 'myproject'... remote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (4/4), done. remote: Total 7 (delta 1), reused 7 (delta 1), pack-reused 0 Unpacking objects: 100% (7/7), done. Checking connectivity... done. # ls -l drwxr-xr-x 3 root root 4096 Feb 19 13:30 myproject
So far, you’ve pushed your changes from a local repository to a remote repository and cloned a remote repository. We haven’t said anything about the “pull” command, though. Pushing changes to GitHub or BitBucket is great. But when other developers push their changes to a remote repository, you’ll want to see their changes on your computer. That is, you’ll want to pull their code to your local repository. To do so, run the following command:
# git pull
Running “git pull” is enough to update your local repository.
Wait, but you said I could clone a repository. Why do I have to pull something?
That’s a valid question, so let’s clarify.
Cloning a repository is very different from pulling from a repository. If you clone a remote repository, Git will:
- Download the entire project into a specified directory; and
- Create a remote repository called origin and point it to the URL you pass.
The last item simply means that you don’t need to run “git remote add origin git@github.com:YourUsername/your-app.git” after cloning a repository. The “clone” command will add a remote origin automatically, and you can simply run “git push” from the repository.
When you run the “pull” command, Git will:
- Pull changes in the current branch made by other developers; and
- Synchronize your local repository with the remote repository.
The “pull” command doesn’t create a new directory with the project name. Git will only pull updates to make sure that your the local repository is up to date.
Conclusion:
Basically, that’s all you need to know about pushing, pulling, and cloning with Git. There are more tutorials that will go into deeper explanation of using Git and GitHub on linux command line, but this is a simple article to just get familiar with the concept.