Welcome to CodeScript! In this article, we will learn about Git and GitHub and all Git commands.
What is Git?
Git is a distributed revision control and source code management system with an emphasis on speed.
Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
VERSION CONTROL SYSTEM(VCS)
Version Control System(VCS) is software that helps the software developers to work together and maintain the history of their works.
Following are two types of Version Control Systems.
- Centralised Version Control System:
Centralized version control systems are based on the idea that there is a single “central” copy of your project somewhere (probably on a server), and programmers will “commit” their changes to this central copy. “Committing” a change simply means recording the change in the central system.
2. Distributed Version Control System
A distributed version control system (DVCS) is a type of version control where the complete codebase — including its full version history — is mirrored on every developer’s computer As Shown in the above Figure. It’s abbreviated DVCS.
Git is a distributed version control system — and the most popular one at that.
Git Life Cycle
Install Git on Mac:
Download the latest Git for Mac installer.
Install Git on Linux: (Ubuntu/Debian)
From Your Shell Install using get-apt
$ sudo apt-get update $ sudo apt-get install git
Install Git on Windows
Download the latest Git for Windows installer.
Choose the Installation option according to your Operating System. Once Installed Now Verify the installation and check Git Version.
Open Your System Terminal and Run below Command
$ git --verson
Configure your Git username and email using the following commands, replacing “username” with your own. These details will be associated with any commits that you create:
$ git config --global user.name "username" $ git config --global user.email "firstname.lastname@example.org"
What is Github?
GitHub is a highly used software that is typically used for version control. It is helpful when more than just one person is working on a project. Say, for example, a software developer team wants to build a website and everyone has to update their codes simultaneously while working on the project. In this case, Github helps them to build a centralized repository where everyone can upload, edit, and manage the code files.
Command and Operation in Git
1. Initialize GIT local repository: (Repository is initialized only once at the time of configuring VCS)
$ git init
2. To check the status of tracked/untracked files and folders:
$ git status
3. To track files and folders which are yet to commit or staging:
$ git add . # to add all changes $ git add path/to/file # to add particular file
4. To commit changes:
$ git commit -m "commit message"
5. To add Remote Origin to add committed files and folder to server repository:
$ git remote add origin online repo URL Example # git remote add origin https://github.com/username/test-git.git
6. To add committed files and folders to the master branch:
$ git push -u origin master
7. To see the list of branches:
$ git branch
8. To create new branch:
$ git branch 'branch-name' Example $ git branch newbranch
9. To checkout to specific branch:
$ git checkout 'branch-name' Example $ git checkout newbranch
10. To Merge NewBranch to Master:
$ git merge 'branch-name' Example $ git merge newbranch # Make sure You are in master barnch
11. To Delete branch from Local Repository
$ git branch -d 'barnch-name' Example $ git branch -d newbranch
12. To Delete branch from Remote Repository:
$ git push origin --delete 'branch-name' Example $ git push origin --delete newbranch
13. To clone the git repository:
git clone 'repository url' Example git clone https://github.com/username/test-git.git
14. To pull the changes:
$ git pull $ git pull origin 'branch-name' # To pull specific branch Example $ git pull origin newbranch