Local Git Repositories: Core Commands and Branching Workflows
Setting Up a Local Repository
Create a directory for the project and initialize a Git repository:
mkdir myproject
cd myproject
git init -b main
After initializasion, the .git folder appears in the project root, storing the repository metadata.
Fundamental Commands for Tracking Changes
Git models files in three areas: working tree, staging area, and repository. The workflow normally moves changes through these states.
Inspecting the Status
git status
The output shows which files are untracked, modified, or staged.
Staging Files
Move更改 from the working directory to the staging area:
git add <filename>
git add .
The first command adds a specific file; the second adds all modifications in the current directory.
Committing to the Repository
Save the staged snapshot into the repository history:
git commit -m "Describe the change"
Viewing the Commmit Log
A compact log with a graph can be 设置 through an alias or used directly:
git log --all --oneline --graph --decorate
If you have defined a git lg alias,06 the samekk same:
git lg
Discarding Changes - Reset
Move the HEAD and branch pointer to a previous commit:
git reset --hard <commit-hash>
Use git reflog to recover "lost" commits after a reset.
Ignoring Files
Create a .gitignore file in the repository root to exclude patterns from version control. Example:
echo "*.log" > .gitignore
echo "tmp/" >> .gitignore
A typical .gitignore can include:
# Build outputs
build/
dist/
# Environment files
.env
# OS generated files
.DS_Store
Thumbs.db
Working with Branches
Branches allow parallel lines of development. Understanding the basic branch operations is essential.
Listing Branches
git branch
The current branch is marked with an asterisk.
Creating a New Branch
git branch feature/new-login
This creates a branch pointing to the current commit without switching to it.
Switching Between Branches
To move to an existing branch:
git switch feature/new-login
Or use the older checkout:
git checkout feature/new-login
To create and directly switch in one step:
git switch -c feature/new-login
# or
git checkout -b feature/new-login
Merging Branches
Combine the work from one branch into another. Suppose we are on main and want to integrate the feature/new-login branch:
git merge feature/new-login
If the branches have diverged, a merge commit will be created unless a fast-forward is possible.
Deleting Branches
Remove a fully merged branch safely:
git branch -d feature/new-login
Force-delete an unmerged branch (use caution):
git branch -D feature/new-login
Handling Merge Conflicts
When two branches modify the same part of a file differently, Git marks the conflict during merge. Open the file and look for the conflict markers:
<<<<<<< HEAD
console.log("Hello from main");
=======
console.log("Hello from login branch");
>>>>>>> feature/new-login
Choose the desired code, remove the markers, stage the resolved file, and commit:
git add conflicted.js
git commit -m "Resolve merge conflict in conflicted.js"
Branching Strategy Overview
A flexible workflow often uses the following long-lived branches:
main(ormaster): production-ready state.develop: integration branch for ongoing development.
Short-lived branches include:
feature/*: new features branched fromdevelopand merged back after completion.hotfix/*: urgent fixes created frommain, then merged into bothmainanddevelop.release/*: preparation for a new production release.
Practical Example
Create a16 feature branch, make some changes, merge it back, and clean up.
# Starting on main
git switch -c feature/update-readme
echo "## Installation" >> README.md
git add README.md
git commit -m "Add installation section"
# Switch back to main
git switch main
# Merge the feature branch
git merge feature/update-readme
# Remove the feature branch
git branch -d feature/update-readme
# View the final history
git log --oneline --graph --all
This loop of creating a brench, working on it, and merging is16 central to nearly all Git workflows.