Git stores configuration settings at different levels. System-wide settings for all users are located in /etc/gitconfig, while user-specific settings reside in ~/.gitconfig.
To display all current configurations:
git config --list
Setting user information globally (affects ~/.gitconfig):
git config --global user.name "John Doe"
git config --global user.email "john@example.com"
For system-wide changes (modifies /etc/gitconfig):
git config --system user.name "John Doe"
Core Git Operations
Cloning Repositories
Create a local copy of an existing repository:
git clone https://github.com/user/project.git
Git supports multiple protocols including SSH, HTTP, and HTTPS.
File Lifecycle States
- Untracked: Files not yet tracked by Git
- Modified: Tracked files with pending changes
- Staged: Changes marked for inclusion in the next commit
- Committed: Files stored in the local Git database
Repository Initialization and Status
Initialize a new repository in the current directory:
git init
Check the current status of all files:
git status
Staging and Committing Files
Add a file to the staging area:
git add src/main.c
To unstage a file without discarding changes:
git restore --staged src/main.c
Commit staged changes:
git commit -m "Add main module"
Modifying Tracked Files
After editing a tracked file, stage the modifications:
git add src/main.c
Unstage modifications:
git restore --staged src/main.c
Discard all local changes and revert to the last committed version:
git restore src/main.c
Removing Files
Delete a file from both the working directory and the repository:
git rm src/old_module.c
git commit -m "Remove deprecated module"
To remove a file from the repository but keep it locally:
git rm --cached config/local_settings.json
git commit -m "Stop tracking local config"
Recover a mistakenly deleted file before committing:
git restore --staged src/old_module.c
git restore src/old_module.c
To recover a file after a committed deletion, use the reflog or checkout from a previous commit:
git log --oneline
git checkout <commit-hash> -- src/old_module.c
Remote Repository Management
Viewing Remote Connections
List all remote repositories:
git remote
Display remote names with their URLs:
git remote -v
Show detailed information about a specific remote:
git remote show origin
Managing Remote References
Add a new remote repository:
git remote add upstream git://server.example.com/project.git
Rename a remote:
git remote rename old_name new_name
Remove a remote reference:
git remote remove upstream
Data Synchronization
Fetch updates without merging:
git fetch origin
Fetch and automatically merge into the current branch:
git pull origin main
Push local commits to a remote repository:
git push origin main
Working with Tags
Listing Tags
git tag
git show v1.0.0
Creating Tags
Create a lightweight tag:
git tag v1.0.0
Create an annotated tag with a message:
git tag -a v1.0.0 -m "Release version 1.0.0"
Tag a specific historical commit:
git tag -a v0.9.0 a1b2c3d
Deleting and Pushing Tags
Remove a local tag:
git tag -d v1.0.0
Push all tags to a remote repository:
git push origin --tags
Branch Operations
Viewing and Switching Branches
git branch # List all branches
git branch -v # Show branches with last commit
git branch --merged # Show merged branches
git branch --no-merged # Show unmerged branches
git checkout dev # Switch to dev branch
Creating New Branches
git branch feature-x # Create branch
git checkout -b feature-y # Create and switch
Merging Branches
git checkout main
git merge feature-x
If conflicts occur, manually resolve them in the affected files, then complete the merge:
git add resolved_file.c
git commit -m "Merge feature-x into main"
Deleting Branches
git branch -d feature-x # Safe delete
git branch -D feature-y # Force delete unmerged branch
Remote Branch Operations
Push a local branch to a remote:
git push origin feature-x
Delete a remote branch:
git push origin --delete feature-x
Setting Up a Bare Repository Server
On the server, create a dedicated user and initialize a bare repository:
groupadd gitusers
useradd -m -g gitusers gituser
su - gituser
mkdir -p ~/repos/project.git
cd ~/repos/project.git
git init --bare
On the client machine, initialize and push to the server:
mkdir ~/workspace/project
cd ~/workspace/project
git init
touch README.md
git add README.md
git commit -m "Initial commit"
git remote add origin gituser@server.example.com:~/repos/project.git
git push -u origin main
Troubleshooting Common Isues
Resolving Merge Conflicts During Pull
When git pull results in conflicts, edit the conflicting files. Conflict markers appear as:
<<<<<<< HEAD
local changes
=======
remote changes
>>>>>>> origin/main
Manually resolve the conflict by keeping the correct code and removing the markers, then commit the resolution:
git add .
git commit -m "Resolve merge conflicts"
Stashing Local Changes Before Pull
If uncommitted local changes conflict with incoming updates:
git stash
git pull
git stash pop
Branch Merge Conflicts
Conflicts during branch merging follow the same resolution process. After resolving, complete the merge with a commit.
Advanced Operations
Generating Patches
git format-patch -1 # Patch for last commit
git format-patch -2 # Patches for last 2 commits
git format-patch a1b2c3d..e4f5g6h # Patches between commits
git format-patch v1.0.0..v2.0.0 # Patches between tags
Exporting Specific Versions
Create a tarball of a specific tag or commit:
git archive --format=tar --prefix="project-v1.0/" v1.0.0 > ../project-v1.0.tar