DEV Community

Cover image for Git for begginers
gdahboy
gdahboy

Posted on

Git for begginers

Table Of Contents

Introduction
Version Controle System

Introduction


in this post you will learn a Version Control System called git . you will be able also to distingue the difference between centralized vcs and distributed vcs . and mostly be able to use git in your individual project , group project or community project .

Version Controle System

Version control systems are a category of software tools that help you change to source code over time. this software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members. so the main benifit of using these softwares are :


benifit


*turn back the clock whatever you want
*keeps track of everything
*facility in recreating things and evolute

Centralized VCS



A centralized version control system works on a client-server model. There is a single, (centralized)master copy of the code base, and pieces of the code that are being worked on are typically locked, (or “checked out”) so that only one developer is allowed to work on that part of the code at any one time. Access to the code base and the locking is controlled by the server. When the developer checks their code back in, the lock is released so it’s available for others to check out. Exemple :

#svn



distributed vcs



The whole approach of central VCS is reversed and each collaborator will have the complete repository on his local machine i.e. the complete revision history, all the branches, tags, commit information is present on the local machine. We do not have a notion of a central server, but we can configure any repository to be a central repository to treat as a source of truth . Exemple

#git

Software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.


comparison between Git and SVN


SVN GIT
Version management Central Distributed
Repository A central repository in which working copies are generated Locally available repository copies in which files and documents can be worked on
Change history Only complete in the repository. Working copies only contain the newest version Repository and working copies contain the complete history
Network connection Needed for access Only necessary for synchronization


Git

Now for the main Topic Git !!! as i mentiond eraly git is a timemachine for our code. we can go back to any moment of life of our programme at anytime . and that's thanks to Commit .

Commit


it represent a life-moment in code , that's why i advice all of you to commit every important add ( for examaple : function , methode , class , test ... ) .
it has a unique id , date , author name and email included and none of this information can be changed .

Branch

branches can be represented by team member (every team member have it owns branch ) or by code version (we can have in the same project , 2 branches and every branch represent different logic and code )

let's get started

1.Config
now let's jump to our terminal (OS X , Linux . Windows users need to download git bash) make sure git is installed by

git --version 
Enter fullscreen mode Exit fullscreen mode

then configure your email and username (change your username and email )

git config --global user.name  Gdahboy 
git config --global user.email abderrahim.gdah@esprit.tn 
Enter fullscreen mode Exit fullscreen mode

2.first commit
now jump to your code repository and type :

git init 
Enter fullscreen mode Exit fullscreen mode


now we will have 3 repo : your working directory (represented in blue in image ) , Staging area (brown) , Local respository (green) they are all logical Repository of course . indeed they represent the lifecycle of working with git. every file exists in Blue , if you add the file you tell git that you want to keep tracking the file , so every modification will be saved in brown and if you commit that change this modification will remain forever and you could get it back whenever you want , because it will be saved in green . Purple (purple repository ) represent github , gitlab ... where all the folder will be saved .
enough talk , let's commit something !!!
sorry , i need to remeber you first we need to add file in order to commit

git add file.c file.txt ...
Enter fullscreen mode Exit fullscreen mode

and finally !!!!

git commit -m "first commit" 
Enter fullscreen mode Exit fullscreen mode

so far , everything is great . but how could we know if a file is addes or not or if git is tracking the changes that we want , it is even easier than we think

git status 
Enter fullscreen mode Exit fullscreen mode


again one more time, even if we add a file to stage area (brown repo) and we commit the file, every change that come after the commit , before to commit the change again you need to add the file first !!!
3.display all commit

git log 
Enter fullscreen mode Exit fullscreen mode

or for less information

git log --pretty=one 
Enter fullscreen mode Exit fullscreen mode


4.back in time !!
now if we want to go back in time in our code we can do

git revert c7aaab7e8efdb750da70a535fb393d1329173bf5
Enter fullscreen mode Exit fullscreen mode

(c7aaab7e...) of course this is the id of random commit you need to change that with the id of commit you like (you can get the id from git log )

  1. let's build a branch so, what if we go back in code and we find something different and you have 2 idea . so you can create branch from old commit that inheret all the code from that commit .
git branch NameOfBranch
Enter fullscreen mode Exit fullscreen mode
  1. navigate between branch
git checkout NameOfBranch
Enter fullscreen mode Exit fullscreen mode
  1. submit your code to github
git push origin master
Enter fullscreen mode Exit fullscreen mode

Conclusion
so to resume, we need to add the file to keep track of it, then commit it, new idea we will have new branch of course git have a lot more feature, we didn't even cover 10% of it's features (that we will do next article ). finally i find this on web, i really like it. it will help you a lot and will introduce for you many new concepts please don't hesitate to text me anytime .

Top comments (0)