Git & Version Control

A comprehensive guide to Git, covering version control fundamentals, branching strategies, merging workflows, and best practices for collaborative software development.

Aug 15, 2025


Understanding Git

Git is a distributed version control system (VCS) created by Linus Torvalds (the same guy who made Linux) in 2005.

It's main uses is to track changes in your source code over time and enable multiple developers to work together on the same project without overwriting each other’s code.


What Does 'Version Control' Mean ?

Version control is like a timeline or history tracker for your project.


What you can do via Git ?


You can:

🕒 Revert back to an older version (rollback).

👥 See who changed what and when.

🌿 Work on new features without affecting the main code (using branches).

🧩 Merge your work back when it’s ready (via merge or pull request).


Notes

Upstream - It is the remote branch, that your local branch sync/follows when you push or pull.



Absolute Beginners Level - Daily used git commands

(Only for self Learners/Coders or for single user working on Git)


Basic/Simple Steps:


git clone clone_http_link_here


git pull origin main


Now, you can create new files & folders, write new lines of code or change/update or remove the lines of code.

It is the step, where u basically do all kind of stuffs inside your project directory(having .git folder).


To check the overall current status on your local project directory, use git status

git status

It tells :

  • which branch you are currently on.
  • whether the current working local branch is ahed or behind the remote.
  • whether the working branch is clean or has some changes or changes staged or not or changes to be committed or not.
  • gives list of tracked, modified, deleted & also the untrackedfiles and folders/directories

git add .


git commit -m 'any commit message here'


git push

  • push to the default upstream branch, if upstream has already been set - upstream is normally set in time of cloning repo.

OR

git push origin main

  • push this local main branch to the main branch on remote repository(i.e. origin) origin/main.

OR

git push -u origin main

  • used for the first push when upstream has not already/yet been set. And, also can be used later to change upstream.

Finally, it is always good to fresh pull remote main & merge it on your local main, before work on another feature.

Note: If you are only developer in this project, then, no need to git pull nor need to git fetch, git rebase or git merge

💡 Rule: git pull is fine if your local branch has no unpushed changes or no new commits yet.

git pull

  • To fetch the latest changes from the remote(origin) main branch and also merges them into your local branch.

OR

SYNTAX: git pull

git pull origin main

  • Fetch/bring all changes from main branch of remote (origin) & also merges to my current local branch.

OR

Optional: Alternative practice of git pull

💡 Better approach - Rule: git fetch & git rebase or git merge

If you to see pro level(safest) approach, then instead of hitting git pull.

U should hit the following commands:

SYNTAX: git fetch <remote_branch>[optional]

git fetch origin

It fetches/downloads all the branches from the remote named origin. It also doesn't touch/change the local branch.

&

Now, either u do merge or u do rebase.

But, you should first check out to your local branch (you should be on local feature branch that u have earlier created)

git merge -

  • Non-linear history (with merge commit). It doesn't maintain Linear log history

SYNTAX: git merge

git merge origin/main


OR


git rebase

  • It maintains Linear log history

SYNTAX: git rebase <local_branch>[optional]

git rebase origin/main

  • It rebase your current working local branch onto origin/main.


Corporate Level - git commands required in a software corporate world:


0. For checking status of local repository


git status


1. For viewing all existing branches on local repository


git branch


2. Always, pull recent update of remote repository i.e 'origin' before working on any new feature

(Or before creating a fresh new local branch)


git pull

OR

If ur are on main branch, and u want to pull recent update from remote reposity i.e "origin"

git pull origin main


3. To create & also switch to that new branch


Syntax: git checkout -b "branch_name"

Example: git checkout -b "aug_4_dhiraj"

NOTE: For switching to main/master branch

git checkout main

NOTE: For switching to any existing branch

git checkout branch_name


4. To stage/track all modifications/changes you have made

(i.e to track changes made in any files and folders)

git add .


5. To create a commit/save in your local repository

git commit -m "message"


6. To go/checkout to main branch and pull the latest update from the origin repository.

git checkout main

git pull origin main

Now, here u can see, all the files that had already been changed when u were working in your local branch.

If u see the same file that u were currently working in your local branch, then u can easily assume/guess, merge conflict will ocuur later on.

(Optional) To See the Difference in the code between main and your local branch.

git diff local_branch_name


(Also, from here u can clearly see the code difference, and can easily predict merge conflict will occur or not)

NOTE: If u figure out merge conflict will occur later, then, first u have to do, conflict resolving steps.



7. Else, If no merge conflict scenario/case, then u are now good to push.

push your local branch to the remote repository named "origin"

Syntax: git push -u origin local_branch_name (set upstream if it’s first push to remote)

Example: git push -u origin aug_04_dhiraj

OR

Syntax: git push origin local_branch_name (if it’s not first push to remote)

Example: git push origin aug_04_dhiraj


Now,

Before creating a PR(Pull Request) to merge into main, always make sure your feature branch is based on the latest remote main.

8. Making sure that my feature branch is based on latest(most recent) remote main.

First, checkout to local feature branch (if u are not inside local feature branch)

and, then hit the following commands, just before doing the later step(which is Pull Request & Merge):

git fetch origin


git rebase origin/main


*** Why doing this step ? ***

  • Ensures your feature branch includes all commits other developers may have added to main.

  • Makes PR merge conflict-free and keeps history clean/linear.


9. Create Pull Request (PR) and then ----> merge.

Basically this task in a coporate/company is handle by a Team Lead or Project Lead.

There are basically 2 ways to merge that pushed branch into remote repository.


UI apprach/way: using Git Web interface (Creating 'Merge/Pull Request')


Go to git web interface( i.e github / gitlab / bitbucket etc.)

On the top, u can see a notification/button of "Create merge request"

click on that 'Create merge/pull request' button

Fill up the information(like, Title, Description, Assignee(assigned to whom), reviewe etc.)

Finally, click on 'Create merge request' button which is available on button.

Now,

If u are 'Project Leed' OR 'Maintainer' of the project:

u can click on 'Merge' button and merge by yourself.


Else, if u are a 'Developer':

Then, ask a Team/Project Lead to check your code and to merge it.

Project lead will merge your code if it is well written.



Terminal approach/way: Using Git Commands (Command Line)


At first be inside the current working local branch (i.e <local_branch_name>):

To switch to local branch

git checkout <local_branch_name>


Now, fetch updates from remote main Before merging, always fetch the latest version of main (or master) so you don’t work on outdated data.

SYNTAX: git fetch

git fetch origin - (It fetches/download all the branches from the remote named origin. It also doesn't touch/change the local branch.)

OR

git fetch origin main - (It fetches only the main branch from the remote origin. It also doesn't touch/change the local branch.)

To view the difference/changes u have made.

git diff origin/main

Finally, To merge local working branch with origin remote repo or main repo.

But, you should first check out to your local branch (you should be on local feature branch that u have earlier created)

git merge origin/main



(Optional): Below code is only, if/when merge conflicts occurs

Only If there occurs conflicts in the code, then, "Merge Conflict" will arrive.

So, if conflicts is there, u should first resolve the conflicts.

(select the code which is to be kept & remove conflicting code).

Then, u should save(press, ctrl + s) all files, where merge conflicts has occured.

Again,

git add .

git commit -m "added feature"

Again, u can see, still conflict exists or not using

Syntax: git diff command

Example: git diff origin/main OR git diff --check (if no conflict has occured then al the text will be green)

Finally push the resolved local branch to remote origin repo.

git push -u origin local_branch_name (set upstream if it’s first push to remote)

OR

git push origin local_branch_name (if not first push to remote)

Again, u have to follow UI way to "Create Merge/Pull Request".

And, "Merge" .



10. After the code(new feature) is being merged to remote repository.

Then, switch to main/master branch:

git checkout main


Pull the latest changes from remote repository:

git pull origin main

OR

git pull


NOTE: if u are 100% sure, u code is being merged to remote repository,

then, only should delete your local branch", otherwise never delete local branch:

git branch -d branch_name



WALLAAHH !!