GIT Cheat Sheet
Basics commmands for GIT.
Git Cheat Sheet
Configuration
- Set the username:
git config --global user.name "Your Name"
- Set the email:
git config --global user.email "your.email@example.com"
- Check settings:
git config --list
Starting a Project
- Initialize a new Git repository:
git init
- Clone an existing repository:
git clone <url>
Basic Commands
- Check the status of the repository:
git status
- Add a file to the staging area:
git add <file>
- Add all files to the staging area:
git add .
- Commit changes:
git commit -m "message"
- View commit history:
git log
Branches
- List all branches:
git branch
- Create a new branch:
git branch <branch>
- Switch to a branch:
git checkout <branch>
- Create and switch to a new branch:
git checkout -b <branch>
- Delete a branch:
git branch -d <branch>
Merging
- Merge a branch into the current branch:
git merge <branch> -m <message>
Remote Repositories
- Add a remote repository:
git remote add <remote> <url>
- Push changes to a remote repository:
git push <remote> <branch>
- Pull changes from a remote repository:
git pull <remote> <branch>
- Fetch changes from a remote repository:
git fetch <remote>
Undoing Changes
- Undo the last commit:
git commit --amend
(Changes must be made before the command) - Revert to a previous commit:
git revert <hash>
- Unstage a file from the staging area:
git reset HEAD <file>
- Same for an untracked file:
git rm <file> --cached
- Reset to a specific commit (be careful, this can result in data loss):
git reset --hard <hash>
- Undo changes in the working directory:
git checkout -- <file>
- Undo all changes in the working directory :
git checkout -f
- Export a file outside the working directory:
git show <hash>:<file> > /path/to/export/file
Stash
- Stash changes:
git stash
- Re-apply stashed changes:
git stash apply
- List stashes:
git stash list
- Delete a stash:
git stash drop <stash-id>
- Apply the last stash and then delete it:
git stash pop
Tagging
- Create a tag:
git tag <tag-name>
- Push tags to the remote repository:
git push --tags
Alias
- Allows you to display the repository graph:
git graph
git config --global alias.graph "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%cd)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --date=format:'%y%m%d-%H%M%S' --all"