--- robots: index, follow lang: en dir: ltr breaks: true --- Git & GitHub === <!-- # <i class="fa fa-github"> --> ![](https://i.imgur.com/ktgZOr2.png) --- # What is Git ? **Git is a version control system that is used for tracking changes to files and coordinating work on those files among multiple people. It was originally developed by Linus Torvalds, the creator of the Linux operating system, to manage the source code for the Linux kernel.** **Git is a distributed version control system, which means that each copy of a repository (a collection of files and directories that are tracked by Git) contains a full copy of the entire repository history, including all of the commits (snapshots of the files and directories at a particular point in time) and branches (lines of development). This allows developers to work independently and make changes to their local repositories, and then push their changes to a remote repository to share them with other team members or make them available to the public.** **Git is widely used by software developers to manage source code for their projects, and is also commonly used by individuals and organizations to track changes to other types of files, such as documents, configuration files, and multimedia content. Git is an open-source project and is available for use on a wide variety of operating systems, including Linux, macOS, and Windows** # What is GitHub ? **GitHub is a website and cloud-based service that helps developers store and manage their code repositories, track changes to their code, and collaborate with other developers. It is a platform that allows developers to host and review code, manage projects, and build software.** **Git is a version control system that allows developers to track changes to their code over time and collaborate with other team members. GitHub is a web-based service that provides a user interface for working with Git and helps developers store and manage their code repositories.** **GitHub also provides a range of tools and features that make it easier for developers to work together and collaborate on projects, including the ability to create and manage issues, pull requests, and wikis. It is a popular platform for open-source projects and is widely used by developers around the world.** --- # So What is Version control ? > Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. So ideally, we can place any file in the computer on version control. # What is a Repository ? **Git Workflow** Before we start working with Git commands, it is necessary that you understand what it represents. A repository repo is nothing but a collection of source code. There are four fundamental elements in the Git Workflow. **Working Directory**, **Staging Area**, **Local Repository** and **Remote Repository**. If you consider a file in your Working Directory, it can be in three possible states. 1. * It can be `staged`. Which means the files with the updated changes are marked to be committed to the local repository but not yet committed. 1. * It can be `modified`. Which means the files with the updated changes are not yet stored in the local repository. 1. * It can be `committed`. Which means that the changes you made to your file are safely stored in the local repository. --- Four areas: --- ![](https://i.imgur.com/Ro9l7cY.png) In the beginning: --- ![](https://i.imgur.com/etuybJn.png =410x250) git init: --- ![docx](https://i.imgur.com/w13aKPa.png =500x350) git add: --- ![](https://i.imgur.com/DXzZ9dX.png ) git commit: --- ![](https://i.imgur.com/gTUUIy1.png) git remote add: --- ![](https://i.imgur.com/7M97Wqk.png) git push: --- ![](https://i.imgur.com/zOKPsW4.png) git pull: --- ![](https://i.imgur.com/8bCqzEw.png) git reset: --- ![](https://i.imgur.com/h44GBGb.png) git checkout: --- ![](https://i.imgur.com/3p6tQwI.png) git restore --- ![](https://i.imgur.com/KGsfIzg.png) --- # Five Steps to Project Tracking by Git & Uploade to GitHub: ## Step 0: Make a GitHub Account. Duh. If you don't already have one, you can make one [here](https://github.com/join). ## Step 1: Make sure you have Git installed on you machine. If you are on a Mac, fire up the terminal and enter the following command: ``` $ git --version ``` This will prompt open an installer if you don’t already have git. So set it up using the installer. If you have git already, it’ll just show you which version of git you have installed if you don't have GIT install from [here](https://git-scm.com/downloads). If you are running Linux(deb), enter the following in the terminal: ``` $ sudo apt install git-all ``` If you are on Windows: ``` $ get a mac ``` Just kidding… Relax… The amount of people I triggered… Phew… Go to this [link](https://www.apple.com/macos/what-is/) or this [link](https://gitforwindows.org/) for more info on how to get it. ## Step 2: Tell Git who you are. Introduce yourself. Slide in. Seriously, mention your Git username and email address, since every Git commit will use this information to identify you as the author. ``` $ git config --global user.name "YOUR_USERNAME" ``` ``` $ git config --global user.email "im_satoshi@musk.com" ``` ``` $ git config --global --list # To check the info you just provided ``` ## Step 3: Let’s Git * Create a new repository on GitHub. Follow this [link](https://github.com/new). Now, locate to the folder you want to place under git in your terminal. ``` $ cd <dir_name> ``` * Initialize Git: And to place it under git, enter: ``` $ touch README.md # To create a README file for the repository $ git init # Initiates an empty git repository ``` * Now to add the files to the git repository for commit: ``` $ git add . # Adds all the files in the local repository and stages them for commit ``` OR if you want to add a specific file ``` $ git add README.md # To add a specific file ``` Before we commit let’s see what files are staged: ``` $ git status # Lists all new or modified files to be committed ``` * Commit Changes you made to your Git Repo: Now to commit files you added to your git repo: ``` $ git commit -m "First commit" # The message in the " " is given so that the other users can read the message and see what changes you made ``` ## Step 4: Multiple branches ``` git checkout -b <branch_name> ``` ## Step 5: Upload to your Repo. ``` git push original <branch_name> ``` # When you want to download a project from GitHub using Git, you need to clone the repository. Here are the steps: ## Step 0: Navigate to the repository on GitHub. You first need to find the GitHub repository you want to clone. Open it in your web browser. ## Step 1: Copy the repository URL. Once you're on the repository page, you will see a green "Code" button near the top right of the page. Click on this button and then click on the clipboard icon to copy the repository's URL. ## Step 2: Clone the repository with Git. Open your terminal or command prompt on your computer. Navigate to the directory where you want to clone the repository using the ``` cd ``` (change directory) command. Once you're in the desired directory, type ``` git clone ``` followed by the URL you copied in the previous step. It should look something like this: ``` git clone https://github.com/username/repository.git ``` Press enter, and Git will begin to clone the repository onto your local machine. ## Step 3: Navigate to the cloned repository. Once the clone is complete, you can navigate into the new directory that has been created. This directory will have the same name as the repository. You can do this with the ``` cd ``` command again: ``` cd repository ``` Now, you will be inside the cloned repository on your local machine, and you can start working with the project. ## Step 4: Pull the latest changes. If you want to update your local copy with the latest changes from the online repository, you can use the ``` git pull ``` command while you are inside the repository directory: ``` git pull ``` This will pull down any new changes that have been made to the repository since you last cloned or pulled. # Merging branches in Git is a common task when working with version control. The following steps will guide you on how to merge one branch into another: ## Step 0: Checkout the branch you want to merge into. First, you need to be on the branch that you want to merge into. This is typically the **main** or **master** branch, but it could be any branch. To do this, use the ``` git checkout ``` command: ``` git checkout main ``` Replace **main** with the name of the branch that you want to merge into. ## Step 1: Merge the other branch. Once you are on the branch that you want to merge into, you can merge the other branch with the ``` git merge ``` command: ``` git merge feature-branch ``` Replace **feature-branch** with the name of the branch that you want to merge. ## Step 2: Resolve any merge conflicts. If there are changes in the branch being merged that conflict with changes in the branch you are merging into, you will need to resolve those conflicts. Git will give you instructions on how to do this when you run the merge command. ## Step 3: Commit the merge. If there were conflicts, after resolving them, you need to commit the merge. You can do this with the ``` git commit ``` command. Git will open a text editor for you to write a commit message. Once you've written the message, save and close the file to create the commit: ``` git commit -m "Your commit message" ``` ## Step 4: Push the merge. Finally, you can push the merge to the remote repository with the ``` git push ``` command: ``` git push origin main ``` Replace **main** with the name of the branch that you are pushing. This updates the remote repository with the merge you just performed.