Logo
Published on

Git Cheat Sheet

Authors
Git

Config

To show configurations list:
git config --list
To modify something:
git config --global user.name "Nazarii"

Setup

Initialize an existing directory as a Git repository
git init
Clone an entire repository from a hosted location via URL
git clone [url]

Stage

Add all files to the 'staging':
git add .
But better to add specific files:
git add test.md

Commit

To commit files (add snapshot)
git commit -m "Initial commit"
Add and commit:
git commit -a-m "Initial commit"
See last commits:
git log
Modify message:
git commit --amend -m "New message"
Modify commit:
Example: You created commit, but forgot about some peace of your-best-codewhile (true) {}😎. Then just add new logic to some file, and after that use git add . and add this changes to the commit with the next command:
git commit --amend --no-edit

Remote

To see remote:
git remote -v
To connect local code to the cloud:
git remote add origin <URL>
To see additional info:
git remote show origin

Push

To push and set upstream remote:
git push origin master -u

Merge

Fetch remote state:
git fetch
Merge remote branch with local branch:
git merge origin/master
Go to original state before merge conflict was started:
git merge --abort

Pull

Combine fetch and merge commands:
git pull origin master

Branch

See all branches:
git branch
Rename current:
git branch -M main
Create new:
git branch feature_1
Delete branch -d soft, -D hard:
git branch -d feature_1
Create and checkout:
git checkout -b production
Go to previous branch:
git checkout -

Reset

Remove files from 'staging':
git reset
Example: Create new file bad.go -> stage it git add bad.go -> commit it git commit -m "Add Bad file". After that you can reset this commit:
git reset <COMMIT_ID>
Reset and delete unwanted files:
git reset --hard <COMMIT_ID>
Safer way of doing this is by using revert - to revert changes, but leave bad commit in commit history:
git revert <COMMIT_ID>

Stash

Hide you local not commit changes:
git stash save <NAME_OF_THE_STASHED_WORK>
Get back stashed changes:
git stash pop
To see stashed list:
git stash list
Apply stash:
git stash apply <INDEX_IN_STASH_LIST>

Rebase

Rebase can help you to keep your feature branch with the master branch, without merge commits
To get new updates from another branch and put your commit on a top - use: rebase**
git rebase master