-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit commands
More file actions
428 lines (304 loc) · 8.26 KB
/
git commands
File metadata and controls
428 lines (304 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# Git Commands Reference Guide
## 📌 Initial Setup & Configuration
```bash
# Configure user information for all local repositories
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# View all configuration settings
git config --list
# Set default branch name to main
git config --global init.defaultBranch main
```
## 🚀 Creating & Initializing Repositories
```bash
# Initialize a new git repository in current directory
git init
# Clone an existing repository from URL
git clone <repository-url>
# Clone to a specific folder name
git clone <repository-url> <folder-name>
```
## 📊 Basic Status & Information
```bash
# Check status of working directory and staging area
git status
# Show commit history
git log
# Show compact commit history (one line per commit)
git log --oneline
# Show last n commits
git log -n 5
# Show commits with file changes
git log --stat
# Show visual branch graph
git log --graph --oneline --all
```
## 📝 Staging & Committing Changes
```bash
# Add specific file to staging area
git add <filename>
# Add all changed files to staging area
git add .
# Add all files (including deletions)
git add -A
# Commit staged changes with message
git commit -m "commit message"
# Add and commit in one step (tracked files only)
git commit -am "commit message"
# Amend the last commit (change message or add forgotten files)
git commit --amend -m "new commit message"
```
## 🔍 Viewing Changes & Differences
```bash
# Show unstaged changes in working directory
git diff
# Show changes in a specific file
git diff <filename>
# Show staged changes (what will be committed)
git diff --staged
# or
git diff --cached
# Compare two branches
git diff <branch1> <branch2>
# Compare specific commits
git diff <commit1> <commit2>
```
## ⏮️ Undoing Changes & Rolling Back
```bash
# Discard changes in working directory for specific file
git checkout <filename>
# or (newer syntax)
git restore <filename>
# Unstage a file (keep changes in working directory)
git reset HEAD <filename>
# or (newer syntax)
git restore --staged <filename>
# Remove all files from staging area
git rm --cached -r .
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Undo last commit and unstage changes (keep in working directory)
git reset HEAD~1
# or
git reset --mixed HEAD~1
# Completely remove last commit and all changes (DANGEROUS!)
git reset --hard HEAD~1
# Revert a specific commit (creates new commit that undoes it)
git revert <commit-hash>
# Go back to a specific commit (detached HEAD state)
git checkout <commit-hash>
```
## 🌐 Working with Remotes
```bash
# View remote repositories
git remote -v
# Add a remote repository
git remote add origin <repository-url>
# Change remote URL
git remote set-url origin <new-url>
# Remove a remote
git remote remove <remote-name>
# Fetch changes from remote (doesn't merge)
git fetch origin
# Pull changes from remote and merge
git pull origin <branch-name>
# Push commits to remote repository
git push origin <branch-name>
# Push and set upstream tracking (first time)
git push -u origin main
# Push all branches
git push --all origin
# Push tags
git push --tags
```
## 🌿 Branching & Merging
```bash
# List all local branches
git branch
# List all branches (including remote)
git branch -a
# Create a new branch
git branch <branch-name>
# Switch to a branch
git checkout <branch-name>
# or (newer syntax)
git switch <branch-name>
# Create and switch to new branch in one command
git checkout -b <branch-name>
# or
git switch -c <branch-name>
# Rename current branch
git branch -m <new-branch-name>
# Delete a local branch
git branch -d <branch-name>
# Force delete a branch (unmerged changes)
git branch -D <branch-name>
# Delete remote branch
git push origin --delete <branch-name>
# Merge another branch into current branch
git merge <branch-name>
# Merge with no fast-forward (always create merge commit)
git merge --no-ff <branch-name>
# Abort a merge in progress
git merge --abort
```
## 🔄 Rebase (Alternative to Merge)
```bash
# Rebase current branch onto another branch
git rebase <branch-name>
# Continue rebase after resolving conflicts
git rebase --continue
# Skip current commit during rebase
git rebase --skip
# Abort rebase and return to original state
git rebase --abort
# Interactive rebase (edit, squash, reorder commits)
git rebase -i HEAD~n
```
## 🏷️ Tagging
```bash
# List all tags
git tag
# Create a lightweight tag
git tag <tag-name>
# Create an annotated tag with message
git tag -a <tag-name> -m "tag message"
# Tag a specific commit
git tag <tag-name> <commit-hash>
# Delete a local tag
git tag -d <tag-name>
# Delete a remote tag
git push origin --delete <tag-name>
# Push a specific tag
git push origin <tag-name>
# Push all tags
git push --tags
```
## 🔍 Searching & Finding
```bash
# Search for text in all files
git grep "search-term"
# Search in specific branch
git grep "search-term" <branch-name>
# Show who last modified each line of a file
git blame <filename>
# Find commits that introduced or removed a string
git log -S "search-term"
```
## 💾 Stashing (Temporary Storage)
```bash
# Stash current changes
git stash
# Stash with a descriptive message
git stash save "work in progress"
# List all stashes
git stash list
# Apply most recent stash and keep it in stash list
git stash apply
# Apply most recent stash and remove it from stash list
git stash pop
# Apply a specific stash
git stash apply stash@{n}
# Delete most recent stash
git stash drop
# Delete a specific stash
git stash drop stash@{n}
# Clear all stashes
git stash clear
# Show changes in a stash
git stash show
```
## 🔧 Advanced Operations
```bash
# Show reflog (history of HEAD movements)
git reflog
# Cherry-pick a specific commit from another branch
git cherry-pick <commit-hash>
# Clean untracked files (dry run first!)
git clean -n
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Show details of a specific commit
git show <commit-hash>
# List files tracked by git
git ls-files
# Update .gitignore for already tracked files
git rm -r --cached .
git add .
git commit -m "Update .gitignore"
```
## 🔀 Forking & Pull Requests Workflow
```bash
# 1. Fork repository on GitHub (using web interface)
# 2. Clone your fork
git clone <your-fork-url>
# 3. Add original repository as upstream
git remote add upstream <original-repo-url>
# 4. Create feature branch
git checkout -b feature-branch
# 5. Make changes and commit
git add .
git commit -m "Add feature"
# 6. Push to your fork
git push origin feature-branch
# 7. Create Pull Request on GitHub (using web interface)
# 8. Sync with original repository
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
```
## 📋 Git Workflow Summary
```
Working Directory → Staging Area → Local Repository → Remote Repository
↓ ↓ ↓
git add git commit git push
```
## 🚨 Common Scenarios & Solutions
### Accidentally committed to wrong branch
```bash
# Reset the branch to previous commit (keep changes)
git reset HEAD~ --soft
# Switch to correct branch
git checkout correct-branch
# Add and commit
git add .
git commit -m "commit message"
```
### Undo last push (CAREFUL!)
```bash
# Reset local branch
git reset --hard HEAD~1
# Force push (only if no one else pulled)
git push -f origin <branch-name>
```
### Resolve merge conflicts
```bash
# 1. View conflicted files
git status
# 2. Edit files to resolve conflicts (remove conflict markers)
# 3. Mark as resolved
git add <resolved-file>
# 4. Complete merge
git commit
```
### Update fork from original repository
```bash
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
```
## 💡 Best Practices
- ✅ Commit often with clear, descriptive messages
- ✅ Pull before you push to avoid conflicts
- ✅ Use branches for new features/fixes
- ✅ Review changes before committing (`git diff`)
- ✅ Keep commits focused (one logical change per commit)
- ✅ Write commit messages in present tense: "Add feature" not "Added feature"
- ✅ Test before committing
- ❌ Don't commit sensitive information (passwords, API keys)
- ❌ Don't use `--force` unless absolutely necessary
- ❌ Don't commit large binary files