Skip to content

02. Basic Commands

Overview

The following is a list of basic Git commands with explanations for each. These commands are often used when working with Git and are a good starting point for understanding the basics of Git.

Commands

# usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
#            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
#            [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
#            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
#            [--super-prefix=<path>] [--config-env=<name>=<envvar>]
#            <command> [<args>]

Git Command Introduction

# Git Configuration
git config --global user.name "[nickname]"        # Set your name for commits and tags
git config --global user.email "[email]@[domain]" # Set your email for commits and tags
git config --global color.ui auto                # Enable colored Git output
# Starting a Project
git init [project_name]     # Create a new local repository
git clone [project_url]     # Download a project and its history from a remote repository

Day-to-Day Work

git status                  # Show the status of your working directory
git add [file]              # Add file(s) to the staging area
git diff [file]             # Show unstaged changes
git diff --staged [file]    # Show staged changes
git checkout -- [file]      # Discard local changes (irreversible)
git reset [file]            # Unstage a file without discarding changes
git commit                  # Commit staged changes with a message

Branching Model

git branch [-a]             # List local branches; use -a to show remote branches too
git branch [branch_name]    # Create a new branch
git checkout [-b] [branch]  # Switch to (or create with -b) a branch
git merge [branch_name]     # Merge specified branch into current branch
git branch -d [branch_name] # Delete a branch (use -D to force delete)

File Management & Stashing

git rm [file]               # Remove file from working directory and staging area
git stash                   # Temporarily store changes for later use
git stash pop               # Reapply stashed changes and clear the stash
git stash drop              # Delete a specific stash

Reviewing Work

git log [-n count]          # Show commit history; limit with -n
git log --oneline --graph --decorate  # Show condensed graph view of commits
git log ref..               # Show commits not merged into ref
git log ..ref               # Show commits in ref not in current branch
git reflog                  # Show local operation history (checkouts, commits, etc.)

Tagging Commits

git tag                     # List all tags
git tag [name] [commit_sha] # Create a lightweight tag
git tag -a [name] [commit_sha] # Create an annotated tag
git tag -d [name]           # Delete a local tag

Reverting & Synchronizing

git reset [--hard] [target_ref]  # Move branch to target; discard changes with --hard
git revert [commit_sha]          # Create a commit that reverses a specific commit
git fetch [remote]               # Download objects but don"t merge
git fetch --prune [remote]       # Remove stale remote-tracking references
git pull [remote]                # Fetch and merge changes
git push [--tags] [remote]       # Push commits (and optionally tags)
git push -u [remote] [branch]    # Push branch and set upstream

Ignoring Files

Use .gitignore in your project root to exclude specific files or directories from version control.

# Example .gitignore
/logs/*
!logs/.gitkeep
/tmp
*.swp

Usefule script

For someday, we need some refresh for our Git element

git branch --remote
git fetch --prune
git  branch -D <non-used-branch-name>

Check config

cat .git/config
Check remote URL

Stale Branch

ENVY@bjn-thuyet MINGW64 /d/PROJECT/thuyetbao/REPOSITORY/hydrus (master)
$ gh release create v0.20.81
? Title (optional) (v0.20.81) [0.20.81] Pipelines deployment

? Title (optional) [0.20.81] Pipelines deployment
? Release notes Write using generated notes as template
? Is this a prerelease? Yes
? Submit? Publish release
https://github.com/thuyetbao/hydrus/releases/tag/v0.20.81
(venv)
ENVY@bjn-thuyet MINGW64 /d/PROJECT/thuyetbao/REPOSITORY/hydrus (master)
$ git fetch --prune
From https://github.com/thuyetbao/hydrus
 - [deleted]         (none)     -> origin/feat/application
 - [deleted]         (none)     -> origin/fix/ref-env-var-location
 - [deleted]         (none)     -> origin/fix/unmarshalling-cicd
 * [new tag]         v0.20.81   -> v0.20.81
(venv)
ENVY@bjn-thuyet MINGW64 /d/PROJECT/thuyetbao/REPOSITORY/hydrus (master)
$ git branch -D feat/application fix/ref-env-var-location fix/unmarshalling-cicd