Joel's dev blog

Using git

March 21, 2018

2 min read

How to revert

git revert <SHA>

git revert will create a new commit that’s the opposite (or inverse) of the given SHA. If the old commit is “matter”, the new commit is “anti-matter”—anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit.

How to revert only one file

git checkout c5f567 -- file1/to/restore file2/to/restore
  • when you want to go back to the commit right before the hash
git checkout c5f567~1 -- file1/to/restore file2/to/restore

just add ~anyNum after the hash.

How to discard changes in the working directory

git stash save --keep-index (--include-untracked if you want to include untracked files as well) 

or

git checkout -- .

for specific files to discard, use:

git checkout path/to/file/to/revert

How to add all files except one file/folder

git add -u
git reset -- oneFile.file
git reset -- folder/* (for a folder)

How to switch back from and forth to a branch

Create new branch and let it point to the master branch

$ git checkout -b iss53
Switched to a new branch "iss53"

equivalent:

$ git branch iss53
$ git checkout iss53

See branches and their last commits

git branch -v

Merge to master

git checkout master # first point the HEAD to master

git merge anotherBranch

Delete branch

git -d branchName

How to clear cache on specific file

git rm --cached fileName

Add username for every repo on computer

git config --global user.name "username"

Add username for single repo on computer

just remove global flag from above. 

Enable credential caching (password)

git config credential.helper store

Written by Joel Mun. Joel likes Typescript, React, Node.js, GoLang, Python, Wasm and more. He also loves to enlarge the boundaries of his knowledge, mainly by reading books and watching lectures on Youtube. Guitar and piano are necessities at his home.

© Joel Mun 2023