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.

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

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

  4. Create a local directory using the command mkdir testing

  5. Initialise the directory using the command git init

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

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

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

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

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

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

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

  13. 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

  2. CD [project path]

  3. To see status of all files

  4. git status

  5. To add file

  6. git add [filepath]

  7. To commit changes

  8. git commit -m"Message"

  9. create new branch

  10. git branch [branch name]

  11. switch to branch

  12. git checkout [branch name]

  13. To fetch all branches from remote origin

  14. git fetch --all

  15. To see all branches at your machine

  16. git branch -a

  17. to reset all local changes

  18. git reset --all

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

  20. git reset --hard head

  21. Create new branch from remote origin

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

  23. To pull a file

  24. git pull

  25. This command puts all your local changes first

  26. git pull --rebase

  27. force to switch branch

  28. git checkout -f staging

  29. To merge branches

  30. git merge [your local branch name]

  31. To restore deleted file

  32. git checkout head [file path]

  33. To push new local branch to server

  34. git push -u origin head

  35. Remove folder forcefully

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

  37. To revert changes made to your working copy

  38. git checkout [filepath]

  39. To make a copy of your changes

  40. git stash

  41. To get that copy back

  42. git stash pop

  43. replace file with origin branch

  44. 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.

30 views0 comments

Recent Posts

See All
bottom of page