Master Git from Scratch: Essential Commands and Workflows
This comprehensive guide walks you through Git fundamentals, installation, repository creation, file states, branching, merging, resetting, reverting, and cherry‑picking, providing clear command examples and visual illustrations to help beginners confidently manage version control in their projects.
Basic Concepts
Git is a free, open‑source, distributed version‑control system that records file changes in a repository. Each file maintains a complete history, and the repository stores all commits.
Installation
sudo apt‑update
sudo apt‑get install git
git --versionWorkflow
Git uses three areas when managing a project: the working directory, the staging area (index), and the local repository. The local repository consists of three "trees":
Working Directory : holds the actual files.
Staging Area (Index) : a cache that temporarily stores your changes.
Local Repository : a directory where Git tracks all file modifications, allowing you to view history or revert to any previous version. HEAD points to the latest commit.
File States
Each file in Git can be in one of three states:
Modified : the file has changes but is not yet staged.
Staged : the file is added to the next commit (using git add).
Committed : the file is safely stored in the local repository (using git commit).
Creating a Repository and the First Commit
1. Initialize Repository
git initThis creates a .git directory in the current folder.
2. Add Files
Add new files such as url.c and README.md. Use git status to see untracked files.
3. Stage Files
git add <filename>
# Add all files
git add .
# Add all C files
git add *.c4. Commit Changes
git commit -m "Initial project version"
# Or open the editor for a message
git commitAfter committing, the changes are in HEAD but not yet in a remote repository.
5. Push to Remote
git push origin masterYou can replace master with any branch name. To connect a local repository to a remote server, add it first:
git remote add origin <server‑url>Branch Operations
View Branches
git branchWithout arguments, this lists local branches. The current branch is marked with an asterisk.
Create a Branch
git branch testA new branch named test appears alongside master.
Switch Branches
git checkout test
# Or create and switch in one step
git checkout -b testMerge Branches
After making changes on test and committing, merge back into master:
git checkout master
git merge testDelete a Branch
git branch -d test
# Force delete
git branch -D testPush a Branch
git push origin <branch>Replacing Local Changes (git checkout)
To discard local modifications to a file, use: git checkout -- <filename> This restores the file from HEAD while leaving staged changes untouched.
Resetting Commits (git reset)
Soft Reset
Moves HEAD to a specified commit without altering the working directory or index, allowing you to amend and recommit changes.
Hard Reset
Resets the working directory, index, and HEAD to a previous commit, discarding all subsequent changes.
Reverting Commits (git revert)
Creates a new commit that undoes the changes introduced by a specific commit, preserving history.
git revert 209485Cherry‑Picking Commits (git cherry-pick)
To apply a specific commit from another branch onto the current branch:
git branch cherry
git checkout cherry
# make some commits on cherry
git checkout master
git cherry-pick b98fc77This copies the selected commit onto master, creating a new commit that contains the same changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
