Codeworx Logo

Git Foundations: A Guide to Version Control

Mandar

Mandar J

Last updated:


Git Foundations: A Guide to Version Control

What is git and the need for it?

Git is a distributed version control system that helps developers track changes, collaborate on projects, and manage code efficiently. Think of it as a record-keeping tool for your code, allowing you to save versions, work on different features simultaneously, and merge changes from multiple contributors without chaos.

The need for git arises from the challenges developers face when working on software projects. These challenges include:

Keeping track of changes made by multiple developers

Undoing changes when something goes wrong

Collaborating with others without overwriting their work

Storing code securely and reliably

Git Foundations:

Branches:

Branches allow developers to work on different features or versions of a project independently. It's like having multiple timelines where you can experiment without affecting the main codebase.

Git Explainer

Commits:

A commit is a snapshot of your code at a particular point in time. It captures changes made to files and helps in tracking progress and reviewing code history.

Git Explainer

Tags:

Tags are markers for specific points in your code's history, often used to denote versions or releases. They make it easier to identify and reference important milestones.

Git Explainer

Head:

HEAD is a pointer to the latest commit in the currently checked-out branch. It helps you know where you are in the commit history.

Stages:

Git has a three-stage workflow: working directory, staging area, and repository. Files move from the working directory to the staging area and then to the repository, allowing developers to control what changes are committed.

Setting up git:

Before using git, you need to set it up on your computer. You can download and install git from git-scm.com.

Basic Git commands:

git init

Initializes the current folder as a git repository.

git init
Initialized empty Git repository in C:/Users/user/Demo/.git/
  • git add <files/folders>

Adds files or folders to the staging area, preparing them for commit.

I have created an index.html file for the demo.

//index.html
<!DOCTYPE html>
<html lang="en">
  <body>
    <h1>HELLO WORLD!!</h1>
  </body>
</html>
C:\\Users\\user\\Demo> git add index.html

git commit

Commits the staged changes to the git repository with a message describing the changes.

git commit -m "initial commit"
[master (root-commit) 45a5028] initial commit
 1 file changed, 11 insertions(+)
 create mode 100644 index.html

git status

Shows the current status of the folder, including changes and staged files.

git status
On branch master
nothing to commit, working tree clean


git log --oneline

Displays a brief log of commits with their corresponding commit IDs(--oneline will display it in a single line)

I will now make a change in the file and commit it to check the log.

//index.html
<!DOCTYPE html>
<html lang="en">
  <body>
    <h1>HELLO WORLD!!</h1>
    <h2>Hi</h2> //add and commit the change then use git log
  </body>
</html>
git log --oneline
ac17ef1 (HEAD -> master) second commit
45a5028 initial commit


git checkout <commit> <file>

Checks out a file from an older commit, restoring it to a previous state.

//second commit with blogId ac17ef1
<body>
    <h1>HELLO WORLD!!</h1>
    <h2>Hi</h2>
  </body>


//restore index.html from first commit using commit ID
git checkout 45a5028 index.html
Updated 1 path from 08d4c84


//after checkout
<body>
    <h1>HELLO WORLD!!</h1>
  </body>

git reset <file>

Unstages a staged file, leaving the working directory unchanged.

git reset

Resets the staging area to the last commit without changing the working directory.

Online Git repositories:

Several online services host git repositories, making it easier to collaborate and share code:

Github

Bitbucket

Make your first online repository:

Let’s make an online repo on github.com and add our committed project.

Go to repositories tab → New → Give your repo name → click publish.

Github: Create new repository

You will see the below page

github new repo

Since we have already initialized, added and committed our project, we will do the second part i.e. push an existing repo.

Commands:

git remote add origin <repository URL>

Adds the remote online repository to your local git configuration.

git remote add origin <https://github.com/MandarJunnarkar/demo.git>

git branch <branch-name>

Create a new branch in a Git repository with the specified.

 git branch -M main // -M force renames the current branch to given name

git push -u origin <branch name>

Pushes the local git repository to the origin (online repository) on the master branch.

git push -u origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 641 bytes | 641.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To <https://github.com/MandarJunnarkar/demo.git>
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

That’s it! You have successfully created your first online repository. Refresh the repo page and check if your project.

github commit history

When you want to push changes, just use add, commit and push commands.

Here's a Git cheat sheet for you:

# Clone Repository
git clone <repository_url>


# Stage Changes
git add <file(s)>


# Commit Changes
git commit -m "Commit message"


# Push Changes
git push


# Force Push (caution)
git push --force


# Reset to Last Commit
git reset --hard


# Create New Branch
git branch <branch_name>


# Switch Branch
git checkout <branch_name>


# Merge Branch
git merge <branch_name>


# Rebase onto Base Branch (caution)
git rebase <base_branch>


# View Status
git status


# View Commit History
git log


# Undo Last Commit (caution)
git reset --soft HEAD^


# Discard Changes
git restore <file(s)>


# Retrieve Lost Commits
git reflog


# Interactive Rebase
git rebase --interactive HEAD~3


# Pull from Remote
git pull <remote_name> <branch_name>


# Fetch from Remote
git fetch <remote_name>

Conclusion:

Git is an essential tool for developers, offering a structured and collaborative approach to managing code. Whether you're working on a solo project or a team-based endeavor, git simplifies version control and streamlines development workflows. So, did you git it?

Remember, mastering git takes practice, but the benefits it offers in terms of code management and collaboration are well worth the effort. Happy coding!

Related Blogs

CodeWorx

© Copyright 2024 - Codeworx