Fundamentals 5 min read

Master Git: Essential Commands and Workflow for Efficient Version Control

This guide walks you through essential Git commands, daily workflows, branching strategies, and remote repository handling, providing clear examples and code snippets to help you manage version control effectively in any project.

21CTO
21CTO
21CTO
Master Git: Essential Commands and Workflow for Efficient Version Control

Key Points – Using Git

git status – shows current state, local changes, remote commits, and untracked files.

git diff – view specific local changes; use --name-only to list changed files.

git add – stage untracked files.

git commit – create a new commit with -m for a meaningful message.

git push – send changes to a remote repository such as GitLab or GitHub.

Basic Workflow – Daily Git Usage with Tags

1. Navigate to the project directory and initialize the repository (git init). Then check status, add all files, commit, and show the commit.

git init
git status
git add --all
git status
git commit -m "meaningful initial commit message"
git show

2. Modify files and commit regularly.

git diff
git commit -a -m "Another commit message. -a performs the add step for you"
git status
git log --graph --pretty=oneline --abbrev-commit

3. After several commits, squash them into a single meaningful commit.

git log --graph --pretty=oneline --abbrev-commit
git reset --soft HEAD~3
git diff --cached
git commit -a -m "Better commit message for last 3 commits"

4. Finally, remove unnecessary files.

git status
git diff --cached
git add -u
git commit -m "Another commit message. -u adds updates, including deleted files"
git status
git log --graph --pretty=oneline --abbrev-commit
git push origin master

Basic Branching

git branch --all          # list all local and remote branches
git checkout <branch>      # switch to existing branch
git checkout -b <branch> master  # create a branch from master and switch to it
git checkout master && git merge <branch>  # merge branch into master

Useful Tags

git reset HEAD -- # revert to the last known commit and cancel other changes

git add -u # add only updated, previously tracked files

git log --graph --pretty=oneline --abbrev-commit # visual branch history

Working with Remote Repositories

git fetch --all # download all commits, files, and refs from remotes

git pull --rebase # rebase onto remote branch without creating a merge commit

git stash # save uncommitted changes for later use

git commit -m "commit message" # follow the project's commit message format

git push origin # push the current branch to the remote

git checkout -b # create and switch to a new branch

git push origin master # push changes to the master branch on the remote

Getting Help

git <cmd> -h # quick reference for a Git command

git <cmd> --help # detailed manual page for a Git command

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitVersion Controlgit-workflowbranchinggit commandsremote repository
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.