What is Git ?

Git

It is a free and open source version control system that tracks changes in any set of computer files, mostly code for the collaborative development of software.

GitHub offers a cloud-based Git repository hosting service, another cloud-based service is gitlab.

About

  1. It is a tool not a programming language.

  2. It is a distributed and actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.

  3. It makes collaboration easy. ie. separate branches for testing separate features, rollback changes on requirement etc.

  4. Can be hosted remotely using Github, Gitlab etc.

Git Architecture

Staging Area: the staging area gives us space to prepare (stage) the changes that will be reflected on the next commit.

Upstream: generally refers to the original repo that we have forked.

Origin: is our own repo on GitHub, clone of the original repo of GitHub.

Some Basic Git commands

  1. git init - to initialise an empty git repository.

  2. git log - to explore the history of repository.

  3. git status - displays status of working directory and staging area.

  4. git add <filename> - to add a file(use . for all the files) to staging area.

  5. git commit -m "" - to commit the changes into the local repository.

  6. git restore --staged <filename> - to remove the file from the staging area.

  7. git reset <reset_code> - all commits above this commit will be deleted.

  8. git branch - to see all the branches.

  9. git checkout <branchname> - to switch to this branch.

  10. git merge <branchname> - to merge the branch to the current branch

  11. git stash - to send the added files to the stash area.

  12. git stash pop - to bring back all files from stash area.

  13. git stash clear - to remove all files from the stash area.

  14. git remote add upstream <link>- to add the upstream link (which can be used to fetch the recent changes)

  15. git remote -v -to see all the remote links attached to the repository.

  16. git fetch - it fetches from origin (our fork).

  17. git fetch upstream - it downloads changes from the original repository from where we forked it and we need to merge it manually.

  18. git merge upstream/<branch_name> - to merge the changes done in remote repository to the local repository.

  19. git pull upstream/<branch_name> - it downloads changes from the original repository and merges it into the current repository.

For all the git commands visit the git documentation here.