HOW TO USE GIT ❓
What is GIT?
❉ Git is a distributed Version Control System which was initially designed and developed by Linus Torvalds for Linux kernel development.
❉ It is used to save different versions of a file or a set of files and allows us to revert one or more files to any of their previous versions at anytime.
❉ It provides us the facility to compare changes done to one file between two versions, so that we can see exactly what was changed in each file, when and why it was changed and also who made the change.
❉ C language is used by Git.
What is a Version Control System?
❉ A Version Control System a system that is used to track changes done to a file (a set of files) over time, so that you can recall specific versions later.
❉ It also refers to the method used to save the versions of a file for future reference.
Types of Version Control Systems
🔴 Localized version Control System
🔴 Centralized version control systems
🔴 Distributed version control systems
Localized Version Control System
🔸 It is one of the simplest and common approaches that is error prone.
🔸 It uses a local database on your local computer, in which changes made to every file is stored under revision control.
🔸 As everything is stored locally, all the patches will get lost if something happens to the local database.
🔸 And also, if anything happens to a single version, all the changes done after that version would be lost. Therefore, it has a single point of failure.
Centralized Version Control System
🔸 Centralized Version Control Systems were developed with the aim of collaborating with developers on other systems.
🔸 This consists of a single server and has all the file versions.
🔸It allows multiple clients to access files on the server simultaneously, so that it provides an overview of what others are doing on the project.
🔸If backups are not kept properly, you will end up loosing almost everything if the hard disk of the central database gets corrupted.
🔸 Required things to make your changes visible to others:
- You commit
- They update
Distributed Version Control System
🔸 Multiple repositories are available in these version control systems.
🔸 In distributed version control systems, the user has a local copy of a repository which consists of all the files and metadata available in the main repository.
🔸 Git is the most popular example of distributed version control systems.
🔸Required things to make your changes visible to others:
- You commit
- You push
- They pull
- They update
Benefits of Version Control Systems
💠 Provide efficient collaboration.
💠 Increase the development speed.
💠Accelerate product delivery.
💠 Diminish possible errors through traceability.
💠Can contribute to project from anywhere irrespective of the geographical locations.
💠 Can access the previous versions of the project easily.
💠 Possible to get information about a file easily.
What is GitHub?
❉ GitHub is a provider of Internet hosting that also provides a Web-based graphical interface.
❉ It facilitates the users with many features features namely, access control and collaboration.
❉ It offers distributed version control and source code management functionalities of Git.
❉ GitHub has now acquired by Microsoft.
Git Vs GitHub
Git Terminology
Repository
⚜️ A repository is like a data structure that contains a collection of files of various different versions of a Project.
⚜️ All the project-related data are stored in a repository.
⚜️ Users can perform various operations such as addition of files, creation of new repositories, committing an action, deleting a repository.
⚜️ There are 2 types of repositories.
- Bare Repositories: These repositories are used to share the changes that are done by different developers. Modifications are not allowed.
- Non-bare Repositories: These repositories allow the users to perform actions such as create new modifications of files, create new versions for the repositories etc. Hence, non-bare repositories are user friendly.
Branch
⚜️ A branch is an essential feature in most version control systems that can be introduced as a version of the repository that diverges from the main working project.
⚜️ A Git project can consist more than one branch.
⚜️ Branches act as a pointer to a snapshot of your changes.
⚜️ When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. Therefore, it is complex to merge the code that is unstable with the main code base.
Git Master Branch : This is the default branch in Git that is instantiated at the moment when the committing for the first time on the project. Only one master branch can be there on a repository. This can be introduced as the official working version of your project in which all the changes eventually get merged back.
Operations on branch
- Creating a new branch
$ git branch <branch name>
2. Listing all available branches in your repository
$ git branch — list
or
$ git branch
3. Renaming a branch
$ git branch -m <old branch name><new branch name>
4.1 Switching from master Branch
$ git checkout <branch name>
4.2 Switching to master Branch
$ git branch -m master
5. Merging a Branch
$ git merge <branch name>
6.1 Deleting a specified branch
A safe operation that prevents you from deleting the branch if it has unmerged changes
$ git branch -d<branch name>
Another format of deleting the specified branch
$ git branch -D <branch name>
6.2 Deleting a remote branch from Git desktop application
$ git push origin -delete <branch name>
Git Status
⚜️This is used to display the state of the working directory and the staging area.
⚜️ It lets you see the changes that have and have not been staged and the files that are not being tracked by Git.
⚜️ It does not show you any information about the committed project history.
$ git status
Git Init
⚜️This is the first command that you will run on Git and it is used to create a local repository.
⚜️ In order to create a blank repository;
$ git init
Checkout
⚜️ This is used to switch between branches in a repository.
⚜️ Git checkout command operates on three different entities namely;
☞ Files
☞ Commits
☞ Branches
Git Commit
⚜️ This command is used to record the changes in the repository.
⚜️ It generates a commit-id when commit the changes.
$ git commit -m " Commit Message"
Git Clone
⚜️This is used to make a copy of any target repository which can either be remote or local.
⚜️ Your repository can be cloned from the remote repository to create a local copy on your system.
⚜️ Syncing between the two locations is also possible.
$ git clone URL
Git Push
⚜️This is used to upload local repository content to a remote repository.
⚜️ Pushing must be done carefully as it can overwrite changes.
⚜️ The push command is used to transfer commits between local and remote repositories.
git push -u <remote> <branch-name>
To push the local content on the master branch of the remote location
$ git push origin master
To push all the branches to the server repository
$ git push — all
Git Pull
⚜️This command is used to access the changes (commits)from a remote repository to the local repository.
⚜️ The local branches will be updated with the remote-tracking branches.
$ git pull URL
Git Log
⚜️Git log can be known as a utility tool that is used to review and read the history of what happens to a repository.
⚜️ It contains the data such as;
- A commit hash
- Commit Author metadata
- Commit Date metadata
- Commit title/message
$ git log
Git Fork
⚜️It is a safe way that allows making a rough copy of the project.
⚜️ Steps for forking the repository;
- Login to the GitHub account.
- Find the GitHub repository which you want to fork.
- Click the Fork button on the upper right side of the repository’s page.
Git Fetch
⚜️ This is used to pull the updates from remote-tracking branches.
⚜️ Apart from that, we can receive the updates that are pushed to our remote branches to our local machines using fetch commands.
⚜️ There are different uses of fetch command.
- To fetch the remote repository:
$ git fetch< repository Url>
2. To fetch a specific branch:
$ git fetch <branch URL><branch name>
3. To fetch all the branches simultaneously:
$ git fetch -all
4. To synchronize the local repository:
$ git fetch origin
Tag
⚜️ Tags are used to mark a commit stage as important.
⚜️ A tag can be committed for future reference.
⚜️ There are two types of tags.
- Light-weighted tag
- Annotated tag
Other Version Control Tools
🔴 GitHub, Bitbucket, and GitLab are three different code collaboration and version control tools that aid in repository management.
🔴 They are cloud based hosting services that allows managing Git repositories.
🔴 They provide hosting services for Git.