Git & GitHub: Beginner to Advanced

Git & GitHub: Beginner to Advanced

What is Git:
Git is a distributed version & change tracking tool where we can track the versions and changes in our project code. Also, through git, people can work collaboratively on the same project from different locations.

What is GitHub:
GitHub is a project/code hosting platform where we can store our projects/source code. GitHub allows multiple teams to work together.

What is a Repository:
A repository is nothing but an online folder in GitHub where everyone/permitted users can access remotely. We can pull the repository in our local machine using Git.

Difference between Git and GitHub:
GitHub is a service where Git is actually a tool/software. GitHub hosts the source code/project where Git helps us to track different versions and changes. GitHub provides a GUI interface whereas Git is a CLI tool. GitHub is maintained in the cloud/web where Git is installed and maintained locally.

GitHub has more than 56 million users and it was founded in 2008. Currently, GitHub is owned by Microsoft. Git was founded by Linus Torvalds and Junio C Hamano in 2005.

Git Installation & Configuration:
Visit the link for download: https://git-scm.com/downloads. Download and install git and then run the command in the CMD: git --version to check whether it has been successfully installed in the system. We can also see the version of git using this command.

For global configuration in the system, go to CMD and type:

  1. git config --global user.name “your username” (For creating username)

  2. git config --global user.email “your email” (For setting up email)

  3. git config --list (to check whether the username and email are set)

  4. git config user.name (To check the username)

  5. git config user.email (To check the email)

How to Create Git Folder:

There are a few steps. They are discussed below.

  1. Working Directory: When we create a local folder, we call it our Working Directory. Git is initialized here. Folders contain multiple files according to our needs. Now, how can we do this? First, we can create a folder anywhere in our machine by writing mkdir folder_name in the CMD. Then we need to initialize git in the folder by writing git init in CMD. We can now add files to that folder. To check the files' statuses we can just write git status . It will show us whether the files are tracked or untracked by git.

  2. Staging Area/Index: We bring the files from the working directory in the Stagging area so that Git can track the files. If we do not move the files in Stagging then the files become untracked by Git. To take the file to Staging we can simply write: git add "filename" . To take all the files at a time we can write: git add -A . We can run git add . if we only want to take all the files inside the directory but not subdirectories. To take only one category of files inside a directory we can write for example git add *.js . It will take all the .js files inside the directory. This is called directory wildcard. For bringing directory and sub-directory files we can write git add **/*.js . To restore any file before adding the changes to Staging we can just write git restore "file name". To unstage any file, we can write git rm --cached "file name" . Here, rm means remove. To see the changes in any file we can simply write git diff .

  3. Local Repository: We move the files from Stagging area to Local Repository. The local repository is a git repository that is stored on our computer. The files are now ready to move to Remote Repository. In this repository, we can control the versions of our code. To move our files to Local Repo from Staging we can write the following command: git commit -m "our message" . To see the commit history we can run: git log . For staging and committing directly we can write: git commit -am "our message" . Another alternative way is writing git add . && git commit "our message" . Here both operations are performed at the same time. If we want to undo committing by keeping everything intact, we can write: git reset --soft HEAD<sup>^</sup> . It will take our files to the staging area. If we want to undo committing and also remove from the staging area, we can write: git reset HEAD<sup>^</sup> . It will take our files to the working directory. Then if we want to completely undo it throwing away all uncommitted changes, and resetting everything to the previous commit, we can write: git reset --hard HEAD<sup>^</sup>. For deleting a number of commits at a time we can write: git reset --soft HEAD^~2/3/4 etc. To check the history in one line we can write: git log --oneline . To know more precisely about one commit we can type: git show 'commit id' / git show / git show HEAD~'number' .

  4. Remote Repository: Remote repository is actually a central storage where everyone pushes their codes from their local repository. By doing this, many of us can work on the same project. If we want to push something into Remote Repo, we must bring those files into Local Repo first.

Points to be noted, we can write the commands in CMD in that particular folder as well as in Git Bash terminal.

Undo:

Using git reset is not always safe. We should try to avoid git reset as much as we can. Because it might permanently remove some files that we no longer can retrive. git checkout can be a safe option here. We can defince in which commit we want to go back. It's like moving from commit to commit! To make this we can write: git checkout "commit id" . If we want to come to the previous commit then we can write: git checkout master . Besides if we write git checkout "file name" then the previous changes in this file will discard.

What is gitignore:

.gitignore is a file which contains the files and folders that we do not want to share. That means, git will not bring them in staging. For example, if we want to ignore one file named test.txt, then we need to specify that file in gitignore. If we add *.txt then git will ignore all txt files. To create a .gitignore file we can write following command in Git Bash: touch .gitignore. To ignore a file we can just simply mention the file name inside gitignore. To ingore a folder we can write: folder_name/ inside gitignore and save it.

GitHub Repository & Commit:

We need to make sure that we have a GitHub account and we are signed in. To create a GitHub account, please follow this link: https://github.com/join. After signing in, we need to create a repository. We can click on the + icon on the top right corner of the page and then select New repository. After that, we need to put a name of our repository. Remember, there is no space allowed in the name segment by default. We can add some description in our repository if we want to. Then we need to choose whether we should keep our repository public or private. If we click on public then the repository will be visible to everyone. We can also add a readme file. Finally, we need to click on the create repository button. We can add files in github and then we can commit our file(s) with some description by clicking on Commit new file button. We can do all of these without the help of git since github is providing us the GUI. To see history of commits we can just simply click on the clock icon. We no longer need to write command. We can also see what has changed in the commit by clicking on the commit id and then we can notice the green marks which indicates the changes.

Markdown:

When we upload a project in github, we need a brief summary of the project. We add the summary & important notes about our project or repository inside the README.md file. Here, .md is the extension of markdown file. Markdown helps us to visualize our notes in a beautiful and meaningful way. Such as, we can use headings in our summary notes inside the readme file. We can add lists & tasks. We can also add table, images or links. We can do all these things using markdown. To get a brief list of markdown techniques, please visit the link: Click here.
You can also download it and open it in the VSCode or in any text editor.

Connecting Local & Remote Repository:

Many engineers work in the same project at the same time. They code in their local machines and save it. Which is actually Local Repository. But we need a central storage where all the engineers can make the changes simultaneously. And that central storage is GitHub Repository. We can also call it Remote Repository. We should push our code to the github repository to contribute in the project. Moreover, if we want to bring the code to our local machine from github repository we should perform pull operation. To check whether the local repo is connected with remote repo or not, we can write: git remote . Also, we can write git remote -v to check the details. To add remote repo in our local macine, we can write git remote add 'any name' <remote url>. Ex: git remote add origin "link" . To copy an entire project from remote repo to our local machine, we can write: git clone "repo link".

SSH Key:

Secure Shell or SSH is a network communication protocol that enables two computers to communicate sucerly. We can generate SSH key and link with github. To link our generated SSH key in github we need to go to the Settings first. Then we will see an option named SSH & GPG Keys on the left side of the screen. Click on the SSH & GPG Keys option and we will notice that initially we do not have any SSH keys associated with our account. Now, we need to press on the New SSH Key button. After that we will give a title, set key type, and paste the SSH Key in the Key box and finally press on Add SSH Key button. To clone any repository, we can use SSH Key instead of HTTPS link.

Git Pull & Push:

We already know about pull & push and their uses. Now, suppose we make a folder and added some files in that folder. To push the files to the remote repo, first we need to initialize git in that folder. Then we need to add the file in staging area and commit in the local repository. Only after that we can push the files to the remote repository. But to pull anything from the remote repo, we can do it directly without performing any prior tasks. To push our files/changes/codes to the remote repo we can write: git push -u origin main/master . To bring the changes in the remote repo to our local repo we can write: git pull .

Branching and Merging:

Branch in github is actually a new and a separate branch of the master repository. While working in a big project, we separate the tasks/features and create banches so that editing in the new branch does not affect the master branch. To create a branch from the master we can write for example: git branch dev. That means, a new branch called dev has been created; besides all the codes in master are copied in the dev. Here, dev is actually the branch name. To move from master to dev, we can just write: git checkout dev. After all the changes and testing in the dev branch we finally merge the changes with the master branch which is our main project directory. Mergin is actually adding the changes in our desired branch. To merge the changes to master branch we can move to master branch first by writitng: git checkout master and then we need to write git merge dev . Here, dev is the branch name which we want to merge with master branch. We should keep in mind that master & main branches are same. Now, we need to know about pull request or PR. Pull requests let you tell others about changes you've pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch which is actually our main branch. To delete a branch we can write git branch -d "branch name". To create a branch and switch to the branch at the same time we can write git checkout -b "branch name" . Now, we also can perform branching and merging locally. For this, we need to first go to the main/master branch. And then we need to write git merge "branch name". To push the updated changes in our main/master branch, we need to pull the changes first. So let's write git pull and then write git push -u origin master.

2 Way Merge:

2 way merge is also known as fast forward merge. Fast forward merge can be performed when there is a direct linear path from the master/main branch to the target branch. In fast-forward merge, git simply moves the master/main branch pointer to the target branch pointer without creating an extra merge commit. To make this, we need to ensure that we are in our source branch: git checkout master/main . Now, we need to write git merge feature. Here, feature is the target branch name.

3 Way Merge:

Suppose, one person is working on source branch and another peron is working on a different branch called feature. Both of them added multiple commits in two different branches; source and feature. So there are 3 commit we can notice. One is in the source branch, one is in the extended commit from source, another is in the feature branch. Now we need to merge all of them together. To do this, we need to be present in the source branch first. Then we need to write git merge feature.

What is Merge Conflict:

Merge conflicts generally arise when two people change the same lines in a file, or if one person deletes a file while another person is modifying it. Suppose we have one branch which is Master. In that branch we have a text file and there are 2 lines present inside that text file. Now we create another branch called Branch-2 and this new branch also gets the same text file with same 2 lines. Now one person added a 3rd line in the Branch-2 saying: "hello there!". Another person added a 3rd line in the Master branch saying: "hi! learning is important". That means, there are different changes in the 3rd line in the same file of both the branches. Merge conflict will take place once we merge the two branches. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the person conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the person's responsibility to resolve the conflict. Merge conflict may look like this:

Resolve Merge Conflict on Git:

To find out the conflict we can write git status and it will show you the summary like this:

That means, it's saying that both file1.txt in two branches are modified. To see the modifications we can open the file by writing file1.txt in the command line and then we will see the changes. Now, we have to correct the changes in the file manually and then we can add the changes and make a commit to resolve the conflict.

GitHub Fork & Clone:

A fork is a new repository that shares code and visibility settings with the original “upstream” repository. Forks let us make changes to a project without affecting the original repository, also known as the "upstream" repository. After we fork a repository, we can fetch updates from the upstream repository to keep our fork up to date, and we can propose changes from our fork to the upstream repository with pull requests. A fork can be owned by either a personal account or an organization. We can use fork to contribute to others' projects or we can use it to work on our own projects based on theirs. We need to remember that, fork is not a command. We need to use GitHub to fork a repository. Besides, forking own repository is not possible and meaningless.

We can clone a repository from GitHub to our local computer to make it easier to fix merge conflicts, add or remove files, and push larger commits. When we clone a repository, we copy the repository from GitHub to our local machine. Cloning a repository pulls down a full copy of all the repository data that GitHub has at that point in time, including all versions of every file and folder for the project. We can push our changes to the remote repository on GitHub, or pull other people's changes from GitHub. To clone a repository, we should copy the HTTPS link first. After that we can open terminal where we want to clone the repository and write git clone 'link' .

Contribute to Others' Projects:

We can also contribute to other's project by using fork & clone. We can clone our own existing repository or clone another person's existing repository to contribute to a project.
First, we need to fork the repository where we want to contribute. That means, that repository is now available as a new repository in our github account. After that, we clone that repository so that we can work in that repository from our machine. Now we can make changes in the project, push to our repository and then make a pull request to the original project owner. If the owner of the original project accepts our PR, then he/she can merge the changes in his/her project repository.

Collaboration in GitHub:

We need to keep in mind that collaboration & contribution are not same. collaboration is the way different people can work on the same project together. It is like creating a group in GitHub just like Groups in other social media. The people added to the collaborator's list are able to push, merge, and do other kinds of similar things on the project. In collaboration there is no need to fork a repository. Only clone is enough. The owner needs to give the collaborator access to the project. On GitHub, click the settings button on the right, then select collaborators, and enter your partner's username. To accept the access to the owner's repo, the Collaborator needs to go to GitHub notifications. Then he/she can accept the access to the Owner's repository.

Hi! Thank you so much for reading this documentation with patience. I hope you found this useful. However, I would love to recommend you to practice whatever you have learned through this article since only reading the documentation will not be that much fruitful. Besides, I am aware that many questions were running through your mind while reading the article. I would request you to do a little more research about the questions you have in your mind since it's so difficult to mention each & everything in a single document. Lastly, this is my humble request to all of you to share this documentation to whoever needs it. Thank you again!