Git and GitHub for beginner
Tags: javascript, github, git, beginners
Published: 2021-09-24 12:07:33

Familiarity with __Git__ caused some uncertainty. So I decided to make a list and a description of the git commands for the terminal.
Here I use a terminal (you can also use a terminal in __VS Code__), pre-installed __Git__ ([here](https://git-scm.com/)) and my page on __GitHub__.

_What is Git and why do we need to use it_?
__Git__ is a distributed free and open source version control program. It is a program that is needed for developers to be able to keep track of changes to program code made by other developers, and be able to make consequential changes if needed.

###_What is GitHub?_
__GitHub__ is a web service for software development where all participants have the opportunity to save their changes to an ongoing project and get an overview of how the project is progressing. So developers should be able to handle version management with the help of git both locally and on their web service (externally).
By having git installed on your local device, you can handle version management both locally and externally on GitHub. Should your program code disappear locally for any reason, you can always find the latest uploaded code on GitHub.

###Git commands:
Here are the commands that will be described in the post:
- git --version;
- git --help;
- git --init;
- git status;
- git add .;
- git rm --cached filename;
- git commit -m "";
- creating a .gitignore file;
- git branch;
- git branch branchname;
- git branch -D branchname;
- git checkout branchname;
- git checkout -b branchname;
- git merge branchname;
- git remote add origin https://... .git;
- git push -u origin branchname;

Let's start!

- __git --version__

Prints the Git suite version that the git program came from.

- __git --help__

Prints the synopsis and a list of the most commonly used commands. 

Next we need to create a folder and files in VS Code that we would like to commit. I have the folder "test" and two files "index.html" and "index.js".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ih2ykvs51yyce6meakp.png)

- __git --init__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79e7fxewz7ivtiz6pj5c.png) Create an empty Git repository or reinitialize an existing one.

    cd .git
    ls
With help of these commands we can see what is in the folder git. In this folder will be all information that needs to control version. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9oakrgcwzyyrh1rzyga.png)


- __git status__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a10ddi7apdczqxa5u688.png) Show the working tree status. 
Here we see that git tells us that there are two files that it does not monitor.

- __git add index.js OR git add .__ (to add all files)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86ydig0fwczq1bntubvz.png) To add a file(s) as it looks now to your next commit (stage). If you want to delete one of the files then you can do it with the following command.


- __git rm --cached filename__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d68gygdtn60d6j5goj4i.png) Thus git stops following the index.js file. But now I add this file again.

If at this stage we will make changes to the file in VS Code, it can also be seen with the command __git status__.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvx97rq5s5jtjo7v5enj.png) We see that the index.html file has been modified, so we need to add it again using the __git add index.html__ command.

- __git commit -m ""__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iey2r37sg8q6r3nma3b7.png) This command record changes to the repository. In quotes we write a comment to our commit.

- __.gitignore__

If we have certain files in a folder that we do not want to commit, then in VS Code you can create a special __.gitignore__ file in which you can write the names of files or folders that we do not want to commit. It will look like this.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmz7slkh99ojwrl7j2a9.png) The .gitignore file must be committed.

- __git branch__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vazrb5yple51i9016x3j.png) We use this command to see which branch we are in now.

- __git branch branchname__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib6lg0b9my2qaxajj1vv.png) With this command we can create an additional branch. It is needed when several people are working on the project.

- __git branch -D branchname__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zjm7mm28pamv0ugqm9v.png) You can use this command to delete a branch.

- __git checkout branchname__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38jl5zxjki9u9wo2et1z.png) With this command you can go to the branch.

- __git checkout -b new__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg613qggrn0lduq8olq1.png) With this command you can immediately create a new branch and go to it.

- __git merge branchname__

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zcgz380od9s9gt2vquo.png) This command is used to integrate changes from another branch.


Next, we need to work with __GitHub__ (respectively, you need to be registered there).
We create a new Repository, where we prescribe its name and choose public or private. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mqjmco59a652zp8n7x9.png)

- __git remote add origin https://... .git__

- __git push -u origin branchname__

Next, you need to run two more commands in the terminal, which are listed on GitHub. They can simply be copied to the terminal. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieb7lk1rf971adahu7k1.png) The only thing you need to consider is the name of the branch in __git push -u origin main__. In my case it will be __git push -u origin master__.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuq2b1n538aoqje2p7z7.png) CONGRATULATIONS! Now the files on GitHub.
Now you need to remember that when you make any changes to the files, you need to commit them.

Once the file is committed, it can be seen on GitHub.
You need to go to the file page and click on the "commits" button. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq3wvrdtx4932td977d2.png)
Next you need to click on the desired commit code. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdsw57qigzho8ih1t72x.png)
Here we see the changes made to the file. Red - what has changed. Green indicates changes. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bvt9wml48105q9cozvl.png)

Thank you for your attention! Hope this is helpful.