How to Use Git: Installation, Configuration, Core Commands, and Advanced Workflows
This article provides a comprehensive guide to installing Git, generating SSH keys, configuring user information, understanding Git's architecture and workflow, and mastering essential and advanced commands such as add, commit, pull, fetch, branch, rebase, cherry‑pick, stash, revert, and alias configuration for efficient development.
When a new employee cannot pull code with Git and gets fired, it highlights how essential Git is for both front‑end and back‑end development. This guide walks you through setting up a local Git environment and pulling code in four clear steps.
Step 1: Install Git
Download Git from https://git-scm.com/downloads and choose the version matching your operating system.
Step 2: Generate an SSH key
Open a terminal and run ssh-keygen -t rsa -C "[email protected]" . After successful execution, navigate to ~/.ssh and copy the contents of id_rsa.pub .
Step 3: Add the public key to your remote repository (e.g., GitHub)
In GitHub, go to settings → SSH and GPG keys , then use cat ~/.ssh/id_rsa.pub to display the key and copy it into the add ssh key field.
Step 4: Configure global username and email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"After completing these steps, you can pull code via SSH without repeatedly entering credentials.
Git Overview
Git is an open‑source distributed version‑control system created by Linus Torvalds in 2005 after the BitKeeper service was withdrawn. It efficiently handles projects of any size and replaces manual merging of Linux kernel sources.
Git Work Areas and Workflow
Git consists of four areas:
Workspace : the directory where you edit files.
Index (Staging Area) : files added with git add are staged here.
Repository (Local) : committed changes are stored here via git commit .
Remote : a server that hosts the repository; you push changes with git push .
The typical workflow is:
Modify files in the workspace.
Stage changes with git add .
Commit staged changes with git commit .
Push commits to the remote repository with git push .
Basic Git Commands
git add
# Add a specific file
git add file.txt
# Add all modified files
git add .git commit
# Open editor for commit message
git commit
# Commit with a message
git commit -m "your message"
# Add and commit in one step
git commit -am
# Amend the most recent commit (creates a new hash)
git commit --amendgit pull
# Pull and merge (default)
git pull
:
# Pull using rebase
git pull --rebase
:git fetch
# Fetch a specific branch
git fetch
# Fetch all branches
git fetch --allgit branch
# Create a new branch (no checkout)
git branch
# List local branches
git branch
# List remote branches
git branch -r
# List all branches
git branch -a
# Delete a local branch
git branch -D
# Rename a branch
git branch -mUsing Git in Real‑World Scenarios
git rebase for a cleaner history
Rebase rewrites commit history by applying changes from one branch onto another, producing a linear history. It is similar to git merge but does not create a merge commit.
Typical workflow:
Switch to the feature branch.
Run git rebase master .
Resolve any conflicts, then git add and git rebase --continue .
Use git rebase --skip to skip a problematic commit.
Interactive rebase
To squash multiple commits into one, run:
git rebase -iIn the editor, change pick to s (squash) for the commits you want to combine, keep at least one pick , then save and exit. Edit the resulting commit message as needed.
git merge vs. git rebase
git merge creates a merge commit when the branches diverge, while git rebase rewrites history to produce a straight line. Merge resolves conflicts once; rebase may require conflict resolution at each commit.
git cherry-pick
Cherry‑pick copies a specific commit from another branch without merging the whole branch:
git cherry-pick
# To pick a range (left‑open, right‑closed)
git cherry-pick
...git revert
Revert creates a new commit that undoes the changes introduced by a previous commit, preserving history. Use git revert for normal commits or git revert -m 1 for merge commits.
git stash
When you need to switch branches but have uncommitted changes, stash them:
git stash # Save changes
git stash save "msg" # Save with a message
git stash list # List stashes
git stash apply stash@{0}# Apply without removing
git stash pop # Apply and remove
git stash clear # Delete all stashesUndoing changes in different areas
To discard modifications in the workspace:
git checkout --To unstage files that have been added:
git reset
# Reset all staged files
git resetConfiguring Git aliases
Aliases shorten frequently used commands:
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
# Example alias file snippet
[alias]
st = status -sb
co = checkout
br = branch
mg = merge
ci = commit
ds = diff --staged
dt = difftool
mt = mergetool
last = log -1 HEAD
ls = log --pretty=format:"%C(yellow)%h %C(blue)%ad %C(red)%d %C(reset)%s %C(green)[%cn]" --decorate --date=short
hist = log --pretty=format:"%C(yellow)%h %C(red)%d %C(reset)%s %C(green)[%an] %C(blue)%ad" --topo-order --graph --date=short
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commitAfter setting these aliases, commands like git lg provide a concise, colorful history view.
Conclusion
This tutorial covered Git installation, basic usage, and many high‑frequency commands useful for front‑end, back‑end, and full‑stack developers. Mastering these techniques will make collaboration smoother and your workflow more efficient.
References
Ruanyifeng's Git tutorial: https://www.ruanyifeng.com/blog/2014/06/git_remote.html
Git merge vs. rebase article: https://juejin.cn/post/6844903603694469134#heading-3
政采云技术
ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.
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.