Core Identity and Global Configuration
Establishing identity is the first step in version control. Use the following commands to configure your global profile or adjust settings for a specific local project.
# Set global user profile
git config --global user.name "developer_name"
git config --global user.email "developer@example.com"
Branch Management and Navigation
Efficiently navigating through branches and cloning remote repositories forms the backbone of collaborative development.
# Clone a remote repository to your local machine
git clone https://github.com/username/repository.git
# List all local and remote branches
git branch -a
# Switch to an existing branch
git checkout feature-xyz
# Create and switch to a new branch simultaneously
git checkout -b hotfix-branch
# Remove a local branch
git branch -d hotfix-branch
The Delivery Pipeline: Add, Commit, and Push
Moving changes from your working directory to a remote server involves staging, committing, and pushing.
# Stage all changes for the next commit
git add .
# Record the snapshot with a descriptive message
git commit -m "feat: implement user authentication logic"
# Link a local repository to a remote server
git remote add origin https://gitee.com/user/project.git
# Manage remote aliases (remove or rename if they exist)
git remote rm origin
git remote rename origin legacy-origin
# Upload changes to a specific branch on the remote
git push -u origin main
Authentication via SSH Keys
Using RSA keys allows for secure, password-less communication with Git hosting services.
# Navigate to the SSH directory or create it
mkdir -p ~/.ssh && cd ~/.ssh
# Generate a new 4096-bit RSA key pair
ssh-keygen -t rsa -b 4096 -C "developer@example.com"
# Display the public key to copy into GitHub/Gitee settings
cat id_rsa.pub
Diagnostic and Maintenance Commands
These utilities help monitor the repository state and resolve discrepancies.
| Command | Purpose |
|---|---|
git status |
View modified files and staged changes. |
git diff |
Show differences between working tree and index. |
git log --oneline |
View a condensed history of commits. |
git reset --hard HEAD~1 |
Revert the local environment to the previous commit. |
git fetch |
Download objects and refs from another repository. |
git pull |
Fetch from and integrate with another repository or local branch. |
Purging Commit History
To restart a repository's history without deleting the files, use an orphan branch. This is useful for clearing sensitive data or resettting long-running projects.
# Create a temporary branch with no history
git checkout --orphan temporary_clean_slate
# Stage all current files
git add -A
# Create the initial commit
git commit -am "Initial system state"
# Delete the old primary branch
git branch -D main
# Rename the current branch to main
git branch -m main
# Force push to overwrite the remote history
git push -f origin main
Environment and Proxy Optimization
Managing cross-platform line endings and network proxies ensures a smooth workflow in restricted or heterogeneous environments.
Line Ending Normalization (LF vs CRLF):
# Enforce LF on checkout and commit to avoid Windows/Linux conflicts
git config --global core.autocrlf input
git config --global core.eol lf
Credential Storage and Proxy Tunnels:
# Cache credentials to avoid repetitive logins
git config --global credential.helper store
# Configure HTTP/HTTPS proxies for restricted networks
git config --global http.proxy 'http://127.0.0.1:7890'
git config --global https.proxy 'http://127.0.0.1:7890'
Leveraging Remote Repositories for Learning
Platforms like GitHub and Gitee provide two primary ways to obtain source code: direct ZIP downloads or Git cloning. Cloning is preferred as it retains versioning information and allows for easier updates.
To clone via the terminal, copy the repository URL and execute:
git clone https://github.com/example/learning-resource.git
Accelerating Access via Mirroring: When direct access to GitHub is throttled, Gitee's repository import feature acts as a high-speed mirror:
- Log in to Gitee and select "New Repository."
- Choose the "Import from External Site" (GitHub/GitLab) option.
- Provide the GitHub repository URL.
- Once the synchronization is complete, clone the repository from Gitee's servers for significantly faster transfer speeds.