Rewrited Git Doc

This commit is contained in:
2026-03-16 15:46:58 +03:30
parent 06eef16b93
commit bd21f7c0df

View File

@@ -1,523 +1,468 @@
# Git Commands Guide (DevOps-Oriented) # Git Commands Guide for DevOps Engineers
**Professional Reference Document**
*Comprehensive Git workflow for development, CI/CD pipelines, and team collaboration*
---
## Table of Contents
1. [Installation and Setup](#1-installation-and-setup)
2. [SSH Key Configuration](#2-ssh-key-configuration)
3. [Repository Initialization](#3-repository-initialization)
4. [Basic Workflow](#4-basic-workflow)
5. [Status and History](#5-status-and-history)
6. [File Operations](#6-file-operations)
7. [Branch Management](#7-branch-management)
8. [Merging and Rebasing](#8-merging-and-rebasing)
9. [Remote Operations](#9-remote-operations)
10. [Commit Management](#10-commit-management)
11. [Removing Commits](#11-removing-commits)
12. [Stash Operations](#12-stash-operations)
13. [Tags and Releases](#13-tags-and-releases)
14. [.gitignore Management](#14-gitignore-management)
15. [Configuration and Aliases](#15-configuration-and-aliases)
16. [Troubleshooting and Recovery](#16-troubleshooting-and-recovery)
17. [Repository Cloning](#17-repository-cloning)
---
## 1. Installation and Setup ## 1. Installation and Setup
### Install Git ### **Install Git**
Download from official source: [git-scm.com](https://git-scm.com/)
Download and install Git from:
[https://git-scm.com/](https://git-scm.com/)
Linux (Debian/Ubuntu):
**Linux Distributions:**
```bash ```bash
# Debian/Ubuntu
sudo apt update && sudo apt install git -y sudo apt update && sudo apt install git -y
# RHEL/CentOS/Fedora
sudo yum install git -y # or dnf install git -y
``` ```
RHEL/CentOS: **macOS:**
```bash
sudo yum install git -y
```
macOS (Homebrew):
```bash ```bash
brew install git brew install git
``` ```
### Verify Installation ### **Verify Installation**
```bash ```bash
git --version git --version
``` ```
*Displays installed Git version*
### Configure User Identity ### **Configure User Identity**
Git requires author information for every commit:
Git uses this information for commits:
```bash ```bash
git config --global user.name "Your Name" git config --global user.name "Your Full Name"
git config --global user.email "your.email@example.com" git config --global user.email "your.email@company.com"
``` ```
Check configuration: **Configuration Scopes:**
| Scope | Command Flag | Applies To | Persistence |
|-------|--------------|------------|-------------|
| System | `--system` | All users on machine | System-wide |
| Global | `--global` | Current user | User account |
| Local | `--local` | Specific repository | Repository only |
**Verify Configuration:**
```bash ```bash
git config --list git config --list
``` ```
Configuration scopes:
* `--system`: All users
* `--global`: Current user
* `--local`: Repository only
--- ---
## 2. SSH Key Configuration ## 2. SSH Key Configuration
### Generate SSH Key ### **Generate SSH Key Pair**
```bash ```bash
ssh-keygen -t ed25519 -C "your.email@example.com" ssh-keygen -t ed25519 -C "your.email@company.com"
``` ```
- **`-t ed25519`**: Modern, secure key algorithm
- **`-C`**: Comment for key identification
Start SSH agent and add key: ### **SSH Agent Management**
```bash ```bash
# Start SSH agent
eval "$(ssh-agent -s)" eval "$(ssh-agent -s)"
# Add private key to agent
ssh-add ~/.ssh/id_ed25519 ssh-add ~/.ssh/id_ed25519
``` ```
### Use Custom SSH Key (Per Repository) ### **Per-Repository SSH Key**
```bash ```bash
git config --local core.sshCommand "ssh -i <PATH_TO_SSH_KEY>" # Set custom key for specific repo
``` git config --local core.sshCommand "ssh -i /path/to/custom_key"
Clone with custom SSH key: # Clone with specific key (one-time)
git -c core.sshCommand="ssh -i /path/to/key" clone git@host:repo.git
```bash
git -c core.sshCommand="ssh -i <key-path>" clone git@host:repo.git
``` ```
--- ---
## 3. Initialize Repository ## 3. Repository Initialization
Create a new Git repository:
### **Create New Repository**
```bash ```bash
# Initialize with main branch
git init -b main git init -b main
```
Existing repository: # Initialize with default branch
```bash
git init git init
``` ```
**Key Concepts:**
- **Working Directory**: Files not yet tracked by Git
- **Staging Area (Index)**: Files prepared for commit
- **Repository**: Committed history and metadata
--- ---
## 4. Basic Workflow ## 4. Basic Workflow
### Stage and Commit Changes ### **Stage Changes**
Stage all changes:
```bash ```bash
# Stage all changes (new, modified, deleted)
git add -A git add -A
# Stage specific files
git add <file1> <file2>
# Stage all modified files (not new files)
git add .
``` ```
Commit changes: ### **Commit Changes**
```bash ```bash
git commit -m "Initial commit" git commit -m "Descriptive commit message"
``` ```
### Connect Local Repository to Remote ### **Connect to Remote**
```bash ```bash
git remote add origin <REPO_URL> git remote add origin <repository-url>
git remote -v # Verify remote configuration
``` ```
Verify: ### **Push to Remote**
```bash
git remote -v
```
### Push to Remote
First push:
```bash ```bash
# First push (sets upstream tracking)
git push -u origin main git push -u origin main
```
Subsequent pushes: # Subsequent pushes
```bash
git push git push
``` ```
--- ---
## 5. Repository Status and History ## 5. Status and History
### Check Repository Status
### **Repository Status**
```bash ```bash
git status git status
``` ```
*Shows working directory and staging area state*
### View Commit History ### **Commit History**
```bash
git log
```
Common options:
```bash ```bash
# One-line summary
git log --oneline git log --oneline
# Visual graph of all branches
git log --graph --oneline --all git log --graph --oneline --all
git log -p
git log -3 # Last N commits with patch
git log -p -3
# Show specific commit details
git show <commit-hash>
``` ```
### View File Changes ### **Change Visualization**
Unstaged changes:
```bash ```bash
# Unstaged changes (working directory)
git diff git diff
```
Staged changes: # Staged changes (index vs HEAD)
```bash
git diff --staged git diff --staged
```
Compare branches: # Branch comparison
git diff main..develop
```bash
git diff main..dev
``` ```
--- ---
## 6. File Operations ## 6. File Operations
### Stage Specific Files | Operation | Command | Effect |
|-----------|---------|---------|
```bash | Stage file | `git add <file>` | Moves file to staging area |
git add <file> | Unstage | `git reset <file>` | Removes from staging, keeps changes |
``` | Discard changes | `git restore <file>` | Reverts to last committed version |
| Rename | `git mv old new` | Stages rename operation |
### Unstage Files | Remove (tracked) | `git rm <file>` | Stages file deletion |
| Untrack | `git rm --cached <file>` | Removes from Git, keeps locally |
```bash
git reset <file>
```
### Discard Local Changes
```bash
git checkout -- <file>
```
Restore using modern command:
```bash
git restore <file>
```
### Rename File
```bash
git mv old-name new-name
```
### Remove File
```bash
git rm <file>
```
Remove but keep locally:
```bash
git rm --cached <file>
```
--- ---
## 7. Branch Management ## 7. Branch Management
### Create and Switch Branch ### **Branch Operations**
```bash ```bash
git checkout -b <branch-name> # Create and switch
``` git switch -c feature/new-api
Modern alternative: # List branches
git branch -v # Local branches with last commit
git branch -a # All branches (local + remote)
```bash # Delete branch
git switch -c <branch-name> git branch -d feature # Safe delete (merged)
``` git branch -D feature # Force delete
### List Branches # Rename branch
```bash
git branch
git branch -a
git branch -v
```
### Delete Branch
```bash
git branch -d <branch-name>
```
Force delete:
```bash
git branch -D <branch-name>
```
### Rename Branch
```bash
git branch -m old-name new-name git branch -m old-name new-name
``` ```
**Branch States:**
- **Local Branch**: Exists only in your repository
- **Remote Branch**: Exists on remote server (`origin/main`)
- **Tracking Branch**: Local branch linked to remote (`main -> origin/main`)
--- ---
## 8. Merging and Rebasing ## 8. Merging and Rebasing
### Merge Branch ### **Merge (Preserves History)**
```bash ```bash
git merge <branch-name> git checkout main
git merge feature/xyz
``` ```
Merge types: **Merge Types:**
| Type | Condition | Result |
* Fast-forward |------|-----------|---------|
* Three-way merge (creates merge commit) | Fast-forward | Target ahead, no divergence | Linear history |
| Three-way | Both branches have new commits | Merge commit created |
### Rebase (Linear History)
### **Rebase (Linear History)**
```bash ```bash
git checkout feature/xyz
git rebase main git rebase main
``` ```
Abort rebase: **Rebase Controls:**
```bash ```bash
git rebase --abort git rebase --abort # Cancel rebase
``` git rebase --continue # Resolve conflicts and continue
Continue rebase:
```bash
git rebase --continue
``` ```
--- ---
## 9. Remote Operations ## 9. Remote Operations
### List Remotes ### **Remote Management**
```bash ```bash
git remote git remote -v # List remotes
git remote -v git remote show origin # Detailed remote info
git fetch --all # Fetch all remotes
``` ```
### Show Remote Details ### **Pull Strategies**
```bash ```bash
git remote show origin git pull # Fetch + merge
``` git pull --rebase # Fetch + rebase (cleaner history)
### Fetch Changes
```bash
git fetch
git fetch --all
```
### Pull Changes
Fetch + merge:
```bash
git pull
```
Rebase instead of merge:
```bash
git pull --rebase
``` ```
--- ---
## 10. Commit Management ## 10. Commit Management
### Amend Last Commit ### **Modify Last Commit**
```bash ```bash
git commit --amend git commit --amend # Edit message/files
``` ```
### Show Commit Details ### **Safe Undo (Shared Branches)**
```bash ```bash
git show <commit-id> git revert <commit-hash> # Creates reversing commit
``` ```
### Revert Commit (Safe for Shared Branches) ### **Reset Types**
```bash ```bash
git revert <commit-id> git reset --soft HEAD~1 # Keeps staging area
git reset HEAD~1 # Unstages, keeps files
git reset --hard HEAD~1 # Discards everything
``` ```
### Reset Commit (Use with Caution) ---
Soft reset: ## 11. Removing Commits
### **Remove Local (Unpushed) Commit**
```bash ```bash
# Soft reset (interactive rebase recommended)
git reset --soft HEAD~1 git reset --soft HEAD~1
# Interactive rebase for multiple commits
git rebase -i HEAD~3
# Change 'pick' to 'drop' or delete line
``` ```
Mixed reset: ### **Remove Pushed Commit from Remote**
```bash **⚠️ DANGER: Rewrites shared history**
git reset HEAD~1
```
Hard reset:
```bash ```bash
# 1. Reset locally
git reset --hard HEAD~1 git reset --hard HEAD~1
# 2. Force push (collaborators must coordinate)
git push --force-with-lease origin main
# 3. Alternative: Safer revert
git revert HEAD # Creates undoing commit
git push
```
**Team Coordination Required:**
```
1. Notify team before force push
2. Team runs: git fetch && git reset --hard origin/main
3. Use revert for shared production branches
```
### **Remove Specific Pushed Commit**
```bash
# Interactive rebase
git rebase -i <commit-before-target>~1
# Or create revert
git revert <specific-commit-hash>
``` ```
--- ---
## 11. Stash (Temporary Changes) ## 12. Stash Operations
Save work without committing:
```bash
git stash
```
List stashes:
**Temporary Storage:**
```bash ```bash
git stash push -m "WIP: API changes"
git stash list git stash list
``` git stash apply stash@{0} # Keep stash
git stash pop # Apply and remove
Apply stash:
```bash
git stash apply
```
Pop stash:
```bash
git stash pop
``` ```
--- ---
## 12. Tags (Releases) ## 13. Tags and Releases
Create tag:
### **Tag Management**
```bash ```bash
git tag v1.0.0 # Lightweight tag
``` git tag v1.2.3
Annotated tag: # Annotated tag (recommended)
git tag -a v1.2.3 -m "Release v1.2.3"
```bash # Push tags
git tag -a v1.0.0 -m "Release v1.0.0"
```
Push tags:
```bash
git push origin --tags git push origin --tags
``` ```
--- ---
## 13. .gitignore ## 14. .gitignore Management
Create `.gitignore`:
**Create/Update:**
```bash ```bash
touch .gitignore touch .gitignore
``` ```
Example: **Common Patterns:**
``` ```
.env # Dependencies
node_modules/ node_modules/
vendor/
# Logs
*.log *.log
logs/
# Environment
.env
*.env.local
# OS
.DS_Store
Thumbs.db
``` ```
Apply after commit: **Apply Existing .gitignore:**
```bash ```bash
git rm -r --cached . git rm -r --cached .
git add . git add . && git commit -m "Apply .gitignore"
git commit -m "Apply gitignore"
``` ```
--- ---
## 14. Useful Configuration and Aliases ## 15. Configuration and Aliases
Change default editor:
### **Editor and Pager**
```bash ```bash
git config --global core.editor "vim" git config --global core.editor "code --wait"
``` ```
Create aliases: ### **Productivity Aliases**
```bash ```bash
git config --global alias.st status git config --global alias.st "status"
git config --global alias.co checkout git config --global alias.co "checkout"
git config --global alias.cm commit git config --global alias.br "branch -v"
git config --global alias.br branch git config --global alias.cm "!f() { git add -A && git commit -m \"$@\"; }; f"
``` ```
--- ---
## 15. Troubleshooting and Recovery ## 16. Troubleshooting and Recovery
Undo last commit but keep changes:
```bash
git reset --soft HEAD~1
```
Recover deleted branch:
### **Common Recovery**
```bash ```bash
# View all history (including resets)
git reflog git reflog
git checkout -b <branch-name> <commit-id>
```
Fix detached HEAD: # Recover deleted branch
git checkout -b recovery-branch <commit-hash>
```bash # Fix detached HEAD
git checkout main git checkout main
``` ```
--- ---
## 16. Clone Repository ## 17. Repository Cloning
Clone via SSH:
```bash ```bash
git clone git@github.com:user/repo.git # Standard clone
git clone <url>
# Specific branch
git clone -b develop <url>
# Shallow clone (history limited)
git clone --depth 1 <url>
``` ```
Clone specific branch: ---
```bash ## Key Git Concepts Explained
git clone -b <branch> <repo-url>
```
| Concept | Definition | Importance |
|---------|------------|-----------|
| **HEAD** | Current commit/branch pointer | Always points to active commit |
| **Index/Staging** | Intermediate area between working dir and repo | Prepares exact commit content |
| **Fast-forward** | Linear merge without merge commit | Clean history |
| **Detached HEAD** | HEAD points directly to commit | Use for inspection, create branch to save work |
| **Reflog** | Local history of HEAD movements | Recovery lifeline |
| **Force Push** | Overwrites remote history | Use only with team coordination |
**Document Version: 2.0**
*Optimized for DevOps workflows, CI/CD integration, and team collaboration*