Retour aux articles

🐙 Git Memo: All Essential Commands

A complete list of essential Git commands: configuration, project creation, snapshots, branching, merging, and sharing. A practical reference guide to keep handy.

Level Sony
Git CLI GitHub
🐙 Git Memo: All Essential Commands
Table des matières

A list of Git commands — feel free to come back to it as often as you need.


Git Overview

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Here’s a summary of the most useful commands, organized by category.


Tell Git Who You Are

First things first, set up your identity so Git knows who is making commits.

DescriptionCommand
Set the author namegit config --global user.name "<username>"
Set the author email addressgit config --global user.email <email address>

Getting & Creating Projects

DescriptionCommand
Initialize a local Git repositorygit init
Create a local copy of a remote repositorygit clone ssh://git@github.com/<username>/<repository-name>.git

Basic Snapshotting

DescriptionCommand
Check statusgit status
Add a file to the staging areagit add <file-name.txt>
Add all new and changed files to the staging areagit add -A or git add .
Commit changesgit commit -m "<commit message>"
Remove a file (or folder)git rm -r <file-name.txt>

Inspection & Comparison

DescriptionCommand
View changesgit log
View changes (detailed)git log --summary
View changes (briefly)git log --oneline or git log --pretty=oneline or git log --pretty=short

Undoing Changes

DescriptionCommand
List all commits (with commit id and message)git log --oneline
Go back to a previous commitgit checkout <commit id>
Undo a specific commitgit revert <commit id>
Reset to a previous commit (removes all history after)git reset <commit id>
Stop tracking a filegit rm --cached <file/folder>
Restore a file to a previous commitgit checkout <file/to/restore>

Branching & Merging

DescriptionCommand
List branches (asterisk denotes the current branch)git branch
List all branches (local and remote)git branch -a
Create a new branchgit branch <branch name>
Create a new branch and switch to itgit checkout -b <branch name>
Clone a remote branch and switch to itgit checkout -b <branch name> origin/<branch name>
Rename a local branchgit branch -m <old branch name> <new branch name>
Switch to a branchgit checkout <branch name>
Switch to the last checked out branchgit checkout -
Discard changes to a filegit checkout -- <file-name.txt>
Delete a branchgit branch -d <branch name>
Delete a remote branchgit push origin --delete <branch name>
Preview changes before merginggit diff <source branch> <target branch>
Merge a branch into the active branchgit merge <branch name>
Merge a branch into a target branchgit merge <source branch> <target branch>
Stash changes in a dirty working directorygit stash
Remove all stashed entriesgit stash clear

Sharing & Updating Projects

DescriptionCommand
Push a branch to your remote repositorygit push origin <branch name>
Push changes to remote repository (and remember the branch)git push -u origin <branch name>
Push changes to remote repository (remembered branch)git push
Delete a remote branchgit push origin --delete <branch name>
Update local repository to the newest commitgit pull
Pull changes from remote repositorygit pull origin <branch name>
Add a remote repositorygit remote add origin ssh://git@github.com/<username>/<repository-name>.git
Set a repository’s origin branch to SSHgit remote set-url origin ssh://git@github.com/sony-level/<repository-name>.git

Commentaires