top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Git concepts and how to use them in projects

Imagine an application being developed. A team of developers and testers working on developing the code, writing user stories, testing their code and analyzing the results. The source code being developed has to be shared among the team members as various features of the application will be integrated. One way is to share files and documents individually with the whole team, update them on the changes you have made and then work together to integrate the code at a source level. In large and complex projects it may take a long time to coordinate and integrate the whole team’s code. there may be errors, omissions and revisions in code that may not reflect. This led to the need for a version control system which provides a way to manage different versions of a collection of files at any given time. Git was initially designed and developed by Linus Torvalds. It is free open-source software that can be used for version control in a distributed environment.


Source:https://github.com/git

Types of Version Control Systems


There are two types of version control systems:

  • Centralised Version Control System (CVCS)

    • CVCS uses a central server to store all files and allows for teams to coordinate various versions of their work. Teams have to be connected on the web to share files but a major challenge of this system is the fallibility of its central server. If the central server fails due to network-related or outage issues, the whole system grinds to a halt. Without comprehensive backup at a local level, it may not be possible for a team to access their project files also.


  • Distributed Version Control System (DVCS)

    • In DVCS there is a central repository, and every contributor has a local copy or clone of the main repository on their local system. So, everyone maintains a local repository of their own which contains all the files and metadata present in the main repository. Every checkout is a full backup of the repository. The client needs to push the changes to the branch which will then be pushed to the central repository.

Centralised Version Control System

Distributed Version Control System

​In CVCS, a client needs to get the local copy of source from the server, make the changes and commit those changes to the central server.

​In DVCS, each user can have a local repository and have a complete history of the code. The user needs to push the changes to the specific branch which are then pushed to the central repository.

CVCS system is easier to set up and adopt.

It is slightly more complex as there are multiple commands to be used for handling different types of situations while pushing, pulling and merging the code.

​Working on branches is bit complex in CVCS as developers often face conflict in merging.

Working on branches is easier in DVCS as it provides ways to resolve conflict.

Does not provide offline access and is a bit slower.

​Works fine on offline mode as the user has a copy of the repository on the local machine and works faster.

​It is difficult to carry out code completion in case system is down or there are network issues.

If server is down, work can be done using local versions on the local machines.

How does Git work


Every local repository in Git has a working tree. There are files in the working tree which may be new or revisions of the original files. The user is authorized to change, create or delete these files. After making changes to the files present in git, the user has two options. One, to push these changes to the main branch. Second, maintain the revised version or revert to the original version in the local git itself. Suppose the user decides to make changes in their working directory and update those changes in the main branch, the following process needs to be followed:


Terminologies used in Git


  1. Push—Used to upload the local repository content to a remote repository.

  2. Pull—Used to fetch content from the remote repository and update the local repository to match that content.

  3. Modified—Modified means that changes have been made to the file but the file has not yet been committed to the database yet.

  4. Staged—Staging is one step before the commit process. It means that there is a modified file ready to go into the next commit snapshot.

  5. Committed—Committed means that the data is safely stored in the local database.

  6. Clone—It creates the instance of the repository. It checks out the working copy and mirrors the complete repository.

  7. Branch—A branch is a pathway in which the user is currently working. The user can work on, let’s say, a dev branch and push the changes to the main branch. The user can decide when to merge the changes to the main branch.

  8. Head—It is a reference used to point to the currently checked-out branch.

  9. Repository—It refers to the history, different versions of the files over time and all the branches and tags. in Git each copy of the repository is a complete repository.

  10. Revision—Represents a version of the source code.

  11. Tag—It points to a commit which uniquely identifies a version of the Git repository.

  12. URL—A URL in Git represents the location of the repository.

  13. Working tree—Contains the set of working files for the repository.


Example of a Git branch

How to install and configure Git


We can download Git from the official website and install the required version based on our operating system. To verify that Git is installed properly, go to the command line and run this command.

  1. Once Git is installed, the next step is to set your username and password.

    1. git config –global user.name “git_username”

    2. git config –global user.email “email_address”

  2. Create a local directory using the command mkdir testing

  3. Initialise the directory using the command git init

  4. Goto testing folder and create a simple text file called demo1.txt

  5. Go to gitbash and check the status by using git status

  6. Add the text file to the current working directory using git add demo1.txt

  7. Make a commit using the command git commit -m “Committing demo1.txt file”

  8. Create an account on Github and then create a new repository called testing_repo

  9. Copy the link of this remote repository, we will use it to link it to our local repository

  10. Go back to git bash and link the remote and local repository using the command git remote add origin <link>

  11. Go to testing_repo in Github and you can see the demo1.txt file there.

List of commonly-used Git commands


The following is the list of some useful git commands I came across while working with my team:


  1. Go to project folder

    1. CD [project path]

  2. To see status of all files

    1. git status

  3. To add file

    1. git add [filepath]

  4. To commit changes

    1. git commit -m"Message"

  5. create new branch

    1. git branch [branch name]

  6. switch to branch

    1. git checkout [branch name]

  7. To fetch all branches from remote origin

    1. git fetch --all

  8. To see all branches at your machine

    1. git branch -a

  9. to reset all local changes

    1. git reset --all

  10. To hard reset files to Head (to be used judiciously)

    1. git reset --hard head

  11. Create new branch from remote origin

    1. git checkout -b [branch1] origin/[branch2]

  12. To pull a file

    1. git pull

  13. This command puts all your local changes first

    1. git pull --rebase

  14. force to switch branch

    1. git checkout -f staging

  15. To merge branches

    1. git merge [your local branch name]

  16. To restore deleted file

    1. git checkout head [file path]

  17. To push new local branch to server

    1. git push -u origin head

  18. Remove folder forcefully

    1. git rm -r -f test-output/*

  19. To revert changes made to your working copy

    1. git checkout [filepath]

  20. To make a copy of your changes

    1. git stash

  21. To get that copy back

    1. git stash pop

  22. replace file with origin branch

    1. git checkout origin/dev


Conclusion


Being open source, Git is freely available to download and use over the internet. As most of the operations are performed locally, there is immense benefit in terms of speed. Though Git mirrors the entire repository, the size of the data on the client side is small. As multiple copies of the data are available on local machines, there is an assured backup of the code at the repository level. The measure of security provided by Git is quite strong as it uses a cryptographic hash function called secure hash function to name and identify objects in its database. The biggest benefit of Git is its ease of usage in adding files, making changes, removing files, pulling files and staging changes.

57 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page