Skip to content

Git

Git is a distributed version control system that tracks changes in any set of computer files

Basic commands

Start a new repository

sh
git init <repository name>

Add remote

sh
git remote add <remote-name> <repo-link>

Clone a repository

sh
git clone <url>

Stage all files

sh
git add .

Commit staged files

sh
git commit -m "<commit-message>"

Check the current status of files

sh
git status

Remove recent changes, only in one file

sh
git restore <filename>

Remove all recent changes

sh
git reset --hard

Clean all files and folders

sh
git clean -df

Fetch changes from remote

sh
git fetch <remote> <branch>

Cherry pick commits from any branch

sh
git cherry-pick <commit-hash>

Cherry pick range of commits

sh
git cherry-pick <old-commit-hash>..<new-commit-hash>

Check logs

sh
git log

Check logs with commit hash

sh
git reflog

Check diff of last commit

sh
git diff HEAD^..HEAD

Check logs - only last commit details

sh
git log --name-status HEAD^..HEAD

Rebase before merging to have a clean history

sh
git rebase -i <branch-to-be-merged OR head-commit-hash>
git push --force-with-lease

Merge branch into current branch

sh
git merge <branch>
OR
git merge <branch-to-be-merged> -m "merge message"
OR
git merge --ff-only <branch-to-be-merged>

Push

Push commited changes

sh
git push <remote-name> <branch-name>

Force push

sh
git push -f <remote> <branch>

Force push (safer option)

sh
git push <remote> <branch> --force-with-lease

Fetch force pushed changes

sh
git fetch; git reset --hard FETCH_HEAD

Push few commits only

sh
git push <remote> <commit-hash>:refs/heads/<branch>

Rebase

Start interactive rebase

sh
git rebase -i <commit-hash>

To advance interactive rebase

sh
git rebase --continue

To abort interactive rebase

sh
git rebase --abort

Type :x to execute interactive rebase

Use Pick to pick commits

Use Drop to drop commits

Use Edit to edit commits

LFS

Install/Uninstall LSF

sh
git lfs install
git lfs uninstall

Track

sh
git lfs track "*.<filetype>"
git lfs track --finename "<filename.filetype>"

Fetch all

sh
git lfs fetch --all

Pull LFS

sh
git lfs pull

Push LFS

sh
git lfs push <remote> <branch> --all

Delete

Delete local branch

sh
git branch -d <branch-name>

Delete remote branch

sh
git push origin --delete <branch-name>

Create

Create local branch

sh
git checkout -b <new-branch-name> <source-branch>

Amend

Amend last commit

sh
git commit --amend