Essential Git Commands: A Practical Guide
This article provides a step‑by‑step walkthrough of common Git operations—including repository initialization, staging, committing, viewing status, diffing, logging, reverting, remote configuration, branching, tagging, and .gitignore setup—illustrated with command examples and screenshots.
Repository initialization and workspace
Initialize an empty directory with git init, creating a .git directory that stores the repository metadata, the staging area, and the default branch master. The working directory is the filesystem location where files are edited.
Add files to the staging area with git add <file>, e.g., git add readme.txt.
Configuration and committing
Set user identity before committing:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Record a snapshot with git commit -m "message". The commit is stored in the repository and referenced by a SHA‑1 hash.
Inspecting repository state
git status– shows staged, modified, and untracked files. git diff – displays line‑by‑line differences between working tree and index or between commits. git log – lists commit history with hashes, authors, dates, and messages.
Reverting and navigating history
HEAD points to the current commit. Use HEAD^ for the parent, HEAD^^ for the grand‑parent, or HEAD~n for n ancestors. Checkout a previous commit by its abbreviated hash, e.g., git checkout 6224937, to view the repository at that point.
Remote repository interaction
Generate an SSH key: ssh-keygen -t rsa -C "[email protected]" and add the public key on GitHub.
Link the local repository to a remote named origin:
git remote add origin git@server-name:path/repo-name.gitPush the initial branch: git push -u origin master. Subsequent pushes use git push origin master.
Clone an existing remote repository:
git clone [email protected]:916812579/learn.gitBranch management
Create and switch to a new branch in one command: git checkout -b dev This is equivalent to:
git branch dev
git checkout devList branches with git branch. Merge another branch into the current one with git merge <branch>. Delete a branch with git branch -d dev or force‑delete with git branch -D <name>. View remote information with git remote -v.
Tag management
Create a lightweight tag on the latest commit: git tag v1.0.
List tags: git tag.
Annotate a specific commit: git tag v0.9 6224937.
Show tag details: git show v1.0.
Delete a local tag: git tag -d v1.0.
Delete a remote tag: git push origin :refs/tags/v0.9.
Push a tag to the remote: git push origin v1.0 or push all tags: git push origin --tags.
Ignore patterns and configuration scope
List files to exclude from commits in a .gitignore file.
Define command aliases, e.g., git config --global alias.st status. Settings with --global are stored in the user‑wide ~/.gitconfig; repository‑specific settings reside in .git/config.
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.
ZhiKe AI
We dissect AI-era technologies, tools, and trends with a hardcore perspective. Focused on large models, agents, MCP, function calling, and hands‑on AI development. No fluff, no hype—only actionable insights, source code, and practical ideas. Get a daily dose of intelligence to simplify tech and make efficiency tangible.
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.
