Essential Git Operations for Local and Remote Repositories
Local Repository Management
A repository represents a directory under Git version control. A local repository refers to a folder on your computer's storage managed by Git.
Initialize a directory as a Git repository
git init
Check repository status
git status
Add files from working directory to staging area
git add .
git add <filename_or_directory>
Unstage files from staging area
git reset .
git reset <filename_or_directory>
Commit staged changes to repository history
git commit -m "Commit message describing changes"
List all branches
git branch
Create a new branch
git branch <new_branch_name>
Switch to a different branch
git checkout <branch_name>
Create and switch to a new branch simultaneously
git checkout -b <new_branch_name>
Merge one branch into current branch
git merge <source_branch>
Delete a branch forcefully
git branch -D <branch_name>
View commit history
git log
Navigate between commits
git reset --hard HEAD^ # Move to previous commit
git reset --hard <commit_hash> # Jump to specific commit using first 7 characters
Configure global user identity
git config --global user.name 'Your Name'
git config --global user.email 'your.email@example.com'
Remote Repossitory Integration
Remote repositories are directories hosted on servers and managed by Git. Popular platforms include GitHub, Gitee, and GitLab.
GitHub is Microsoft-owned with overseas servers, offering free personal repositories. Gitee provides domestic server access for better performance in China. GitLab, originally developed in Ukraine, offers enterprise self-hosting capabilities with customizable security.
Clone remote repository locally
git clone <repository_url>
git clone <repository_url> <custom_directory_name>
Push local changes to remote
git push
Conflict Resolution
Conflicts arise when attempting to create a new commit without properly basing it on the previous commit. Resolution requires synchronizing with the remote repository before pushing changes.
git pull
git push
Cross-Branch Workflow
Start by cloning the main branch locallly, then create local branches matching remote branch names for development work.
git push --set-upstream origin <branch_name>
For subsequent pushes:
git push
Alternatively, clone a spceific remote branch directly:
git clone -b <branch_name> <repository_url>
To synchronize a specific remote branch locally:
git pull <repository_url> <branch_name>
It's recommended to maintain identical branch names between local and remote repositories.
File Management
Git provides two special files for configuration:
.gitignore: Specifies files and paths to exclude from version control. Simply list the patterns or paths within this file.
.gitkeep: Empty file used to track otherwise empty directories in Git.