Git
Git is a distributed version control system that tracks changes in any set of computer files
Basic commands
Start a new repository
git init <repository name>
Add remote
git remote add <remote-name> <repo-link>
Clone a repository
git clone <url>
Stage all files
git add .
Commit staged files
git commit -m "<commit-message>"
Check the current status of files
git status
Remove recent changes, only in one file
git restore <filename>
Remove all recent changes
git reset --hard
Clean all files and folders
git clean -df
Fetch changes from remote
git fetch <remote> <branch>
Cherry pick commits from any branch
git cherry-pick <commit-hash>
Cherry pick range of commits
git cherry-pick <old-commit-hash>..<new-commit-hash>
Check logs
git log
Check logs with commit hash
git reflog
Check diff of last commit
git diff HEAD^..HEAD
Check logs - only last commit details
git log --name-status HEAD^..HEAD
Rebase before merging to have a clean history
git rebase -i <branch-to-be-merged OR head-commit-hash>
git push --force-with-lease
Merge branch into current branch
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
git push <remote-name> <branch-name>
Force push
git push -f <remote> <branch>
Force push (safer option)
git push <remote> <branch> --force-with-lease
Fetch force pushed changes
git fetch; git reset --hard FETCH_HEAD
Push few commits only
git push <remote> <commit-hash>:refs/heads/<branch>
Rebase
Start interactive rebase
git rebase -i <commit-hash>
To advance interactive rebase
git rebase --continue
To abort interactive rebase
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
git lfs install
git lfs uninstall
Track
git lfs track "*.<filetype>"
git lfs track --finename "<filename.filetype>"
Fetch all
git lfs fetch --all
Pull LFS
git lfs pull
Push LFS
git lfs push <remote> <branch> --all
Delete
Delete local branch
git branch -d <branch-name>
Delete remote branch
git push origin --delete <branch-name>
Create
Create local branch
git checkout -b <new-branch-name> <source-branch>
Amend
Amend last commit
git commit --amend