USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Basic Git Operations

Lesson 10/32 | Study Time: 25 Min

Git is an essential distributed version control system widely used in software development for tracking and managing changes in source code.

It facilitates collaboration among multiple developers, enabling efficient workflows through features like branching, merging, and remote repositories.

Mastering basic Git operations is essential for managing your projects effectively, keeping track of changes, and coordinating updates across teams. 

Staging Changes (git add)

The staging area (index) is where Git collects changes before committing. The git add command moves file changes from the working directory to the staging area.


1. Stage a specific file:

text
git add filename


2. Stage all changed files recursively:

text
git add .


3. You can stage newly created, modified, or deleted files.


Staging lets you prepare a meaningful group of changes for your next commit.

Committing Changes (git commit)

Once changes are staged, commit them to your local repository with a descriptive message.


1. Commit with a message:

text
git commit -m "Add feature X"


2. Commit and open your configured editor to type a message:

text
git commit


3. Each commit records a snapshot of your project’s state and metadata like author and timestamp.

Checking Repository Status (git status)

The git status command provides information on the current state of your working directory and staging area:



Example output:

text
On branch main
Changes to be committed:
modified: file1.txt
Changes not staged for commit:
modified: file2.txt
Untracked files:
file3.txt

Viewing Commit History (git log)

git log shows the chronological list of commits in the current branch.


Common options include:


git log --oneline – brief one-line summaries.

git log -p – shows patch/diff for each commit.

git log --graph – visualizes branch structure.

Understanding the log helps explore project history and collaborate effectively.

Pushing Changes to Remote Repository (git push)

To share commits with others or back up your work, push changes to a remote repository.


1. First, add the remote repository if not done yet:

text
git remote add origin <remote-url>


2. Push changes to the remote branch (usually main or master):

text
git push origin main


3. Ensure you have permission and network access to the remote.


Pushing makes your changes available in collaborative platforms like GitHub, GitLab, or Bitbucket.

Pulling Changes from Remote (git pull)

Synchronize your local repository with remote updates:

text
git pull origin main


This fetches the latest changes and merges them locally, keeping your branch up-to-date.

Undoing Changes


  • Unstage a file:
text
git reset HEAD filename


  • Discard changes in working directory:
text
git checkout -- filename


These help correct mistakes or abort unwanted changes before committing.