Basic Git commands: Every developer should know
Git

Basic Git commands: Every developer should know

RK
Ramesh KannanSenior .NET Full Stack Developer
#git

Originally published on arasu-rrk.com.

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Here are some basic Git commands you need to know.

Clone the Repository

git clone https://github.com/OWNER/REPOSITORY.git

By default, Git checks out the repository's default branch. To start on a different branch while cloning, use -b:

git clone -b development https://github.com/OWNER/REPOSITORY.git

Create and Switch Branches

After cloning, use git switch to change branches in the local repository:

Create a new branch and switch to it:

git switch --create {branch_name}

To switch to an existing local branch:

git switch {branch_name}

To switch to a branch that exists on the remote but not locally:

git fetch origin
git switch --create {branch_name} --track origin/{branch_name}

Fetch Changes from Remote

Fetch updates from the origin remote without changing your current branch or working files:

git fetch origin

The fetched branches are available as remote-tracking branches, such as origin/main. Fetching does not merge changes into your current branch.

Pull Updates from Remote

Fetch and integrate updates from the current branch's configured upstream:

git pull

To fetch and merge a specific remote branch into your current branch:

git fetch origin
git merge origin/{branch_name}

For example, git merge origin/development merges the fetched development branch into the branch you currently have checked out.

Check the Status of Local Changes

List the files you have changed and need to stage or commit:

git status

Stage Your Files

To stage your files, use the add command.

Syntax
git add {file_path}
Examples
  1. Add a specific file: git add test/file.md
  2. Add an entire folder: git add {folder_name}/
  3. Add files by extension: git add '*.css'
  4. Stage all changes in the repository: git add -A

Review what will be committed with git status or git diff --cached.

Commit Changes

To commit the staged changes:

Syntax
git commit -m "{message}"
Example
git commit -m "Test commit"

NOTE:

  • Changes will not be pushed to the remote unless you explicitly push them.

Push Changes to Remote

Push the current branch to its configured upstream:

git push

For a new branch that has no upstream yet:

git push --set-upstream origin {branch_name}

Discard Local Changes

Discard unstaged changes to a tracked file:

git restore {file_path}

Unstage a file without discarding its edits:

git restore --staged {file_path}

Check git status first. git restore {file_path} discards unstaged tracked edits, while git restore --staged {file_path} preserves edits and only unstages the file. To preview untracked files and directories that Git would delete:

git clean --dry-run -d

Only after reviewing the preview, delete those untracked files and directories with:

git clean -f -d

git clean permanently deletes untracked files; it does not move them to the trash. Check its preview carefully before running it.

References