Git And Github

  • Git
  • Why Use Git?
  • Github
  • Why Use Github?
  • Workflow On Git
  • Branching Merging And Conflicts
  • Basic Git Commands
  • Useful Hints

Git

Git is a version control system i.e software that is used for tracking the changes in any set of files over time so that we can go through the specific versions later. It is usually used for coordinating work among the programmers and collaboratively developing source codes during software development. It was created by Linus Torvalds in 2005. Git is open-source software distributed under GNU General Public License.

Why Use Git?

  • Go to the earlier version when needed
  • One can undo mistakes
  • Things don't mix up since it provides the feature of branching
  • Distributed development i.e. multiple persons can work
  • Community Support, since there are more than 40 million users over the globe so we can easily get help when getting stuck

Github

Github is an online platform that provides hosting services to the git repositories. Suppose a team is working on a project, then to collaborate on a git-enabled repository, they need to keep a copy of the project at a central place so that anyone can access it. Such a central platform is called the host and Github is a host. Hence, Git is a tool and Github is a service.

Why Use Github?

  • It acts as a central platform so that one can access the files easily
  • Github also act as a backup for our files, in case our machine gets destroyed.

20210301_012119.jpg

Workflow In Git

There are three stages or areas in Git which are as follows:

  • Working Area
  • Staging Area
  • Committed Area

Working Area:

It is also known as an untracked area i.e. files are not tracked in this area. Here we can create files, update them as well as delete them.

Staging Area:

It is also known as the index stage. It is the preparation area before we add the changes to the committed area. From this area, the tracking of the files is started. Although, we can still make the changes to the files, and if we do so we will go back to the untracked changes.

Commited Area:

We make a commit with a message that tells what changes we had made in the files. We are committing, it means that we are making a version or a checkpoint i.e. we can access it in the future even after the next commit.

20210301_011934.jpg

Branching, Merging, And Conflicts

Branching:

Branches are used to develop features isolated from each other. The default branch is the "master" branch whenever we create a repository. Other branches can be made whenever needed.

Merging:

We use other branches for making any changes and add them back to master upon completion. The process of adding the new modified branch to the master branch is called merging.

Conflicts And Resolving Them:

After passing the commands to merge, git tries to auto-merge the changes. Unfortunately, this is not always possible and sometimes results in conflicts. We are responsible to merge those conflicts manually by editing the files shown by Git. After changing we need to mark them as merged.

Before merging the files we should preview them.

20210301_013350.jpg

Basic Git Commands

  • git --version = To check whether git is installed, if installed then it tells its version
  • mkdir <FolfderName> = To make a repository on local machine
  • touch <FileName> = To add a file in the repository
  • git init = To initializing the Git repository
  • git add <FileName> = To add particular file to the staging area
  • git add . = To add the whole folder to the staging area
  • git rm -cached <FileName> = To remove file from the staging area
  • git commit -m "Message"= To add changes in the committed area with a message
  • git commit -am "Message" = To add files from the working tree to the committed area directly with a message
  • git status = To check the status of a repository
  • git log = To check the commit history
  • git checkout <CommitHash> = To go back in a state of working tree in a particular commit
  • git restore --staged <FileName> = To move a file from the staging area to the working area
  • git branch <BrabchName> =To create a new branch
  • git checkout <BranchName> =To switches to an named branch
  • git merge <BranchName> =To merge the named branch in the current branch
  • git branch -d <BranchName> =To delete the named branch
  • git checkout -b <BranchName> = To create and switch to the named branch
  • git clone <URL> = To clone remote repository to local machine
  • git pull = To update your local repository to the newest commit
  • git push origin master = To move files to the master remote repository on the Github
  • git push origin <BranchName> =To move files to the branch remote repository on the Github
  • git checkout -b hotfix-<BranchName> = To create an temporary repository
  • git diff <SourceBranch> <TargetBranch> = To preview files before merging

Useful Hints

  • gitk = Built-in git GUI
  • git config color.ui true = To use colourful git output
  • git config format.pretty oneline = Show log on just one line per commit
  • git add-i = Use interactive adding
  • git log --help = To get help
  • git log --name-status = To see only those files which have changed
  • git log --graph --oneline --decorate --all = If we wanna see an ASCII art tree of all the branches, decorated with the names of tags and branches



Images taken from Google.
Your suggestions and feedbacks are appreciated. You can write back to me on: