gitGuide.tsxFractalis
1
# First steps
2
 
3
git init <!-- Initialize a new Git repository -->
4
git clone <url> <!-- Clone a Git repository -->
5
 
6
# Your commits
7
 
8
git add <file> <!-- Add a file to the staging area --> 
?

git add . <!-- Add all untracked files and unstaged changes -->

git add -p <!-- Add all untracked files and unstaged changes -->

9
git mv <file> <new-file> <!-- Move a file -->
10
git rm <file> <!-- Remove a file --> 
!

git rm --cached <file> <!-- Git forget about a file without deleting it -->

11
 
12
git reset <file> <!-- Unstage everything --> 
!

<!-- This command unstage EVERYTHING, if you want to unstage a specific file, use: --> git reset <file>

13
 
14
git status <!-- Check what you added to your staging area -->
15
 
16
# Make a commit
17
 
18
git commit -m <message> <!-- Make a commit --> 
?

<!-- If you modified existing files and want to commit quickly, use: --> git commit -am <message>

19
 
20
# Branches
21
 
22
git switch -c <branch> <!-- Create and switch to a new branch --> 
!

git checkout -b <branch> <!-- Create and switch to a new branch -->

23
git switch <branch> <!-- Switch to a existing branch --> 
!

git checkout <branch> <!-- Switch to an existing branch (legacy command) -->

24
 
25
git branch <!-- List all branches -->
26
git branch --sort=-committerdate <!-- List branches by most recently committed -->
27
 
28
git branch -d <branch> <!-- Delete a branch --> 
!

git branch -D <branch> <!-- Force delete a branch -->

29
 
30
# Diff staged and unstaged changes
31
 
32
git diff <!-- Show unstaged changes -->
33
git diff --staged <!-- Show staged changes -->
34
git diff HEAD <!-- All changes since last commit -->
35
 
36
# Diff commits
37
 
38
git diff show <commit> <!-- Show diff between a commit and its parent -->
39
git diff <commit1> <commit2> <!-- Show changes between two commits -->
40
git diff <commit> <file> <!-- Diff one file since a commit -->
41
 
42
git diff <commit> --stat <!-- This command show a summary of a diff --> 
?

git show <commit> <!-- Show changes between working directory and staging area -->

43
 
44
# Discard changes
45
 
46
git restore <file> <!-- Delete unstaged changes on the file. --> 
!

git checkout <file> <!-- Legacy command for discard changes -->

47
git restore --staged --worktree <file> <!-- Delete all staged and unstaged changes to one. --> 
!

git checkout HEAD <file> <!-- Alternative for delete all staged and unstaged changes to one. (Legacy command) -->

48
 
49
git reset --hard <!-- This command DELETE ALL staged and unstaged changes. Use with caution! -->
50
git clean <!-- Delete untracked files -->
51
git stash <!-- Stores all staged and unstaged changes temporarily -->
52
 
53
# Edit history
54
 
55
git reset HEAD^ <!-- "Undo" the most recent commit, this keep your working directory the same -->
56
git rebase -i HEAD-6 <!-- This command squash the last 5 commits into one --> 
?

<!-- This command allow you change "pick" to "fixup" for any commit you want to combine with the previos one -->

57
git reflog BRANCHNAME <!-- Undo a failed rebase -->
58
git reset --hard <commit> <!-- Then manually find the right commit ID in the reflog and run this command -->
59
git commit --amend <!-- You can change a commit message (or add a file you forgot) -->
60
 
61
# Logs
62
 
63
git log main <!-- Look a main history --> 
?

git log --graph main <!-- Displays branches and merge history visually -->

git log --oneline <!-- Shows a compact, one-line commit history -->

64
git log <file> <!-- Show every commit that modified a file --> 
?

git log --follow <file> <!-- Shows the commit history of a file, even if it was renamed -->

65
git log -G example <!-- Find comits that changed specific text -->
66
git blame <file> <!-- Show who last edited each line -->
67
 
68
# Combine branches
69
 
70
git switch <branchName> <!-- First step for combine with rebase -->
71
git rebase main <!-- Second step for combine with rebase -->
72
 
73
git switch main <!-- First step for combine with merge -->
74
git merge <branchName> <!-- Second step for combine with merge -->
75
 
76
git switch main <!-- First step for combine with squash merge -->
77
git merge --squash <branchName> <!-- Second step for combine with squash merge -->
78
git commit <!-- Third step for combine with squash merge -->
79
 
80
git switch main <!-- First step for "fast-forward merge" -->
81
git merge <branchName> <!-- Second step for "fast-forward merge" --> 
?

<!-- A fast-forward merge is possible only when the main branch has not diverged since the banana branch was created. -->

82
 
83
git cherry-pick <commit> <!-- Applies the changes from a specific commit to the current branch -->
84
 
85
# Restore
86
 
87
git restore <file> --source <commit> <!-- Command for restore a file from another commit --> 
?

git checkout <commit> <file> <!-- Legacy command for restore a file from another commit -->

88
 
89
# Add a remote and push
90
 
91
git remote add origin <url> <!-- Add a remote -->
92
git push origin main <!-- Push the main branch to remote origin --> 
!

git push <!-- Push the current branch to its remote "tracking branch" -->

git push -u origin main <!-- Push a branch that you've never pushed before -->

git push --force-with-lease <!-- Force push -->

git push --tags <!-- Push tags -->

93
 
94
# Pull changes
95
 
96
git fetch origin main <!-- Fetch changes, but dont change any of your local branches -->
97
git pull --rebase <!-- Fetch changes and then rebase your current branch -->
98
git pull origin main <!-- Fetch changes and then merge them into your current branch --> 
?

<!-- The first command is most secure and safe, but the second command is faster -->

git pull <!-- Alternative to changes and then merge them into your current branch. -->

99
 
100
# Configure Git
101
 
102
git config user.name <Your Name> <!-- Set a config option -->
103
git config --global ... <!-- Set option globally -->
104
git config alias.st status <!-- Add an alias -->
105