Git Cheat Sheet for Beginners
| Use case | Command | Notes |
|---|---|---|
| Initiating Git Use case: Onboard your project on git repository | ||
| Create a new local git repository | git init | Creates a local repository with a default master branch |
| Status | ||
| Get current working directory status | git status | Reports branch status against last committed state along with staged or modified files |
| Get history of commits | git log --graph --oneline --all | Use with --graph for a graphical view |
| Stage/Unstage modified files | ||
| Stage a file | git add <filename> | |
| Unstage a file | git restore --staged <filename> | |
| Revert changes to modified file | git restore <filename> | Use with caution, once reverted git doesn’t retain history |
| Stage all modified files | git add . | Alterntively use git add --all |
| Unstage all modified files on a given path | git restore --staged <path> | |
| Commit/Amend staged files Use case: Commit your work to git repository | ||
| Commit all staged files to local repository | git commit -m <message> | -m switch is for message |
| Add a missed file to previous commit | git add <filename> git commit --amend | |
| Working with remote repository Use case: Integrate with remote repository | ||
| Check remote repositories names | git remote -v | Shows name with url and permissions |
| Clone a remote repository | git clone <url> | |
| Add a remote branch | git remote add <remote_name> <url> | |
| Fetch from remote branch | git fetch | Fetches data from remote server, but doesn’t merge with any local modifications |
| Fetch and automatically merge remote branch changes with local | git pull | |
| Push to remote branch | git push <remote_name> <branch_name> | |
| Rename a remote branch | git remote rename <current_name> <new_name> | |
| Remove a remote branch | git remote remove <branch_name> | |
| Working with branches | ||
| Find your current branch | git branch | |
| Create a new local branch | git branch <branch_name> | This command creates a new branch but doesn’t switch automatically |
| Switch to local branch | git switch <branch_name> | |
| Create and switch to new local branch | git switch -c <branch_name> | |
| Delete a local branch | git branch -d <branch_name> | |
| Merge branch to current branch | git merge <branch_nam> | Works well assuming there are no merge conflicts. A more detailed cheatsheet on branching strategies to follow |
| Stashing changes Use case: Your work is not yet ready to commit, and I need to switch branch or work on something else now | ||
| Stash a change | git stash | Alternatively use git stash push |
| Get list of stashed changes | git stash list | |
| Apply most recent stash to working directory | git apply stash | Use with --index switch to revert to staging stage. By default all files will only be reverted to modified state |
| Apply a specific stash to working directory | git apply stash <stash_id> | |
| Drop a stash | git stash drop <stash_id> | git stash pop command combines apply and drop together |
| Move stashed work to a new branch | git stash branch <branch_name> | Useful in case of merge conflicts |