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

If you wish to reset the HEAD to the main branch, you can use the following command:

git reset --hard origin/main

Revert

You can use the revert command to revert the changes made in a commit. This will create a new commit with the changes reverted. This would be an idle case when you want to keep the git history intact.

The --no-commit flag will revert the changes but not commit them. You can then commit the changes and push them to the remote repository.

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

Make sure that the order of the commitId is in the reverse order of the commits you want to revert. Otherwise, you will get a conflict.

force vs force-with-lease

  • --force - This will force push the changes to the remote repository alterting the git history no matter what
  • --force-with-lease - This will force push the changes to the remote repository only if the remote branch is in the same state as the local branch. If there are any changes in the remote branch, the push will be rejected.

Removing sensitive data from a repository

It is very important when you work for an organization that you do not leak any sensitive information. If you have accidentally pushed any sensitive information to a repository, you can remove it by referring this GitHub Documentation

Working with feature branches

Ideally when working with feature branches you will get into a situtation where you have to merge the changes from the main branch to your feature branch. You can do this by:

  1. Merge - This will merge the changes from the main branch to your feature branch and create a new commit
  2. Rebase with fast forward - This will rebase the changes from the main branch to your feature branch and not create a new commit. This will make the git history linear
  3. Squash - This will squash all the commits from the main branch to your feature branch into a single commit. Note this will create a new commit. We would use this when you want to keep all the changes together in a single commit.
Read more

Checkout this video by ByteByteGo on Git MERGE vs REBASE

When you rebase, you might encounter merge conflicts and these conflicts can re-appear even after you resolve them. This is because the changes are being applied on top of the changes from the main branch. You can remember the merge changes by setting up the git configuration as follows:

git config --global rerere.enabled true

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