Git & Github

Git & Github


1. What is Git and why is it important?

Git is a distributed version control system that basically tracks the changes in source code or files under a repository and that source code and repository are maintained by developers or collaborators who they are regularly pushing their changes to the repository.

2. Difference between Git and GitHub?

3. What is the difference Between Main Branch and Master Branch?

There is no functional difference between the main and master branch. When we create a repository, the master or main branch will be our default branch. But some people in the US think or consider that the Master word is offensive (Kind of master-slave). So, Microsoft started using the main as a default branch for Github, as Github is managed by Microsoft whereas others are still using the master as a default branch.

4. Important Terms - Repository, Server, Working directory, Commit, Commit ID, Tags, Snapshots, Push, Pull?

  1. Repository: A repository is a place where you have all the source code and a ".git" hidden folder (which contains all the git configurations like objects, heads and tags) presents called a repository. A repository can only be for a single project.

  2. Server: It stores all the repositories and contains metadata as well.

  3. Working Directory: Where the files, codes, etc related to our project are physically present and can do the modifications called working directory and at a time we can work on a particular branch.

  4. Commit : The process of storing or saving the changes in the repository called commit.

    • Once we commit any changes, we get a commit ID.

    • Commit ID is 40 Alphanumeric character value.

    • It uses the SHAI checksum concept.

    • Even if we change a dot in our code the commit ID will change.

    • Commit is also known as SHA-I hash.

  5. Commit ID: It is a reference to the identity of each change and it will also show who does these changes.

  6. Tags: Tags assign a meaningful name with a specific version in the repository. Once a tag is created for a particular save, Even if you create a new commit it will not be updated.

  7. Snapshots: Represents data for a particular time. It is always incremental. I.e. It stores the change (append date) only, not the entire copy.

  8. Push: Push is a git operation that copies the changes from the local repository server to the remote or the central repository. This is used to store the changes permanently in the git repository.

  9. Pull: Pull is a git operation that copies the changes from the central repository to a local machine. It is also used for synchronization between the repository.

5. How to create a new repository locally and remotely on your GitHub?

There are two types of repositories local repositories and remote repositories. We will see how to create a local or remote repository.

  • Local repository creation: Using the git init command we can make a local directory as a repository. it will create a .git under the local directory that contains Objects, heads and tags

  • Remote repository creation on Github:

    Log in to GitHub, Click on your profile picture and click on your repositories.

    Click on new

    Enter your repository name, provide a description, select repository accessibility whether you want to make it public or private and add readme.md file and .gitignore file are optional if you want you can add them. then click on Create Repository.

6. Distributed Version Control System (DVCS) VS Centralized Version Control System (CVCS)?

In DVCS Every contributor has a local copy or clone of the main repository in their local file system where they can make the changes in the project and commit to the local repository later on, after updating the local repository they can push the changes in the remote repository and it's not like Centralized Version Control System (CVCS) where we have no choice to commit changes in the local repository, we have to communicate and commit the changes directly to remote repository for every changes that is not recommended because if remote server is down we can not communicate and perform the changes in repository in CVCS.

Source: GeeksForGeeks

Source: GeeksForGeeks

7. Git 3-Stage Architecture

There are 3 stages of Git:

  • Working directory: the location where we keep source code and all the related things to our project.

  • Staging Area: Once we finish or do the changes in the source code or any project-related files then we need to give a command to the git tool to track it otherwise our changes will be untracked by git. So, we use the command(git add <file-name>) to make the changes tracked. The stage where our changes got tracked by git called as Staging area.

  • Git Repository: Once we add the changes in the staging area we need to save or commit our changes that stage is called Git Repository. We use the command(git commit -m "<commit-message>") to commit our changes in the Git repository.

8. Git commands and uses:

  • git init: used to make a project workspace as a repository. it will create a .git hidden folder that contains all the objects, tags and heads.

  • git branch: used to check in which branch we are present in currently.

  • git branch -m <branch-name> : used to create a new branch.

  • git checkout -b <branch-name>: command is used to switch branches whereas the "-b" flag with this "git checkout" command will create a new branch by copying data of the current branch.

  • By default, it was on master when we execute the above command dev branch got created and switched to that dev branch.

  • If we do not use the "-b" flag it will not create a new branch it will just switch to the existing branch which we will pass with the command.

  • git branch -d <branch-name> : Used to delete a branch.

  • git config --global user.name <username>: set username locally for Git.

    git config --global user.email <Email>: set Email locally for git.

    git config --global user.name: To check local git user.

    git config --global user.email: To check user email locally.

  • git status: used to check the status of our git repository on the current branch, we could see untracked files, merge conflicts status, etc. If everything is fine on the branch it will show "Nothing to commit, working tree is clean."

  • git add <file-name>: Add an untracked file present in the working directory to the staging area.

    git add . : If we use "." with the "git add" command it will add all the files present in the working directory to a staging area or a git.

  • git restore --staged <file-name>: Used to make tracked file untracked or take back the file from the staged area to the unstaged area.

  • git commit -m "<commit-message>": To add files present in the staging area to Git.

  • git remote add origin <remote-git-url>: used to add remote git URL or origin so, that we can push our local repository changes to the remote repository.

    git remote -v: to see the remote git URL in our local repository.

  • git remote remove origin: used to remove remote git URL or origin.

  • git push origin <branch-name>: Used to push our local repository changes to the remote repository. while pushing changes we need to provide a Github username and password or if you have generated a token then instead of a password we can use a token.

  • git clone <remote-git-url>: Used to clone the remote repository to our local directory in our system.

  • git fetch: to fetch all remote branches.

  • git pull origin <branch-name>: To fetch changes from the remote repository merge it to the local repository within the same branch. Below we did not have the git_pull_test.txt file before we pulled it from the remote repository.

  • git log: to see all the git logs and commit history which includes author, commit ID, date of commit and commit message.

  • git log --oneline: It will show the git log or commit history in one line which will show the commit ID, commit message and at which version our branch is currently.

  • git revert <commit-ID>: If we want to switch back to the previous version of the application because of any reason we can use this command to switch back to the previous version using a specific commit ID.

    We can see in the below screenshot origin/dev switched to the previous version and git revert added a new commit ID for it which will help to again switch back to the latest version in the future.

  • git stash save <file-name>: Let's take an example, we are working on a dev branch and changes are in progress not completed yet and suddenly we got escalation to work on the issue on the hotfix branch. In that case, we need to switch to a hotfix branch but what happens if we switch to a hotfix branch or the file will be visible to git as it is not committed yet and we can see it using git status? So, to hide the uncommitted files from git so, that it can not track we can use the git stash command.

  • git stash pop <file-name>: Once we finish work on another branch and want to get back the incompleted working file that we have stashed we can use this command.

  • git stash list: to list out stashed items list.

  • git stash clear: to clear stashed items.

9. Cherry-picking in Git

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidentally made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

  • Let's say we have committed some changes on a dev branch and its commit ID is "<commit-ID-of-the-source-branch>" but we should have committed this change on the master branch. So, in this case, we should switch the branch dev to master and use the below cherry-pick command to commit that specific change on the master branch.

  • git cherry-pick <commit-ID-of-the-source-branch>

10. Merge conflict and workaround to fix it.

It occurs when we are doing some changes in a file (let's say feature1.txt) on the dev branch and the same file (feature1.txt) on the same remote dev branch someone committed some changes and then if we try to git pull those changes in our local repository it will give merge conflict.

Workaround to fix merge conflict:

  • Do git pull origin <branch-name> --f on the branch where you are getting merge conflict.

  • Execute git status and observe in which file we have conflicts.

  • Go to that affected file and analyze the changes you have done in this file and what done by others. And then analyse what changes you want to keep whether your changes or other's changes.

  • Keep the required things in that file.

  • Again do git status

  • Execute git add <affected-file-name>

  • git commit -m "<commit-message>"

  • git push origin <branch-name>

Thanks,

Gurudeo ray