Skybinary

View Categories

3. Basic Git Commands

1 min read

Basic Git Commands & Repository Management #

git init — Initialize a Repository #

This command is used to create a new, empty Git repository in your current project folder. It initializes the .git directory that stores all version control data.

Example:

git init

After running this, your folder becomes a Git repository and you can start tracking changes.


git clone — Copy a Remote Repository Locally #

Used to copy an existing repository (from GitHub or another source) to your local machine. This command downloads all files, commits, and branches.

Example:

git clone https://github.com/username/repository.git

This creates a new directory with the project’s files and Git history.


git status — Check Repository State #

Displays the current status of your working directory and staging area. It shows which files are modified, staged, or untracked.

Example:

git status

This helps you know what changes need to be committed or added to the staging area.


git add — Add Changes to Staging Area #

This command stages the changes you made so they can be committed. It prepares specific files (or all files) to be saved in the next snapshot.

Examples:

git add filename.txt

Adds a specific file.

git add .

Stages all modified and new files.


git commit — Save Snapshot #

Used to save your staged changes into the repository as a new commit. Each commit includes a message describing the changes made.

Example:

git commit -m "Added homepage design"

Each commit is a record of your project’s state, allowing you to go back to previous versions later.


git log — View Commit History #

Displays the list of all commits made in the repository, including commit IDs, author names, dates, and messages.

Example:

git log

To view a shorter summary:

git log --oneline

This helps track project progress and identify specific changes over time.


Undoing Changes #

git checkout — Switch Branches or Restore Files #

This command allows you to move between branches or restore files to a previous version.

Examples:

  • Switch to another branch: git checkout main
  • Restore a file to the last committed state: git checkout -- filename.txt

Note: In newer versions of Git, git switch (for branches) and git restore (for files) are preferred.


git reset — Undo Commits #

Used to undo commits or unstage changes by moving the branch pointer and optionally changing the working directory or staging area.

Examples:

  • Unstage a file: git reset filename.txt
  • Undo the last commit (keep changes): git reset --soft HEAD~1
  • Undo the last commit and discard changes: git reset --hard HEAD~1

Be careful with --hard because it permanently deletes changes.


git revert — Undo with a New Commit #

Unlike git reset, this command creates a new commit that reverses the changes of a specific commit. It’s the safest way to undo commits in a shared repository because it preserves history.

Example:

git revert <commit-id>

This adds a new commit that undoes the effects of the specified commit.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top