Git Commands Basics

Git is a distributed version control system. Like other traditional version control systems it saves the versions of a project with modification details. But unlike others, git maintains a local repository for every project.

There are mainly 3 git stages for a file in a local computer:

  1. Un-tracked : File in working area

  2. Staged: File in staging area

  3. Committed: File committed to local repository

Basic Git Commands

  1. git init: To make a local folder to a git working area.

  2. git status: Used to understand what stage the files in repository at.

  3. git clone <remote_repo_url>: To clone a remote repository into local working area.

  4. git add <file name> : To add a file to staging area.

  5. git commit -m '<message>': Commits the file from staging area to local repository.

  6. git push: Pushes the committed file from local repository to remote repository.

  7. git log: Shows logs of all commit history.

  8. git log --oneline: Shows commit logs in a single line.

  9. git difftool | HEAD: Shows the differences in the files from previous version.

  10. git branch <branch name>: Creates a new branch from the master (A git branch is a copy of complete project where you can add new changes can be made).

  11. git checkout <branch name>: Changes the head to the new branch.

  12. git push origin <branch name>: Pushes the branch changes to remote.

  13. git merge <branch name>: Merges the branch changes to master (remote) branch.

  14. git -d <branch name>: Deletes a branch.

  15. git rebase: Renounce the changes committed in branch, and reflects the master changes in branch.

  16. git revert: Reverts the changes in a commit.

  17. git reset: Deletes commit and changes history.

  18. git show HEAD: To view changes made in the last commit.

  19. git pull origin master: To merge changes from the remote repo.

  20. git checkout <file name>: Replaces the file in the working directory to last committed version.

Git Rules

  1. Create a new Git repository for every new project.

  2. Create a new branch for every new feature.

  3. Delete every branch created after merge.

  4. Write a good descriptive commit message.

  5. Use pull request to merge your code with master.