3git init <!-- Initialize a new Git repository -->
4git clone <url> <!-- Clone a Git repository -->
8git 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 -->
9git mv <file> <new-file> <!-- Move a file -->
10git rm <file>
<!-- Remove a file --> !git rm --cached <file> <!-- Git forget about a file without deleting it -->
12git reset <file>
<!-- Unstage everything --> !<!-- This command unstage EVERYTHING, if you want to unstage a specific file, use: --> git reset <file>
14git status <!-- Check what you added to your staging area -->
18git commit -m <message>
<!-- Make a commit --> ?<!-- If you modified existing files and want to commit quickly, use: --> git commit -am <message>
22git switch -c <branch>
<!-- Create and switch to a new branch --> !git checkout -b <branch> <!-- Create and switch to a new branch -->
23git switch <branch>
<!-- Switch to a existing branch --> !git checkout <branch> <!-- Switch to an existing branch (legacy command) -->
25git branch <!-- List all branches -->
26git branch --sort=-committerdate <!-- List branches by most recently committed -->
28git branch -d <branch>
<!-- Delete a branch --> !git branch -D <branch> <!-- Force delete a branch -->
30# Diff staged and unstaged changes
32git diff <!-- Show unstaged changes -->
33git diff --staged <!-- Show staged changes -->
34git diff HEAD <!-- All changes since last commit -->
38git diff show <commit> <!-- Show diff between a commit and its parent -->
39git diff <commit1> <commit2> <!-- Show changes between two commits -->
40git diff <commit> <file> <!-- Diff one file since a commit -->
42git diff <commit> --stat
<!-- This command show a summary of a diff --> ?git show <commit> <!-- Show changes between working directory and staging area -->
46git restore <file>
<!-- Delete unstaged changes on the file. --> !git checkout <file> <!-- Legacy command for discard changes -->
47git 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) -->
49git reset --hard <!-- This command DELETE ALL staged and unstaged changes. Use with caution! -->
50git clean <!-- Delete untracked files -->
51git stash <!-- Stores all staged and unstaged changes temporarily -->
55git reset HEAD^ <!-- "Undo" the most recent commit, this keep your working directory the same -->
56git 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 -->
57git reflog BRANCHNAME <!-- Undo a failed rebase -->
58git reset --hard <commit> <!-- Then manually find the right commit ID in the reflog and run this command -->
59git commit --amend <!-- You can change a commit message (or add a file you forgot) -->
63git 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 -->
64git 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 -->
65git log -G example <!-- Find comits that changed specific text -->
66git blame <file> <!-- Show who last edited each line -->
70git switch <branchName> <!-- First step for combine with rebase -->
71git rebase main <!-- Second step for combine with rebase -->
73git switch main <!-- First step for combine with merge -->
74git merge <branchName> <!-- Second step for combine with merge -->
76git switch main <!-- First step for combine with squash merge -->
77git merge --squash <branchName> <!-- Second step for combine with squash merge -->
78git commit <!-- Third step for combine with squash merge -->
80git switch main <!-- First step for "fast-forward merge" -->
81git 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. -->
83git cherry-pick <commit> <!-- Applies the changes from a specific commit to the current branch -->
87git 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 -->
89# Add a remote and push
91git remote add origin <url> <!-- Add a remote -->
92git 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 -->
96git fetch origin main <!-- Fetch changes, but dont change any of your local branches -->
97git pull --rebase <!-- Fetch changes and then rebase your current branch -->
98git 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. -->
102git config user.name <Your Name> <!-- Set a config option -->
103git config --global ... <!-- Set option globally -->
104git config alias.st status <!-- Add an alias -->