Git Quick Reference Guide

This is a quick reference guide about Git so I can have it at hand. I took the data from the official documentation located at: https://git-scm.com/docs

CommandDescriptionCommon VariantsExample
git initInitializes a new Git repository in the current directory.git init
git cloneCopies a repository from a remote location to your local machine.git clone
<repo-url> --branch <branch-name>
git clone https://github.com/user/repo.git
git statusShows the current state of the working directory and the staging area.git status
git addAdds changes from the working directory to the staging area.git add .
(adds all changes)
git add filename.txt
git restore –staged .The recommended modern way to undo git add is using git restoregit restore --staged .
git remote -vThis will show you the list of remotes configured for your repository. Typically, the default remote is called ‘origin’.git remote -v
git remote addAdds a new remote repository URL.git remote add <name> <url>git remote add origin https://github.com/user/repo.git
git remote removeRemoves remotegit remote remove <name>git remote remove main
git commitSaves changes from the staging area to the repository with a descriptive message.git commit -am "Message"
(adds and commits)
git commit -m "Initial commit"
git pushUploads local repository changes to a remote repository.

It can also delete a remote branch

Command Breakdown

git push: The basic command to upload local repository content to a remote repository

origin: This is the default name for the remote repository you cloned from or added as a remote 

master:main: This syntax specifies pushing from your local master branch to the remote main branch
git push origin <branch>

————————

git push <remote_name>--delete <branch_name>
1) git push origin main

2) git push origin master:main

3) git push origin --delete feature-branch
git pullFetches and integrates changes from a remote repository into the local branch.git pull --rebase (rebases instead of merging)1) git pull origin main

2) git pull origin main:master
git fetchDownloads updates from a remote repository but does not merge them.git fetch origin
git mergeCombines changes from different branches into the current branch.git merge
feature-branch
git branchLists, creates, or deletes branches in the repository.

It will show an asterisk (*) next to the current branch.
git branch -d branch-name
(delete branch)

————————-

git branch -a
(list all branches including remotes)
git branch new-feature

git checkoutSwitches between branches or restores files.git checkout -b new-branch
(create and switch)
git checkout main
git logDisplays a history of commits in the repository.git log --graph (show commit graph)git log --oneline
git diffShows the differences between files in the working directory and the repository.git diff --staged (show staged changes)git diff HEAD
git resetResets the state of the working directory to a previous commit.git reset HEAD <file>
(unstage changes)
git reset --hard HEAD~1
(It effectively “undoes” the last commit and all changes since then)
———————-
git reset --soft HEAD~1
(moves the HEAD and branch pointer back one commit, but keeps the changes in the staging area)
git restoreAllows us to abandon all changes since the last commit in a specific filegit restore .git restore filename.txt
git rebaseReapplies commits on top of another base tip, used to clean up commit history.git rebase -i HEAD~3
(interactive rebase)
git rebase main
git rmRemoves files from the working directory and the index (staging area).git rm --cached filename.txt (unstage file)git rm filename.txt

Share