Working With Git

Important Commands

  1. git init: Initialize a git repository,

  2. git status: Check the status of any directory whether it's a git repo or not,

  3. git add file_name: Add the given file in the staging area,

  4. git add -A: Add all files changed files into the staging area,

  5. git add --a: Add all files changed files into the staging area,

  6. git restore --staged file_name: Unstage the given file,

  7. git restore file_name: Make the given file unmodified and restore the changed data.

  8. git commit -m "Commit Message": Commit all the staged files,

  9. git commit -a -m "Commit Message": Commit all the tracked files by skipping the staging area,

  10. git log: List all the previous commits,

  11. git log -p: List all the previous commits with changes,

  12. git log -p -n: List n number of previous commits with changes,

  13. git log --stat: List all the previous commits with short info about changes,

  14. git log --prety=oneline: List all the previous commits in one line,

  15. git log --prety=short: List all the previous commits with short info about changes,

  16. git log --since=n.days: List all the previous commits in the previous n days,

  17. git log --since=n.weeks: List all the previous commits in the previous n weeks,

  18. git log --since=n.months: List all the previous commits in the previous n months,

  19. rm -rf .git: Remove the .git file (i.e. Now the directory will not be a git repo)

  20. git clone repo_url: Clone the repository,

  21. git clone repo_url repo_name: Clone the repository and give the new name to the repository,

  22. git diff: Compare the staging area with the working directory,

  23. git diff --staged: Compare the previous commit with the current stating area,

  24. git rm file_name: Delete a file, and stage the change,

  25. git mv file_name_1 file_name_2: Rename/Move the file, and stage the change,

  26. git rm --cached file_name: Untrack the given file,

  27. git checkout -- file_name:

  28. git remote add remote_name remote_url: Add remote to your git repository

  29. git remote:

  30. git remote -v:

  31. git config --global alias.st status

Branching

  • git checkout -b branch_name: Create a new branch and switch to that new branch.

  • git checkout branch_name: Switch to branch

  • git branch: List all the branches