Git version control system is a way modern developers are tracking changes in the code and files for their applications. Git was invented by Linus Torvalds while he was working on the Linux operating system in the 2005 year and it is widely adopted by commercial and opensource software developers since then.
There are 2 major and free online services where you may check existing open-source code projects:Github.com
and Gitlab. Both of them allow their users to create public and private repositories with your code.
Installing the GIT command line is simple! Download it from this site, install and follow the instruction.
For the purpose of this tutorial let’s take some open repository from Github.
Choose the repository from Github‘s Trending at https://github.com/trending or use this sample repository: https://github.com/bytescout/ByteScout-SDK-SourceCode.
Now find a green “Clone or Download” button and copy the Git address. It will look like this: git@github.com:bytescout/ByteScout-SDK-SourceCode.git
To clone repository please type in this command in Command Line Console:
git clone git@github.com:bytescout/ByteScout-SDK-SourceCode.git
Git will run cloning which is essentially downloading files from the remote repository and putting all them into a local folder. You will see something like this in the console:
Cloning into 'ByteScout-SDK-SourceCode'... remote: Enumerating objects: 3380, done. remote: Counting objects: 100% (3380/3380), done. remote: Compressing objects: 100% (1613/1613), done. remote: Total 48553 (delta 1900), reused 2801 (delta 1397), pack-reused 45173 Receiving objects: 100% (48553/48553), 50.66 MiB | 1.46 MiB/s, done. Resolving deltas: 100% (35044/35044), done. Checking out files: 100% (13149/13149), done.
The folder will automatically use the same name as the remote repository: ByteScout-SDK-SourceCode.git
Now enter this folder using this command:
cd ByteScout-SDK-SourceCode
Now change README.md file. For example, just add hey hey hey at the very beginning the README.md
and save it.
Type the following command to show a list of changed files:
git status
And Git will display the list of changed files. We have just one single changed file, README.md. You will see this:
On branch master Your branch is up-to-date with 'origin/master'. Changes were 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: README.md no changes added to commit (use "git add" and/or "git commit -a")
You may want to preview differences between the old version of the file and the new version of the readme.md
To view differences please type the following command:
git diff
the output will look like this:
diff --git a/README.md b/README.md index d60cb2f..594cd44 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +hey hey hey # ByteScout SDK - Source Code ## Source code samples for:
Git compares files line by line. Lines added are marked with + symbol at the beginning of the line, removed lines are marked with – at the beginning.
So it shows that the following line was added:
+hey hey hey
Often times you want to roll back changes you’ve just made. And it is simple to do with git. Use the following command to revert changes in README.md
file:
git checkout README.md
This command will immediately rollback changes in README.md
file. You may confirm by using git status
command again:
On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
Let’s just edit README.md
file again and re-add hey hey hey
line at the beginning of this file again.
To save this change in Git repository then you should create a so-called commit
that is a set of changes along with a comment to identify why this change was made.
There is 2 steps process on making a new commit. First, we need to add changed files to the list of files for future commits. This is useful if you have a lot of changed files and you want to create separate commits for different files.
To add files into commit please use the following command:
git add README.md
You may use git status
to confirm that file was set to be included into an upcoming commit:
git status
And it will show:
On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.md
Now a new “commit” using the following command below. Include a description of why changes were made in the quotes after -m
parameter
git commit -m 'added hey hey hey into Readme file'
It will show the confirmation:
[master adc0dcf] added hey hey hey into Readme file Committer: Tester <Tester@Mac.local> 1 file changed, 1 insertion(+)
Sometime you may need to rollback the latest commit because you want to add more edits to your files and then re-commit them again.
git reset HEAD~1
it will show the following message:
Unstaged changes after reset: M README.md
IMPORTANT: this command will only cancel the latest commit but files will remain in their changed state.
If you want to revert changes in files as well then do git checkout
command for every file. To revert changes in README.md
file please type
git checkout README.md
Once you have made changes, added changed files using git add
command and then created new commit using git commit
command then you may finally push changes to the remote repository.
To send changes to remote repository use this command:
git push
For the repository that we use in this tutorial, Git will display an error because you should be authorized to make changes to this repository. But with your own repository, you will see a message indicating successful sending of changes.
To download and sync the latest changes from remote Git repository use this command:
git pull
It is similar to git clone
but git clone
should be used for the very first time downloading from a remote Git repository. To get updates for the existing repository you should use git pull
instead.
In this tutorial, you have learned how to install Git repository, how to download existing repository into a local folder on your computer, how to create a so-called commit for a set of changed files, and how to include a short comment about the changes. Also, you’ve learned how to roll back changes, how to cancel the latest commit, how to send local changes into a remote repository, and how to update your local copy and sync with a remote repository.