Getting started with GIT

bouchahda jouda
4 min readSep 5, 2019

--

This article introduces you to Git, learn how to use it ,manage your source code via its commands in a very simple way. You will be able to add, delete, view a commit and undo changes in a git repository.

What is GIT?

GIT is a Version Control System (VCS) to report changes done in a file. Make versions of your file as long as you make changes. With Git you can see the changes done in your project, what are the changes, who made them and when it was made. And of course if you want to go back to any previous version you can roll back to that version easily.

What you will learn?

1- Initializing a Git repository
2- Writing useful commit messages
3- Tracking files (when edited, deleted , added, moved)
4- Comparing versions
5- Undoing changes to revert back to previous version

Initializing a Git repository:

First thing to do is enter your project directory via the command line and type
git init

you have to be inside our project directory to initialize the repo.

This will initialize a git file that has a .git as extension .

Writing useful commit messages:

After making some changes in a file, we need Git to be able to track these changes, so with the command line we use
git add .” //this command will tell Git to add all the changes that are done in the directory and stage them for a commit. then
git commit -m first commit ” //this command tell Git to commit these changes and prepare to push them in a remote repository.
the m option refers to the message we wrote, here we chose `first commit´.
Normally we write meaningful messages that explains the changes that we have done during the commit.

Push your project to the remote repository in Github:

  • git remote add origin remote repo url ” // this links your local repository to the remote repository.
  • git remote -v ” // verifies the remote url.
  • git push origin master ” // pushes the changes from the local repo up to the remote one. here we are pushing directly to the master branch.
  • git push origin your branch ” // here we are pushing to a specific branch.

Tracking files:

Add new files to a project:

Create a new file ( called “ example.txt ” as an example), now type “ git status ” // this command will tell you what is going on on your project. In our case and after creating the new file , git does not know about it yet. That´s why this command will tell us that we have an untracked file and Git does not know about it, we have to commit it, so Git can track it. We will do

Git now has uploaded the new file to the remote repo.
Git now has uploaded the new file to the remote repo.

Delete a file:

To delete a file from a local copy we do “ rm file name ” . If we do “ git status ” git will tell us that the file has been deleted locally and not staged. We need then, to tell git about it so we use “ git add file name ” // this command tell git to be aware of the any change done in the file including deleting, adding the file.

Move files:

Move a file to a new location inside your project.
git status ” // git will tell you file has changed its location and needs to be commited.

Comparing versions:

To compare a specific commit we do
git diff commit-id

in this example we can see that the changes were made in file.txt; some lines were added.

git diff HEAD ^ ” // tells you all the changes made in the last commit.

Undoing changes to revert back to previous version:

To restore an old version we use the “ reset ” command.
git reset — hard commitID ”. With using option `hard`: all commits that came after this version are undone. You are exactly at the point as this version.
Another solution to consider is to create a new branch that starts with new revision by using “ git checkout -b newBranchName commitID ”. This new version starts from where you want to make the changes.
In the end it is up to you if you are sure to roll back to some point in your branch or simply create another that starts from some commit where you want to make the changes.

--

--