DEV Community

Cover image for Git and GitHub for beginner
Hanna
Hanna

Posted on

Git and GitHub for beginner

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) 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

  • git --init

Alt Text Create an empty Git repository or reinitialize an existing one.

cd .git
ls
Enter fullscreen mode Exit fullscreen mode

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

  • git status

Alt Text 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 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 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 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 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 The .gitignore file must be committed.

  • git branch

Alt Text We use this command to see which branch we are in now.

  • git branch branchname

Alt Text 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 You can use this command to delete a branch.

  • git checkout branchname

Alt Text With this command you can go to the branch.

  • git checkout -b new

Alt Text With this command you can immediately create a new branch and go to it.

  • git merge branchname

Alt Text 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

  • 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 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 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
Next you need to click on the desired commit code. Alt Text
Here we see the changes made to the file. Red - what has changed. Green indicates changes. Alt Text

Thank you for your attention! Hope this is helpful.

Top comments (0)