Fresh Opportunities Brighter Tomorrow!

Git for Beginners: Commit, Branch, Merge and Remote

If you are learning software development, DevOps, cloud computing or IT administration, Git is one of the most useful tools to learn.

Git is a distributed version control system that helps developers track changes to files, work on different versions of a project and collaborate with other people.

The good news is that you do not need to memorize hundreds of commands to get started. A small set of Git concepts explains most of the basic workflow: repository, commit, branch, merge and remote.

Simple definition:
Git records changes to files over time so that developers can track, compare and collaborate on different versions of a project.

What Is Git?

Git is a version control system. It keeps a history of changes made to a project and allows developers to work with different versions of their files.

Without version control, a developer might create files such as:

project-final.zip
project-final-v2.zip
project-final-new.zip
project-final-new2.zip

Git provides a much more structured way to maintain the history of a project.

The Basic Git Workflow

Git Workflow
Working Files
Edit
Staging
git add
Repository
git commit
Remote
git push

This workflow is worth understanding before memorizing individual commands.

What Is a Git Repository?

A Git repository is a project that is being tracked by Git. It contains the files of the project and Git's information about their history.

To create a new Git repository in an existing directory:

git init

Git creates the necessary repository metadata inside the project directory.

You can then check the current state of the repository with:

git status

Configure Git

Before creating commits, it is useful to configure the name and email associated with your Git commits.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

These settings identify the author information stored in your commits.

What Is a Git Commit?

A commit records a set of changes in the Git repository's history.

A simple workflow is:

git add .
git commit -m "Add homepage"

The first command stages changes, while the second creates the commit.

Think of a commit as a checkpoint.
It gives the project a recorded point in its history that you can inspect later.

What Is the Git Staging Area?

Git provides a staging area between your working files and the repository.

This lets you choose which changes should be included in the next commit.

For example:

git add index.html

Only the selected change is staged.

You can then inspect the repository:

git status

What Is a Git Branch?

A branch provides a separate line of development within a Git repository.

Branches allow developers to work on features or fixes without immediately changing the main development line.

Simple Branch Concept
main ──●────●────●────────●
                  ↖
feature ─────────●────●────●

A new branch can be created with:

git branch feature-login

You can switch to it using:

git switch feature-login

A convenient command for creating and switching to a new branch is:

git switch -c feature-login

What Is Git Merge?

Once work on a branch is complete, the changes may need to be incorporated into another branch.

This is where git merge is used.

For example, if you want to merge the feature branch into the main branch:

git switch main
git merge feature-login

Git attempts to combine the histories of the two branches.

If Git cannot automatically combine particular changes, it can report a merge conflict.

What Is a Merge Conflict?

A merge conflict can occur when Git cannot automatically determine how two sets of changes should be combined.

For example, two developers may modify the same section of a file in different ways.

Git will mark the conflicting area and require a developer to decide which changes should remain.

After resolving the file, the normal process is to stage the resolved file and complete the merge:

git add FILE_NAME
git commit

What Is a Git Remote?

A remote repository is another copy of a Git repository that can be accessed over a network.

Remote repositories are commonly used for collaboration and centralized project hosting.

You can see configured remotes using:

git remote -v

A remote can be added with:

git remote add origin REMOTE_REPOSITORY_URL

What Does git push Do?

git push sends commits from your local repository to a configured remote repository.

A common example is:

git push origin main

This pushes the local main branch to the remote named origin.

What Does git pull Do?

git pull is commonly used to retrieve changes from a remote repository and integrate them into the current local branch.

git pull

Understanding the difference between local and remote repositories is important when working on a team.

What Is git clone?

If a project already exists in a remote repository, you can create a local copy using git clone.

git clone REMOTE_REPOSITORY_URL

Cloning creates a local working copy of the repository.

Viewing Git History

Git stores the history of commits, which you can inspect with:

git log

A compact version is:

git log --oneline

This is useful when you want a quick overview of the project's commit history.

Viewing Changes

Before committing changes, you can inspect differences using:

git diff

This can help you review your work before adding it to the staging area.

A Complete Git Workflow Example

Imagine you are working on a website and want to add a login feature.

Step 1: Create a branch

git switch -c feature-login

Step 2: Modify your files

Make the required changes to the application.

Step 3: Check the changes

git status
git diff

Step 4: Stage the changes

git add .

Step 5: Create a commit

git commit -m "Add login feature"

Step 6: Push the branch

git push -u origin feature-login

The exact collaboration workflow can vary between teams and hosting platforms, but these commands illustrate the core Git concepts.

Essential Git Commands

Command Purpose
git init Create a Git repository
git status Show repository status
git add Stage changes
git commit Record changes
git branch Manage branches
git switch Switch branches
git merge Combine branch histories
git clone Create a local copy of a repository
git pull Retrieve and integrate remote changes
git push Send local commits to a remote
git log View commit history

Why Git Is Important for DevOps

Git is closely connected with modern DevOps workflows because source code changes often trigger automated build, testing and deployment processes.

A simplified DevOps workflow might look like:

Developer

Git Commit

Remote Repository

CI Pipeline

Build & Test

Deployment

This is why Git is one of the first tools many people learn when beginning a DevOps career.

Git Interview Questions for Beginners

1. What is Git?

Git is a distributed version control system used to track changes to files and support collaboration.

2. What is a repository?

A repository is a project managed by Git that contains files and their version history.

3. What is a commit?

A commit records a set of changes in the repository history.

4. What is a branch?

A branch is a separate line of development within a Git repository.

5. What is git merge?

Git merge combines the histories of two branches.

6. What is a remote repository?

A remote repository is another copy of a Git repository that can be accessed over a network.

7. What is the difference between git pull and git push?

git pull retrieves changes from a remote repository and integrates them into the current local branch, while git push sends local commits to a remote repository.

Frequently Asked Questions

Is Git the same as GitHub?

No. Git is the version control system. GitHub is a platform that provides hosting and collaboration features for Git repositories.

Do I need to learn every Git command?

No. Beginners can accomplish many common tasks using a relatively small set of commands such as status, add, commit, branch, switch, merge, pull and push.

Can Git be used without a remote repository?

Yes. Git can be used locally to track project history without connecting to a remote repository.

Why are branches useful?

Branches allow developers to work on separate features or fixes without immediately changing another branch.

Practical Git Exercise

Try this exercise on a test directory.

Step 1: Create a directory

mkdir git-learning
cd git-learning

Step 2: Initialize Git

git init

Step 3: Create a file

echo "Hello Git" > README.txt

Step 4: Check the status

git status

Step 5: Stage the file

git add README.txt

Step 6: Create the first commit

git commit -m "Add README"

Step 7: Create a branch

git switch -c feature-example

Step 8: Make another change

echo "Learning Git" >> README.txt

Step 9: Commit the change

git add README.txt
git commit -m "Update README"

Step 10: View the history

git log --oneline

This small exercise demonstrates the basic Git workflow without requiring a remote hosting service.

Key Takeaways

  • Git is a distributed version control system.
  • A Git repository stores project files and their history.
  • A commit records changes in the repository history.
  • The staging area allows you to choose changes for the next commit.
  • Branches provide separate lines of development.
  • Merge combines branch histories.
  • Remote repositories allow Git projects to be shared and collaborated on.
  • git pull retrieves and integrates remote changes.
  • git push sends local commits to a remote repository.
  • Git is an important foundation for modern DevOps and CI/CD workflows.

Related Topics

  • CI/CD Explained: From Code Commit to Deployment
  • Docker Containers Explained
  • Kubernetes Explained
  • Linux Commands for Beginners
  • DevOps Fundamentals
  • Version Control Best Practices
  • Git Branching Strategies
Freshers Nation Editorial Note: This tutorial is intended as an educational introduction to Git for students, freshers and IT professionals building software development and DevOps fundamentals. Git workflows can vary between teams and organizations.

No comments:

Post a Comment

Stay Connected

Get the latest job opportunities and career updates.

👍 Like us on
facebook
Subscribe to Job Updates

Get the latest jobs directly from Freshers Nation.

Subscribe via RSS → No spam. Unsubscribe anytime.