5 Years Using Git Yet Still Missing the Basics? Master Core Git Operations
This article walks through Git installation, essential configuration, the three internal areas, object storage, branch mechanics, common commands, best‑practice workflows, and how Git triggers CI/CD pipelines, providing a complete understanding for developers who think they already know Git.
1. Installing and Configuring Git
Installation commands for common Linux distributions.
sudo apt update
sudo apt install git sudo yum install git
# or on CentOS 8+
sudo dnf install gitSet the global user name and email, which are recorded with every commit.
git config --global user.name "张三"
git config --global user.email "[email protected]"Example ~/.gitconfig that configures editor, file‑mode handling, performance tweaks, line‑ending conversion, color output, useful aliases, and credential helpers.
# ~/.gitconfig
[user]
name = 张三
email = [email protected]
[core]
editor = vim
filemode = false
preloadindex = true
fscache = true
autocrlf = true
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
[color "status"]
added = yellow
changed = green
untracked = cyan
[alias]
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
st = status
df = diff
dfc = diff --cached
br = branch -a
co = checkout
cb = checkout -b
unstage = reset HEAD --
discard = checkout -- .
last = log -1 HEAD
show = show --stat
[pull]
rebase = true
[push]
default = simple
[merge]
ff = only
[credential]
helper = store2. Core Working Model
Git operates on three areas.
Working Directory : files as they appear on the filesystem.
Staging Area (Index) : records which changes will be included in the next commit.
Repository : permanent storage of all commits.
Analogy: a room is the working directory, a box at the door is the staging area, and a moving truck is the repository. git add moves files to the box; git commit loads the box onto the truck.
Object storage – everything is a hash
Each object is identified by a 40‑character SHA‑1 hash. Four object types exist:
Blob : file contents.
Tree : directory structure and filenames.
Commit : metadata (author, timestamp, message) plus pointers to parent commit(s) and the root tree.
Tag : human‑readable label, often used for releases.
Branch representation
A branch is a plain text file under .git/refs/heads/ containing a single 40‑character hash that points to the tip commit of that branch.
3. Typical Development Workflow
Recommended sequence instead of a one‑liner git add . && git commit -m "msg" && git push:
Inspect changes:
git status
git diffSelectively add to the index:
# single file
git add filename
# whole directory
git add directory/
# interactive, choose hunks
git add -pCommit with a descriptive message:
git commit -m "Brief description of the logical change"Push to the remote repository:
git pushThe interactive git add -p helps avoid committing unintended files.
Commit‑history hygiene
One logical change per commit.
Each commit builds and runs.
Do not commit half‑finished work; use git stash if needed.
Before pushing, rewrite local history with git rebase -i to squash or reorder commits.
Branching strategies
Git Flow : suited for teams with fixed release cycles.
GitHub Flow : simpler, fits continuous deployment.
Selection depends on the team’s workflow requirements.
4. Git in CI/CD Pipelines
Push events trigger pipelines on platforms such as GitHub, GitLab, or Gitee. Configuration files ( .github/workflows/, .gitlab-ci.yml) define the steps.
Example: GitHub Actions for a Node.js project
# .github/workflows/ci.yml
name: CI/CD Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Lint code
run: npm run lint
- name: Run unit tests
run: npm test
- name: Build project
run: npm run build
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Deploy to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/myapp
git pull origin main
npm ci --production
pm2 restart myappThe workflow runs tests on every push or pull request to main. If tests succeed and the push targets main, the deployment job logs into the server, pulls the latest code, installs production dependencies, and restarts the application with pm2.
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.
AI Agent Super App
AI agent applications, installation, large-model testing, computer fundamentals, IT operations and maintenance exchange, network technology exchange, Linux learning
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.
