Git: Cheat Sheet
Last updated on
Basic Commands:
- git init: Initializes a new git repository
- git clone [repo]: Clones a remote repository to your local machine
- git status: Shows the status of the current repository (modified files, untracked files, etc.)
- git add [file]: Adds a file to the staging area, ready for commit
- git commit -m "[message]": Commits changes with a message describing the changes
- git push: Pushes committed changes to the remote repository
- git pull: Fetches changes from the remote repository and merges them into the local branch
Branching:
- git branch: Lists all branches in the repository
- git branch [branch name]: Creates a new branch with the given name
- git checkout [branch name]: Switches to the specified branch
- git merge [branch name]: Merges changes from the specified branch into the current branch
- git branch -d [branch name]: Deletes the specified branch
Reverting Changes:
- git reset [file]: Removes a file from the staging area
- git revert [commit hash]: Reverts changes made in a specific commit
- git checkout [file]: Discards changes made to a file
Advanced Commands:
- git stash: Temporarily saves changes without committing them
- git stash pop: Restores changes from the last stash
- git remote: Lists all remote repositories connected to the local repository
- git remote add [remote name] [remote URL]: Adds a new remote repository
- git fetch [remote name]: Fetches changes from a remote repository without merging them
Tips:
- Always add a descriptive commit message when committing changes
- Use branches for different features or bug fixes to keep your code organized
- Use git pull regularly to stay up-to-date with changes made by other contributors.