Essential Git Commands: A Quick Reference Guide



SETUP & INIT

Command Description  
$ git init Initialize a local directory as a Git repository  
$ git clone [url] <local_name> Get an entire repository from a remote
and place on your local computer under a new name (optianal)
 
$ git clone --recurse-submodules Get a repository with submodules from the remote  
$ git clone [url] <local_name>
$ cd <local_name>
$ git submodule init
$ git submodule update
Get a repository with submodules from the remote
(when forgot to clone with submodules)
 
or
$ git clone [url] <local_name>
$ cd <local_name>
$ git submodule update --init
   
$ git remote add <new_name> [url]
Ex.: $ git remote add upstream [url]
Add new remote repository
:: Add new “upstream” repo link
 
Fork a repo:
$ git clone [url] <local_name>
$ cd <local_name>
$ git remote add upstream [url]
$ git pull upstream main

:: Create a new empty repo on a remote and clone it to local
:: Switch to the local working directory
:: Add the source repo as upstream
:: Pull the changes from the remote source repo
 

CHECK IT UP

Command Description
$ git status Check the working stage
$ git branch Check the list of branches
$ git branch -vv
Ex.: *main <..> [origin/main] <..>
Check what main branch tracks
:: Main local branch tracks remote repo “origin” branch “main”
$ git config -l Check your settings
$ git remote -v
Ex.:
origin [url] (fetch)
origin [url] (push)
upstream [url] (fetch)
upstream [url] (push)
Check remote repositories

:: Two remote repos: “origin” and “upstream”
$ git log Check the history of commits
$ git log --merge Check logs of merge process
$ git diff Check difference

SIMPLE PUSH

Command Description
$ git add . Specify files to be committed
$ git commit -m “Message Make a commit
$ git pull Download changes from a remote repo
$ git push [remote_repo] <branch_name>
Ex.:
$ git push origin main
$ git push upstream main
Upload changes to a remote repo

:: Push to remote repo “origin” branch “main”
:: Push to remote repo “upstream” branch “main”
or
$ git push -u origin main (once)
$ git push (all next times)
Upload changes to a remote repo “origin” branch “main” and set this reference for future

GLOBAL .gitignore

Command Description
1. $ nano ~/.gitignore_global Create global gitiglore file (in the root)
2. $ git config --global core.excludesfile ~/.gitignore_global Set to use this file as the global gitignore file
3. $ git config --global core.excludesfile Confirm that this file is configured to be used
4. (Optional) $ git rm --cached -r <what-to-ignore> Remove all before tracked from cach

SUBMODULES

Git Docs

Command Description
$ git submodule add [url] Add a new repo as our submodule
$ git submodule update --remote <submodule_name> Update the submodule to the latest changes from the remote

CORRECT MISTAKES

Command Description
$ git add rm -r <file> Remove “file” from “add”
$ git commit --amend -m “New comment Change the last committed comment (typo correction)

BRANCHES

Command Description
$ git checkout -b <branch_name> Create and switch to a new branch
$ git push origin <branch_name> Upload changes from the local branch to the remote branch on the “origin” repo
$ git push -u origin <branch_name>
Next times: $ git push
Upload changes from the local branch to a remote repo “origin/branch” and set this reference for future
$ git checkout <branch_name>
$ git merge <main_branch_name>
Take changes from a main branch
$ git checkout <main_branch_name>
$ git branch -d <branch_name>
Delete branch
$ git stash
$ git checkout <branch_name>
Save uncommitted changes without committing them and
switch to another branch
$ git stash show Show a summary of what changes are in a stash
$ git stash clear Delete the whole stash
$ git checkout <branch_name>
$ git stash pop
Return to the branch and
restore saved uncommitted changes
$ git restore <file_name> Return the file to its original (last committed) version
$ git restore --staged <file_name> Return the file to its original version if the file has already been added to the stage using git add
$ git checkout <main_branch_name>
$ git pull
$ git merge <branch_name>
Merge branch to the main branch