Git Command Cheatsheet
2024.08.22
2๋ถ
Table of contents
- Create a New Repository
- Recover on Origin changes
- Stash changes
- List all stashes
- Apply the latest stash
- Drop the latest stash
- Create a new tag
- List all tags
- Delete a tag
- Push tag to remote
- Show changes between working directory and index
- Show changes between working directory and last commit
- List all remote repositories
- Add a new remote repository
- Remove a remote repository
Create a New Repository
$ git init
$ git add --all
$ git commit -m "First draft"
$ git remote add origin https://github.com/<user>/<repo>.git
$ git push -u origin master
Working with local changes
$ git status
$ git add --all
$ git add -p <file>
$ git commit -m "Commit message here"
View History
$ git log
$ git log -p <file>
$ git blame <file>
Branching and Merging
$ git branch -av
$ git branch <new-branch>
$ git checkout <branch>
$ git merge <branch>
$ git branch -d <branch>
$ git branch -D <branch>
Undoing Changes
$ git reset --hard HEAD
$ git checkout HEAD <file>
$ git revert <commitId>
Recover on Origin changes
$ git reset --hard HEAD^
$ git reset --hard <commitId>
$ git push --force
Clone Repository
$ git clone https://github.com/<user>/<repo>.git
Pull Repository
$ git pull origin <branch>
Push Repository
$ git push origin <branch>
Stash
# Stash changes
$ git stash
# List all stashes
$ git stash list
# Apply the latest stash
$ git stash apply
# Drop the latest stash
$ git stash drop
Tagging
# Create a new tag
$ git tag <tag-name>
# List all tags
$ git tag
# Delete a tag
$ git tag -d <tag-name>
# Push tag to remote
$ git push origin <tag-name>
View Differences
# Show changes between working directory and index
$ git diff
# Show changes between working directory and last commit
$ git diff HEAD
Working with Remotes
# List all remote repositories
$ git remote -v
# Add a new remote repository
$ git remote add <name> <url>
# Remove a remote repository
$ git remote remove <name>