Git: Cheat Sheet

Git: Cheat Sheet
Photo by Kelly Sikkema / Unsplash

Basic Commands:

  1. git init: Initializes a new git repository
  2. git clone [repo]: Clones a remote repository to your local machine
  3. git status: Shows the status of the current repository (modified files, untracked files, etc.)
  4. git add [file]: Adds a file to the staging area, ready for commit
  5. git commit -m "[message]": Commits changes with a message describing the changes
  6. git push: Pushes committed changes to the remote repository
  7. git pull: Fetches changes from the remote repository and merges them into the local branch

Branching:

  1. git branch: Lists all branches in the repository
  2. git branch [branch name]: Creates a new branch with the given name
  3. git checkout [branch name]: Switches to the specified branch
  4. git merge [branch name]: Merges changes from the specified branch into the current branch
  5. git branch -d [branch name]: Deletes the specified branch

Reverting Changes:

  1. git reset [file]: Removes a file from the staging area
  2. git revert [commit hash]: Reverts changes made in a specific commit
  3. git checkout [file]: Discards changes made to a file

Advanced Commands:

  1. git stash: Temporarily saves changes without committing them
  2. git stash pop: Restores changes from the last stash
  3. git remote: Lists all remote repositories connected to the local repository
  4. git remote add [remote name] [remote URL]: Adds a new remote repository
  5. 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.