top of page

Git Basics

Updated: Jan 9

1 Introduction

Git is an open-source, distributed version control system which helps the developer's to keep a complete copy of the project’s history. It is used to tracked locally track the changes and also provided the developers to seamlessly integrated their offline work. In software engineering, Git is the backbone of version control systems. It is used in everything from managing small codebases for personal projects to complex, multi-repository systems in large organizations.

Git is a tool for tracking changes in your files and projects over time. It allows developers to:

  • Keep a complete history of code changes.

  • Collaborate efficiently, even in distributed teams.

  • Experiment with new features without impacting the main codebase.

  • Revert back to previous versions when needed.

2 Setup & Initialization

  • git init

    Command -

git init

What it does - This command is used to initialize the current project and add a new project to the repository.

  • git add .

Command -

 git add .

What it does - This command stages all changes in your working directory and make the git ready to add the changes done in the repository. It adds all modified, newly created, or deleted files to the staging area. Files must be in the staging area before you can commit them.

  • git commit

Command -

git commit -m "your_message"

What it does - This saves the staged changes into the repository, with a message describing the changes. It takes all files in the staging area and creates a snapshot (commit).

The -m "" flag allows us to include a short commit message in quotes for future reference  to understand what was being committed

3 Branching & Merging

  • git branch

Command-

git branch

What it does - It shows a list of all branches, with the current working branch highlighted.

Command

What it does

git branch

List all branches

git branch -n branch_name

Create's a new branch

git branch -d branch_name

Delete a branch

  • git merge

    Command-

git merge

What it does - It is used to combinine the  work from different branches.It  integrates changes from one branch into another.

Types of Merges:

  1. Fast-Forward Merge: It happens when the branch you’re merging can just “move forward” because there’s no divergence.

  2. Three-Way Merge - It happens when both branches have made changes and diverged. Git uses the latest common ancestor of both branches to merge them.

  • git checkout

    Command-

git checkout -b "branch_name"

What it does - The purpose of this command is to create a new branch and immediately switch to it.

git checkout is normally used to switch between branches.

-b: This option tells Git to create a new branch.

"branch_name": Replace this with the desired name for your new branch.

git checkout -b "branch_name"   is equivalent to running:

git branch branch_name
git checkout branch_name

4 Collaborating with Remote Repos

  • git push

Command-

git push

What it does - This command is used to push a local branch to a remote repository .

Command-

git push --set-upstream origin <branch_name>

What it does - To establish a tracking relationship between the local branch and the corresponding remote branch we can set the upstream. It ensures that our local branch is linked to the corresponding remote branch, so that Git knows from where to fetch updates or push new commits in the future.

  • git pull

Command-

git pull

What it does - This  command is used to update our local repository with changes from a remote repository. 

  • git status

This reflects the current state of your repository, including:

  • Which files have been changed.

  • Which files are staged for a commit.

  • Which files are not being tracked by Git.

git status

5 Stashing & Resetting

  • git reset

Command -

git reset --<type> HEAD~<count>

What it does-

This is generally used to undo the changes committed by the user. Reset can be done in 3 modes - soft, mixed and hard.

The reset can be done in 3 ways -

  • --soft Reset - It removes the last commit but keeps changes in the staging area.

    After this, your changes are still staged (ready for git commit).

	git reset --soft HEAD~1
  • --mixed reset - It is the default behaviour of the command and is followed if the user does not mention any type specifically. It removes the last commit and unstages the changes but keeps them in our working directory. After this, your changes are in your working directory (not staged).

	git reset --mixed HEAD~1
  • --hard reset- It removes the last commit and discards the changes completely.

	git reset --hard HEAD~1

Reset Type

Commit Removed?

Staged Changes?

Working Directory Changes?

--soft

Yes

Yes

Yes

--mixed

Yes

No

Yes

--hard

Yes

No

No


  • git stash

    Command-

git stash

What it does - It temporarily saves our changes without committing them. It holds/saves our uncommitted changes so we can return to a clean working directory.

stash pop- It restores the stashed changes and removes them from the stash list. This will:

  • Apply the most recent stash.

  • Remove it from the stash list.

	git stash pop

6 Debugging & Logs

  • Git log

Command-

git log

What it does- It displays a list of commits, their hashes, authors, and messages.

  • git reflog

Command-

git reflog

What it does- This command shows a log of all the HEAD movements, including resets, checkouts, and reverts. Mostly it is helpful when several users are working with the same repository and want to merge their code then they can use this command to check where they are currently pointing to.

A general workflow of the github repository
A general workflow of the github repository

7 References


Comments


bottom of page