Skip to content

Git

Install

Install git using homebrew with the following command:

brew install git

Configuration

Set username

git config --global user.name "[Firstname Lastname]"

Set email

git config --global user.email "[EmailId]"

Set credential helper

git config --global credential.helper store

This will store your credentials in a plain text file at ~/.git-credentials.

Usage

Init

Initialize got tracking inside current directory

git init

Initialize git tracking inside a new directory

git init [directory]

Clone

Clone a repository

git clone [url]

Clone only a specific branch

git clone --branch [branch] [url]

Clone a repository to a specific directory

git clone [url] [directory]

Branch

Create a new branch

git branch [branch]

Create a new branch and switch to it

git checkout -b [branch]

Switch to a branch

git checkout [branch]

Delete a branch

git branch -d [branch]

Delete a branch forcefully

git branch -D [branch]

Merge a branch

git merge [branch]

Merge a branch with a message

git merge [branch] -m "Message"

Add

Add a file to staging area

git add [file]

Add all files to staging area

git add .

Stage all files except a specific file

git add -u
git git reset HEAD main/dontcheckmein.txt #or
git restore --staged main/dontcheckmein.txt

Remove a file from staging area

git rm [file]

Commit

See the changes in the staging area

git status

Saving the staged changes with a message

git commit -m "Message"

Saving the staged changes with a message and adding all changes

git commit -am "Message"

Edit the last commit message

git commit --amend -m "New Message"

Saving

Saving stage and unstaged changes

git stash

Saving stage and unstaged changes with a message

git stash save "Message"

Saving stage and unstaged changes with a message and include untracked files

git stash -u save "Message"

Apply the last stash

git stash apply

Apply a specific stash

git stash apply stash@{n}

Apply the last stash and remove it from the list

git stash pop

Remove the last stash

git stash drop

Remove a specific stash

git stash drop stash@{n}

Diff

See the changes between the working directory and the staging area

git diff

See the changes between 2 commits

git diff [commit1] [commit2]

Push to a previous commit

There are 2 ways of doing this

Reset

Use with caution

Use this wisely as this would manipulate the git history and could very well jeopardise everything for you.

git reset [commitId] --hard && git push --force
  • --soft - Moves the HEAD to the commitId and keeps the changes in the staging area
  • --mixed - Moves the HEAD to the commitId and keeps the changes in the working directory
  • --hard - Moves the HEAD to the commitId and discards all changes
  • --merge - Moves the HEAD to the commitId and keeps the changes in the staging area and working directory
  • --keep - Moves the HEAD to the commitId and keeps the changes in the working directory
Read more

Simon Dosda's Post

Revert

git revert --no-commit [commitId1 commitId2] && git push

GitHub via SSH

Generate a new SSH key

ssh-keygen -t ed25519 -C "[EMAIL ID]"

Add SSH key to your GitHub account

Cheat Sheet

Download Cheat Sheet

Back to top