git doc: updated and cleaned
This commit is contained in:
@@ -1,215 +1,523 @@
|
|||||||
# Git Commands Guide
|
# Git Commands Guide (DevOps-Oriented)
|
||||||
|
|
||||||
## 1. Installation and Setup
|
## 1. Installation and Setup
|
||||||
|
|
||||||
### Install Git
|
### Install Git
|
||||||
Download from [git-scm.com](https://git-scm.com/).
|
|
||||||
|
Download and install Git from:
|
||||||
|
[https://git-scm.com/](https://git-scm.com/)
|
||||||
|
|
||||||
|
Linux (Debian/Ubuntu):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt update && sudo apt install git -y
|
||||||
|
```
|
||||||
|
|
||||||
|
RHEL/CentOS:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo yum install git -y
|
||||||
|
```
|
||||||
|
|
||||||
|
macOS (Homebrew):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
brew install git
|
||||||
|
```
|
||||||
|
|
||||||
### Verify Installation
|
### Verify Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git --version
|
git --version
|
||||||
```
|
```
|
||||||
|
|
||||||
### Configure User Info
|
### Configure User Identity
|
||||||
Set your name and email for commits:
|
|
||||||
|
Git uses this information for commits:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git config --global user.name "Your Name"
|
git config --global user.name "Your Name"
|
||||||
git config --global user.email "your.email@example.com"
|
git config --global user.email "your.email@example.com"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Check configuration:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git config --list
|
||||||
|
```
|
||||||
|
|
||||||
|
Configuration scopes:
|
||||||
|
|
||||||
|
* `--system`: All users
|
||||||
|
* `--global`: Current user
|
||||||
|
* `--local`: Repository only
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 2. SSH Key Configuration
|
## 2. SSH Key Configuration
|
||||||
|
|
||||||
Use a custom SSH key:
|
### Generate SSH Key
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git config --add --local core.sshCommand 'ssh -i <PATH_TO_SSH_KEY>'
|
ssh-keygen -t ed25519 -C "your.email@example.com"
|
||||||
```
|
```
|
||||||
|
|
||||||
Clone with custom key:
|
Start SSH agent and add key:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git -c core.sshCommand="ssh -i <key-path>" clone host:repo
|
eval "$(ssh-agent -s)"
|
||||||
|
ssh-add ~/.ssh/id_ed25519
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Use Custom SSH Key (Per Repository)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git config --local core.sshCommand "ssh -i <PATH_TO_SSH_KEY>"
|
||||||
|
```
|
||||||
|
|
||||||
|
Clone with custom SSH key:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git -c core.sshCommand="ssh -i <key-path>" clone git@host:repo.git
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 3. Initialize Repository
|
## 3. Initialize Repository
|
||||||
|
|
||||||
Create a new Git repo:
|
Create a new Git repository:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git init -b main
|
git init -b main
|
||||||
```
|
```
|
||||||
- `-b main`: Sets default branch name to `main`.
|
|
||||||
|
Existing repository:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git init
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 4. Basic Workflow
|
## 4. Basic Workflow
|
||||||
|
|
||||||
### Stage and Commit
|
### Stage and Commit Changes
|
||||||
|
|
||||||
Stage all changes:
|
Stage all changes:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git add -A
|
git add -A
|
||||||
```
|
```
|
||||||
|
|
||||||
Commit changes:
|
Commit changes:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git commit -m "Initial Commit"
|
git commit -m "Initial commit"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Connect to Remote
|
### Connect Local Repository to Remote
|
||||||
Link local repo to remote:
|
|
||||||
```bash
|
```bash
|
||||||
git remote add origin <Repo-Link>
|
git remote add origin <REPO_URL>
|
||||||
|
```
|
||||||
|
|
||||||
|
Verify:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git remote -v
|
||||||
```
|
```
|
||||||
|
|
||||||
### Push to Remote
|
### Push to Remote
|
||||||
|
|
||||||
|
First push:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git push origin main
|
git push -u origin main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Subsequent pushes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 5. Repository Status and History
|
## 5. Repository Status and History
|
||||||
|
|
||||||
### Check Status
|
### Check Repository Status
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git status
|
git status
|
||||||
```
|
```
|
||||||
Shows staged, unstaged, and untracked files.
|
|
||||||
|
|
||||||
### View History
|
### View Commit History
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git log
|
git log
|
||||||
```
|
```
|
||||||
- `git log -p`: Shows diffs per commit.
|
|
||||||
- `git log -3`: Shows last 3 commits.
|
|
||||||
- `git log --graph`: Shows history as a graph.
|
|
||||||
- `git log --oneline`: Shows compact log.
|
|
||||||
|
|
||||||
### View Changes
|
Common options:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git log --oneline
|
||||||
|
git log --graph --oneline --all
|
||||||
|
git log -p
|
||||||
|
git log -3
|
||||||
|
```
|
||||||
|
|
||||||
|
### View File Changes
|
||||||
|
|
||||||
|
Unstaged changes:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git diff
|
git diff
|
||||||
```
|
```
|
||||||
Shows unstaged changes.
|
|
||||||
|
Staged changes:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git diff --staged
|
git diff --staged
|
||||||
```
|
```
|
||||||
Shows staged changes.
|
|
||||||
|
Compare branches:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git diff main..dev
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 6. File Operations
|
## 6. File Operations
|
||||||
|
|
||||||
### Stage Specific Files
|
### Stage Specific Files
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git add <filename>
|
git add <file>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Unstage Files
|
### Unstage Files
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git reset <filename>
|
git reset <file>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Discard Changes
|
### Discard Local Changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git checkout -- <filename>
|
git checkout -- <file>
|
||||||
|
```
|
||||||
|
|
||||||
|
Restore using modern command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git restore <file>
|
||||||
```
|
```
|
||||||
Reverts file to last committed state.
|
|
||||||
|
|
||||||
### Rename File
|
### Rename File
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git mv <old-name> <new-name>
|
git mv old-name new-name
|
||||||
```
|
```
|
||||||
|
|
||||||
### Remove File
|
### Remove File
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git rm <filename>
|
git rm <file>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Remove but keep locally:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git rm --cached <file>
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 7. Branch Management
|
## 7. Branch Management
|
||||||
|
|
||||||
### Create and Switch to Branch
|
### Create and Switch Branch
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git checkout -b <branch-name>
|
git checkout -b <branch-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Modern alternative:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git switch -c <branch-name>
|
||||||
|
```
|
||||||
|
|
||||||
### List Branches
|
### List Branches
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git branch
|
git branch
|
||||||
```
|
git branch -a
|
||||||
List with details:
|
|
||||||
```bash
|
|
||||||
git branch -v
|
git branch -v
|
||||||
```
|
```
|
||||||
|
|
||||||
### Delete Branch
|
### Delete Branch
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git branch -d <branch-name>
|
git branch -d <branch-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Switch Branch
|
Force delete:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git checkout <branch-name>
|
git branch -D <branch-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
## 8. Merging
|
### Rename Branch
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git branch -m old-name new-name
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Merging and Rebasing
|
||||||
|
|
||||||
### Merge Branch
|
### Merge Branch
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git merge <branch-name>
|
git merge <branch-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
**Merge Types:**
|
Merge types:
|
||||||
- **Fast-forward:** No conflicts; branch is ahead of target.
|
|
||||||
- **Three-way:** Conflicts possible; creates new merge commit.
|
* Fast-forward
|
||||||
|
* Three-way merge (creates merge commit)
|
||||||
|
|
||||||
|
### Rebase (Linear History)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git rebase main
|
||||||
|
```
|
||||||
|
|
||||||
|
Abort rebase:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git rebase --abort
|
||||||
|
```
|
||||||
|
|
||||||
|
Continue rebase:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git rebase --continue
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 9. Remote Operations
|
## 9. Remote Operations
|
||||||
|
|
||||||
### List Remotes
|
### List Remotes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git remote
|
git remote
|
||||||
```
|
|
||||||
|
|
||||||
### Show Remote Info
|
|
||||||
```bash
|
|
||||||
git remote -v
|
git remote -v
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Show Remote Details
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git remote show origin
|
git remote show origin
|
||||||
```
|
```
|
||||||
|
|
||||||
### Fetch Changes
|
### Fetch Changes
|
||||||
```bash
|
|
||||||
git fetch
|
|
||||||
```
|
|
||||||
Get updates without merging.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
git fetch
|
||||||
git fetch --all
|
git fetch --all
|
||||||
```
|
```
|
||||||
Fetch from all remotes.
|
|
||||||
|
### Pull Changes
|
||||||
|
|
||||||
|
Fetch + merge:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git pull
|
||||||
|
```
|
||||||
|
|
||||||
|
Rebase instead of merge:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git pull --rebase
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 10. Commit Management
|
## 10. Commit Management
|
||||||
|
|
||||||
### Edit Last Commit Message
|
### Amend Last Commit
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git commit --amend
|
git commit --amend
|
||||||
```
|
```
|
||||||
|
|
||||||
### Show Commit Changes
|
### Show Commit Details
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git show <commit-id>
|
git show <commit-id>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Revert Commit
|
### Revert Commit (Safe for Shared Branches)
|
||||||
```bash
|
|
||||||
git revert HEAD
|
|
||||||
```
|
|
||||||
Revert last commit.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git revert <commit-id>
|
git revert <commit-id>
|
||||||
```
|
```
|
||||||
Revert specific commit.
|
|
||||||
|
|
||||||
## 11. Miscellaneous
|
### Reset Commit (Use with Caution)
|
||||||
|
|
||||||
|
Soft reset:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git reset --soft HEAD~1
|
||||||
|
```
|
||||||
|
|
||||||
|
Mixed reset:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git reset HEAD~1
|
||||||
|
```
|
||||||
|
|
||||||
|
Hard reset:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git reset --hard HEAD~1
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11. Stash (Temporary Changes)
|
||||||
|
|
||||||
|
Save work without committing:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git stash
|
||||||
|
```
|
||||||
|
|
||||||
|
List stashes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git stash list
|
||||||
|
```
|
||||||
|
|
||||||
|
Apply stash:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git stash apply
|
||||||
|
```
|
||||||
|
|
||||||
|
Pop stash:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git stash pop
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. Tags (Releases)
|
||||||
|
|
||||||
|
Create tag:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git tag v1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
Annotated tag:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git tag -a v1.0.0 -m "Release v1.0.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
Push tags:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push origin --tags
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. .gitignore
|
||||||
|
|
||||||
|
Create `.gitignore`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
touch .gitignore
|
||||||
|
```
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
.env
|
||||||
|
node_modules/
|
||||||
|
*.log
|
||||||
|
```
|
||||||
|
|
||||||
|
Apply after commit:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git rm -r --cached .
|
||||||
|
git add .
|
||||||
|
git commit -m "Apply gitignore"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 14. Useful Configuration and Aliases
|
||||||
|
|
||||||
|
Change default editor:
|
||||||
|
|
||||||
### Change Default Editor
|
|
||||||
```bash
|
```bash
|
||||||
git config --global core.editor "vim"
|
git config --global core.editor "vim"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Clone Repository
|
Create aliases:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone <Repo-Link>
|
git config --global alias.st status
|
||||||
|
git config --global alias.co checkout
|
||||||
|
git config --global alias.cm commit
|
||||||
|
git config --global alias.br branch
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 15. Troubleshooting and Recovery
|
||||||
|
|
||||||
|
Undo last commit but keep changes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git reset --soft HEAD~1
|
||||||
|
```
|
||||||
|
|
||||||
|
Recover deleted branch:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git reflog
|
||||||
|
git checkout -b <branch-name> <commit-id>
|
||||||
|
```
|
||||||
|
|
||||||
|
Fix detached HEAD:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git checkout main
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 16. Clone Repository
|
||||||
|
|
||||||
|
Clone via SSH:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone git@github.com:user/repo.git
|
||||||
|
```
|
||||||
|
|
||||||
|
Clone specific branch:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone -b <branch> <repo-url>
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user