Introduction – What is GIT ?
Git is a free and open source version control system. Unlike older centralized version control systems such as SVN and CVS, Git is distributed : every developer has the full history of their code repository locally.
Git also has excellent support for branching, merging, and rewriting repository history, which has lead to many innovative and powerful workflows and tools. Pull requests are one such popular tool that allow teams to collaborate on Git branches and efficiently review each others code.
All developpers can work on their side but let the master branch containing all tested and reliable features developed by everyone :
Git is the most widely used version control system in the world today and is considered the modern standard for software development.
Let’s get started – Installation and testing
Obviously, the first thing to be able to use GIT is to install git. On CentOS, just run the following command :
yum install git-all -y
You can find the official documentation here.
You can then check if git is correctly installed by running :
git --version
Then, you need to create your local Git repository in order to be able to use it.
Go into your project folder (here git-demo) and add a local Git repository to the project using the following commands:
cd git-demo
git init
The git init command adds a local Git repository to the project. You can now create files, change them and commit your code !
Staging and Committing changes
After creating files and making changes, you can now commit them.
Git has something called the “staging area“. This is an intermediate area where commits can be formatted and reviewed before completing the commit.
One thing that sets Git apart from other tools is that it’s possible to quickly stage some of your files and commit them without committing all of the other modified files in your working directory or having to list them on the command line during the commit.
Remote repositories
In my previous examples, we have been using a local repository. Each developer will work in their local repository but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code.
Most famous remote repository are GitLab, Bitbucket and GitHub.
Most used Commands
To use Git, developers use specific commands to copy, create, change, and combine code.
Here are some common commands for using Git:
- git init initializes a brand new Git repository and begins tracking an existing directory. It adds a hidden subfolder within the existing directory that houses the internal data structure required for version control.
- git clone creates a local copy of a project that already exists remotely. The clone includes all the project’s files, history, and branches.
- git add stages a change. Git tracks changes to a developer’s codebase, but it’s necessary to stage and take a snapshot of the changes to include them in the project’s history. This command performs staging, the first part of that two-step process. Any changes that are staged will become a part of the next snapshot and a part of the project’s history. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work.
- git commit saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that’s been staged with git add will become a part of the snapshot with git commit.
- git status shows the status of changes as untracked, modified, or staged.
- git branch shows the branches being worked on locally.
- git merge merges lines of development together. This command is typically used to combine changes made on two distinct branches. For example, a developer would merge when they want to combine changes from a feature branch into the main branch for deployment.
- git pull updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.
- git push updates the remote repository with any commits made locally to a branch.
You can find the official documentation here !
Sources
atlassian.com
github.com
wikipedia.org