Git + GitHub beginners friendly
Tags: git, github, programming, fcorreia18
Published: 2023-03-15 10:04:30


If you're a beginner developer, you might think that these two terms mean the same thing – but they're different.

**So lets have quick aside before we start:** _Git_ and _Github_ are not the same thing. Git is a free and open-source, version control system designed for working on the Linux operating system; GitHub is a company that makes tools which integrate with git in other terms github uses Git underneath, and lets you manage your Git repositories or folders easily on its platform. 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.
Now that we know the difference, lets get right into.


**In order to follow along with me, you'll need:**

1. A command line interface.
2. A text editor.
3. A GitHub account.


- **How to install Git**

In order to use Git, you have to install it on your computer. To do this, you can download the latest version on the [official website](https://git-scm.com/downloads). You can download for your operating system from the options given.

Follow the necessary installer guide until installation is complete. Open the command prompt and type `git version` to verify that Git was successfully installed.


- **Create a GitHub account**

The next thing you'll need to do is to create an account, you can do this [here](https://github.com) 

Now with your terminal opened, configure your username and email address. Git will use this information to identify who made specific changes to files.

To set your username, type and execute these commands: 

```
git config --global user.name "YOUR_USERNAME"
git config --global user.email "YOUR_EMAIL"
```
Just make sure to replace "YOUR_USERNAME" and "YOUR_EMAIL"  with your personal data.

- **Set up the project**

At your terminal move to where you want to place the project on your local machine using the cd (change directory) command. For example, i've created a folder named **git-github** and changed to it:

>###### **Quick note:** the commands of the next two sections may change according your operation system and the terminal you're using, i recomend to take a look at the documentation of the terminal.

![Created the folder wich contains the project files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rpnf1989haip0enovxv.png)
Now to initialize a git repository in the root of the folder, run the `git init` command: 


![Initialized a git repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0qp80l7xxscq5mjbgow.png)

- **Add a new file to your local repository**

Add a new file to the project,by running a touch command. `touch tasks.txt` this command will create and save a blank file named tasks.txt. 

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


![checkig the status of the files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxqkbfr72phv3ayc37ul.png)

Git is basically saying: 

> we noticed you created a new file called **tasks.txt**, but unless you use the `git add` command we can't  do anything with it."

Now lets add into the staging area so git can track our file.

![adding the file into tracking/staging area](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbjxayjezasppk5gg4ai.png)

If you notice i have used the 3 following commands: 
 `echo "git-github beginner friendly" >> tasks.txt` to write inside my file.

`git add tasks.txt` that actually put the file in the staging area.

`git status` shows the files we have and their current state. And now, we can see that we have a different information, "git is now tracking tasks.txt".


- **Commit files in Git**

The next state for a file after the staged state is the committed state. To commit our file, we use the `git commit -m "message of commit"` command.

The message at the end of the commit should be something related to what changes you made - maybe it's a new feature, maybe it's a bug fix. A clear explanation of your changes can be extremely helpful for future programmers who are trying to figure out why some changes was made looking at your  code years later.

The command `git commit`  records the modification of all files. The second part `-m "first commit"` is the commit message. `-m` is shorthand for _message_.

After executing this command, you should get something similar to this: 

![Commiting a file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9g8y9t2xjhp5v2ca1x6.png)

Now our file is in the committed state.


- **Lets create a Github repository**

![github create new repository page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9q61wc5l19k8na2o5ra.png)

When you press **Create repository** github will make your new repository. And then you're gonna be redirected to a page that tells you how to create a new repository from scratch or if you want to push a repository you have created locally.

![github new repository image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcnll01x1uvvjh2w1wdh.png)

since we've already created a new repo locally, we will use commands in the _"…or push an existing repository from the command line"_ section. These are the commands:

`git remote add origin https://github.com/fcorreia18/git-github.git
git branch -M main
git push -u origin main`

![push a local repository to github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbrug98m37e5r26gzixl.png)

1. The first command creates a connection between your local repo and the remote repo on Github.
2. The second command changes your main branch's and name it to "main". acording your git version The default branch might be created as "master", but "main" is the standard name for repositories now.
3. The last command pushes your local repository to GitHub.


**Git Branches**

Git recommend us to work with branches: because it allows you to create a copy of a file you would like to work on without messing up the original copy. You can either merge these changes later to the original copy or just let the branch remain independent.for example, if you want to work on a project feature, create a feature branch using `git checkout` or `git branch`. Make changes the changes in your files(the actual feature) and when you have done, merge the branch with the base or just create a pull request inside your github repository. 
Here's an example:


![Creating a new branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r83tltm3fsjzxgxdsyl.png)

`git checkout -b feature` In our case, we have created and switched to a new branch called `feature`.


![change inside the files in a new branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wowf70go0z117jvrchp8.png)

`echo` to write inside the file
`cat` to view the changes we made
`git add tasks.txt` to stage our changes
`git status` to check the status of our file, we can see that git notice that we've changed something.


Now we just need to commit it:


![commit in a new branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uayvbdm74lsqzy0trigt.png)
Now that we commit our changes we need to send it to the remote repository, but the remote repo doesn't have a _feature_ branch. To create a new branch and push changes, we can use `git push origin feature:feature` or `git push -u origin feature`. The output of the command shows that a new branch have been created and both local and remote `feature` branches are synced. 


![push the new branch to remote repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hpvt2u27hsp0f68smzc.png)

Looking at the output you can observe that we have successfully pushed the local branch to GitHub with a modified version of our tasks.txt file. 
Git also says to create a pull request inside of our github repository, but in order to learn how pull requests work we gonna do this manually by merging the main branch with the feature.

**Merging Branches**

Before we merge the branchs i just want to show you the current stage of our files in the main branch


![checkout the files stage inside main branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgocavkmetofxxdmgdfi.png)
Did you notice that the changes we made did not afect the file inside of the main branch? this is one of the greatest features of git it allows us to have multi stages of our files in different branchs and even at the same branch we can `checkout` different versions of the file _just giving you a spoiler_😁🤭


Now we can merge the changes we made in the `feature` branch into the main branch by running `git merge feature`. At this point, you will see all the changes made in the `feature` branch reflected on the main branch. You should also have an output similar to this:


![merging branchs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7l0l4tplx4a84f6slp5.png)



## Basic Commands

Before we finish i just want to give some basic commands that you probably gonna use often. The basic commands include initializing the Git repository, saving changes,review files, checking logs, pushing the changes to the remote repository, merging, etc.
 
`git config` use to set user specific configurations such as email, username, and file format.
`git init` create a Git repository in a local directory.
`git clone` <remote-repo-url>: copy the entire repository from a remote server to remote directory. You can also use it to copy local repositories.
`git add` <filename.extension>: add a single file and folder to the staging area. While `git add .` lets you add all files in the present folder.
`git commit –m “message”`: _-m_ stand for message followed by the actual message, will create a snapshot of changes and save it in the repository.  
`git status` shows the list of changed files or files that have yet to be staged and committed.  
`git push -u <remote-name> <branch-name>`: send local commits to remote branch of repository, the -u flag creates a tracking reference for the branch.  
`git checkout -b <branch-name>`: creates a new branch and switches to a new branch, if you dont want to switch just use `git branch <branch-name>`.
`git remote –v`: view all remote repositories.
`git remote add` [-t <branch>] <remote-URL>: connects the local folder to the repository on GitHub.
`git branch –d` <branch-name>: delete the branch.
`git pull` pull the history commits to a local repository from a remote repository. 
`git merge <branch-name>`: after resolving merge conflicts(if you have) the command blends selected branch into the current branch.
`git log` shows a detailed list of commits for the current branch(We didnt use it, why dont you try it by yourself as a homework?).



Those are some of the main commands that'll help get you started using Git and you'll use all the time. if you find hard to use it just stick at this article, feel free to take a look as many time you need. Just remember _**pratice makes improvement**_
The more you continue using Git and GitHub, the more comfortable you'll get using these commands. it will eventually get easier as you build small projects and host them on GitHub using Git.



**CONGRATULATIONS**
I'm glad you've done so far 💯, in this article, we have learned about Git and GitHub and why they are so important. The tutorial also introduces you to basic Git commands and provides hands-on experience on how to track changes in your files and code. If you are interested in learning more about Git, here are some resources:
[Git Official Documentation](https://git-scm.com/doc)
[Introduction to Github](https://docs.github.com/get-started/quickstart/hello-world) 
[Git Playground](https://learngitbranching.js.org)
[w3 schools Git Basics](https://www.w3schools.com/git)
[Free Code Camp Introdution to Git](https://www.freecodecamp.org)

If you like this article consider to subscribe and follow me in my social mideas:  

[LINKEDIN](https://www.linkedin.com/in/francisco-correia-2393411a4/)
[GITHUB](https://github.com)

Happy coding!💻