Skip to content

Instantly share code, notes, and snippets.

@nasirhafeez
Last active January 16, 2024 06:35
Show Gist options
  • Save nasirhafeez/e450a9b07770e5d0862ea5bc44b746c6 to your computer and use it in GitHub Desktop.
Save nasirhafeez/e450a9b07770e5d0862ea5bc44b746c6 to your computer and use it in GitHub Desktop.
Git Commands

Git Commands

Delete Git Commit History

How To Remove All Git Commit History

git checkout --orphan latest_branch
git add -A
git commit -am "initial commit"
git branch -D main
git branch -m main
git push -f origin main

Review Changes Before Pull

git fetch
git diff main origin/main

Review Changes After Pull

The output of git pull will have something like this: Updating 37b431a..b2615b4. Then we can do:

git diff 37b431a b2615b4

Clone

git clone <repo URL>

Get Cloned Repo Detail

git remote show origin

Store/unstore Credentials Globally

git config --global credential.helper store
git config --global --unset credential.helper

Checkout

git checkout <branch name> (to create a local branch based on a remote branch and make it the local Head branch)

Push

git push -u origin <branch name> (to publish local branch)

Commit

git commit -m “Implement the new login box”

Branches

git branch <branch name> (create a new branch)
git branch [-v] (lists current branches)
git checkout <branch name>
git merge <branch name>
git merge --abort (undo last merge)

Reset

git reset --hard HEAD (discard all uncommitted changes)
git reset --hard 2be18d9 (roll back to the revision whose hash is provided)

Revert

git revert 2b504be (undo the changes in the revision whose hash is provided)

Deleting Branches

git branch -d <branch name> (to delete local branch)
git branch -dr <branch name> (to delete remote branch)

After fresh install

git config --global user.name “John Doe”
git config --global user.email “[email protected]”
git config --global color.ui auto

Staging

git add new-page.html index.html css/*
git rm error.html
git status

Log

git log
git log -p (for details changes including diff)

Stash

git stash (store in stash)
git stash list (display stash list)
git stash pop (apply last stash and delete it from stash)
git stash apply <stashname> (apply specific stash and keep it saved)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment