Introduction to Git
Tags: git, github
Published: 2022-01-16 15:41:02

Git is an open-source, version control tool created in 2005 by developers working on the Linux operating system; GitHub is a company founded in 2008 that makes tools which integrate with git. You do not need GitHub to use git, but you cannot use GitHub without using git. There are many other alternatives to GitHub, such as GitLab, BitBucket, and “host-your-own” solutions such as gogs and gittea. All of these are referred to in git-speak as “remotes”, and all are completely optional. You do

not need to use a remote to use git, but it will make sharing your code with others easier.

Step 1: Create a local git repository

When creating a new project on your local machine using git, you'll first create a new repository (or often, 'repo', for short).

To begin, open up a terminal and move to where you want to place the project on your local machine using the cd (change directory) command.
![Image1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0apwjhel04f7iujochk.png)

To initialize a git repository in the root of the folder, run the `git init` command:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ze50jad1of7z8n7zor33.png)

Step 2: Add a new file to the repo

Go ahead and add a new file to the project, using any text editor you like or running a `touch` command. `touch newfile.txt` just creates and saves a blank file named newfile.txt.

Once you've added or modified files in a folder containing a git repo, git will notice that the file exists inside the repo. But, git won't track the file unless you explicitly tell it to. Git only saves/manages changes to files that it tracks, so we’ll need to send a command to confirm that yes, we want git to track our new file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vthrtv37iiynhseyi4zq.png)

After creating the new file, you can use the `git status` command to see which files git knows exist.
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98l1wb8bakik6nynht7x.png)


###An interlude: The staging environment, the commit, and you

One of the most confusing parts when you're first learning git is the concept of the staging
environment and how it relates to a commit.

 A commit is a record of what changes you have made since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those changes into a commit.

Commits make up the essence of your project and allow you to jump to the state of a project at any other commit.

So, how do you tell git which files to put into a commit? This is where the staging environment or index come in. As seen in Step 2, when you make changes to your repo, git notices that a file has changed but won't do anything with it (like adding it in a commit).

To add a file to a commit, you first need to add it to the staging environment. To do this, you can use the git add <filename> command (see Step 3 below).

Once you've used the git add command to add all the files you want to the staging environment, you can then tell git to package them into a commit using the git commit command.
 
Step 3: Add a file to the staging environment

Add a file to the staging environment using the git add command.

If you rerun the git status command, you'll see that git has added the file to the staging environment
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihg6llkmk9mg3f10a3dp.png)

Step 4: Create a commit
It's time to create your first commit!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyct2qsez8cdd64irtdv.png)
Run the command git commit -m

Step 5: Create a new branch

Now that you've made a new commit, let's try something a little more advanced.

Say you want to make a new feature but are worried about making changes to the main project while developing the feature. This is where git branches come in.

Branches allow you to move back and forth between 'states' of a project. Official git docs describe branches this way: ‘A branch in Git is simply a lightweight movable pointer to one of these commits.’ For instance, if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you're done with the page, you

can mergeyour changes from your branch into the primary branch. When you create a new branch, Git keeps track of which commit your branch 'branched' off of, so it knows the history behind all the files. Let's say you are on the primary branch and want to create a new branch to develop your web page. Here's what you'll do: Run git checkout -b <my branch name>. This command will automatically create a new branch and then 'check you out' on it, meaning git will move you to that branch, off of the primary branch.

After running the above command, you can use the git branch command to confirm that your branch was created
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7bplzpiw4xfsk3ez4hg.png)
 
Step 6: Create a new repository on GitHub

If you only want to keep track of your code locally, you don't need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project's code.

To create a new repo on GitHub, log in and go to the GitHub home page. You can find the “New repository” option under the “+” sign next to your profile picture, in the top right corner of the navbar
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jhu880s8jmus87v5hoq.png)
 

After clicking the button, GitHub will ask you to name your repo and provide a brief description
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mmf6i2t3i4q34y8t6wh.jpeg)
 

When you're done filling out the information, press the 'Create repository' button to make your new repo.

GitHub will ask if you want to create a new repo from scratch or if you want to add a repo you have created locally. In this case, since we've already created a new repo locally, we want to push that onto GitHub so follow the '....or push an existing repository from the command line' section

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9iqszkp76r27zso39yh.jpeg)
 

Step 7: Push a branch to GitHub

Now we'll push the commit in your branch to your new GitHub repo. This allows other people to see the changes you've made. If they're approved by the repository's owner, the changes can then be merged into the primary branch.

To push changes onto a new branch on GitHub, you'll want to run git push origin yourbranchname. GitHub will automatically create the branch for you on the remote repository
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okn5v5iure2sxa3u54um.png)
 

Step 8: Create a pull request (PR)

A pull request (or PR) is a way to alert a repo's owners that you want to make some changes to their code. It allows them to review the code and make sure it looks good before putting your changes on the primary branch.

This is what the PR page looks like before you've submitted it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u07vn7d0ryy0n3ij6bg1.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6senncb522ap9d2hfel.jpeg)
 
 

You might see a big green button at the bottom that says 'Merge pull request'. Clicking this means you'll merge your changes into the primary branch..

Sometimes you'll be a co-owner or the sole owner of a repo, in which case you may not need to create a PR to merge your changes. However, it's still a good idea to make one so you can keep a more complete history of your updates and to make sure you always create a new branch when making changes.

Step 9: Merge a PR

Go ahead and click the green 'Merge pull request' button. This will merge your changes into the primary branch.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bq2d0jbl20qkofhvd3k.png)
 
 

When you're done, we recommend deleting your branch (too many branches can become messy), so hit that grey 'Delete branch' button as well.

You can double check that your commits were merged by clicking on the 'Commits' link on the first page of your new repo.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pw7dsa3rgim76hxp3u5y.png)
 

This will show you a list of all the commits in that branch. You can see the one I just merged right up top (Merge pull request #1).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1o7kt9xpmeobh1hp6fb.png)
 
 

You can also see the hash code of the commit on the right hand side. A hash code is a unique identifier for that specific commit. It's useful for referring to specific commits and when undoing changes use the git revert <hash code number> command to backtrack.
Step 10: Get changes on GitHub back to your computer

Right now, the repo on GitHub looks a little different than what you have on your local machine. For example, the commit you made in your branch and merged into the primary branch doesn't exist in the primary branch on your local machine.

In order to get the most recent changes that you or others have merged on GitHub, use the git pull origin master command (when working on the primary branch). In most cases, this can be shortened to “git pull”.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyh14wulzhn8wa8urksb.png)
 
 
This shows you all the files that have changed and how they've changed. Now we can use the git log command again to see all new commits.

(You may need to switch branches back to the primary branch. You can do that using the git checkout master command.)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxlpyzuhkd642nrc37jv.jpeg)

<pre>
    ❤️ <a href="https:ieeemace.org">Website</a>  |  📷<a href="https://www.instagram.com/ieeemace">Instagram </a>  |  💼<a href="https://in.linkedin.com/company/ieeemace">LinkedIn</a>   |  🐥<a href="https://twitter.com/ieeemace">Twitter</a>    
</pre>