Beginner's guide to Git and GitHub

Beginner's guide to Git and GitHub

What is Git??

Git is a powerful piece of version control software that helps you to keep track of different versions of your code, collaborate on your code with other people, and experiment with new changes to your code. It's used in industry, in many open-source projects.

What is GitHub??

GitHub is a web-based hosting service for version control using Git. It is mostly used for computer code. It offers all of the distributed version control and source code management functionality of Git as well as adding its own features.

Uses of Git??

  • Keeps track of changes to code
  • Collaborate with any number of people without any limits.
  • Synchronizes code between different people
  • Test changes to code without losing the original
  • Tracking History and changes of a file over time
  • Revert back to old versions of code

FORK AND CLONE

When you go to Github.com, you will see some projects from other people that you will be interested in. You can click on Fork to create a copy of this project on your own repository.

Screenshot 2022-01-29 122117.png

Screenshot 2022-01-29 114320.png To clone a repository to your computer, open your terminal and 'cd' to the folder that you want to put that repository in, then clone by copying this link and pasting it into your terminal by this command:

git clone <url of the repository>

GIT ADD

It adds the file to the "staging area". If you want to add the file to the repository, use this command:

git add <filename>

Sometimes, you want to add all the files to the directory you are working on at the same time. It sometimes gets frustrating when you type all the file names sequentially, so you can use this command instead:

git add .

Difference between " git add . " and " git add * "

  • git add . – It has no special meaning in your shell, and thus Git adds the entire directory recursively, which is almost the same but includes files whose names begin with a dot.
  • git add * – It means add all files in the current directory, except for files whose names begin with a dot. This is your shell functionality and Git only ever receives a list of files.

GIT COMMIT

Once you have added all the files that you want to save now you need to actually make the save. This command commits changes to the head on the local repository.

git commit -m “message”

where the message is usually some summary of the changes that you have made in this particular change so that if anyone has to go back and look at what we call the “commit history ” they will be able to see all the changes, when you have made the changes or what changes you have made.

  • git commit -am “message” – adds and commits in the same repository

GIT PUSH

Once you have finished committing, all of the changes are now in HEAD. But when you refresh your repository on GitHub, you couldn't find any changes in the repository. To make those changes to the repository on GitHub, you have to push it with the command:

git push
  • git push – sends committed changes to the remote repository
  • git push origin master – This sends changes to your remote repository on the master branch.

GIT PULL

Sometimes it may happen that the remote repository has added some new files, deleted something, or made some new changes and your local repository is not synchronized. Then you cannot push your code to GitHub. To keep everything in sync and up to date, we have to pull down the new code from the remote repo to the local repo to make sure everything is the same. We can use this command for that purpose:

git pull

It retrieves the changes from the remote repository.

BRANCHING

  • Branch is a version of the repository.
  • Each branch has its own commit history and current version.

GIT BRANCH

  • git branch – It shows all the branches of code.
  • git branch <branch_name> – It creates a new branch.
  • git checkout <branch_name> – It switches to (”checkout”) a new branch.

GIT MERGE

Now if you want to merge two different branches together, you use this command:

git merge <branch_name>

It merges the branch 'branch_name' with the current branch. So basically, every commit that was made in the new branch will be brought to the master branch.

Let's understand some more Git commands

  • git init – Create a new repository on your machine
  • git remote add origin <server> – This command connects your local repository to the remote repository.
  • git status – It shows the current status of the repository
  • git log – It shows a history of commits and messages
  • git reset --hard <commit> – It reverts the code back to its previous commit
  • git reset --hard origin/master – It reverts code back to the remote repository version. It should be reserved for undoing changes on a private branch.
  • git revert – It is used to undo changes on a public branch, and git reset

So, that was all about the basics of Git and GitHub that will help you get started with your projects and collaborations. It is my first blog so do let me know if you have any feedback in the comments below.