Codementor Events

Git and GitHub: How to get started?

Published Oct 02, 2020

Have you ever wrote code or wrote some documents and kept it on your system, and every time you make changes you keep the previous code/document file separately for future reference in case you want to go back and revert those changes. This makes a mess in your system as it will grow in number of files and will end up hard to track and maintain. Also another problem arises when you are in a different system or your current system crashes or your files get deleted mistakenly. You won’t be able to work on it and all your progress might go away. So what is the solution? Version control systems comes to rescue. It is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Today we’re are going to look at Git, a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. And also GitHub, a hosting platform for software development and version control using Git.

So to make it clear, Git performs operations to keep track of changes and history and save the files and a lot more in regards to version control and then GitHub is the server where your files are going to be saved so that you can access files easily online and it also has a lot of features. Lets now dive into the basics of how to use it, which is our goal to learn today.

Visit the official website of Git and download the latest version of Git and install it. After installing, launch the Git Bash and check the version of git using the command git --version and it should show you the latest version installed.

$ git --version
git version 2.22.0.windows.1

If it’s showing the version you installed, you have successfully installed it in your system.

  1. First-time Git Setup

The next steps would be to let git know whats your identity so that it knows who is writing and saving the changes. It helps to know when your working with multiple people in the same project to identify who made which changes. You do this by setting your user name and email address.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

The --global option will always use that information for anything you do on that system. If you want to override this with a different name or email address for specific projects, you can run the command without the --global option when you’re in that project.

  1. Init a Git Repository

A repository is a directory that contains your project work, as well as a few files(hidden by default) which are used to communicate with Git. Repositories

can exist either locally on your computer or as a remote copy on another computer.

To make a project a git repository:

  1. Open the project folder
  2. Open Git Bash in that project folder location
  3. Type git init
  4. Press enter and done. You have a git repository.
~/Documents/Workspace/my-tutorials/git-demo-project
$ git init
Initialized empty Git repository in C:/Users/Asus/Documents/Workspace/my-tutorials/git-demo-project/.git/

~/Documents/Workspace/my-tutorials/git-demo-project (master)
$
  1. Add the files

Create or make changes in the files on your project and add the files you need to save and want to keep track of.

  • To add a specific file type git add filename
  • To add all the files in the project type git add .
~/Documents/Workspace/my-tutorials/git-demo-project (master)
$ git add .

 ~/Documents/Workspace/my-tutorials/git-demo-project (master)
 
$

5 . Commit files

Git thinks of its data like a set of snapshots of a mini file system.Every time you commit or save the state of your project in Git, it basically takes apicture of what all your files look like at that moment and stores a reference to that

snapshot.

To commit with a message:

  • Type commit -m "Your message or commit name"
~/Documents/Workspace/my-tutorials/git-demo-project (master)
$ git commit -m "initial commit"
[master (root-commit) c8ace53] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.kt

~/Documents/Workspace/my-tutorials/git-demo-project (master)
$
  1. Integrating with GitHub

This step is needed only for the first time when setting up the project on GitHub. To begin with first you need to go ahead and sign up on GitHub if you don’t already have an account. The next steps after setting up your GitHub account would be:

  • Create a new repository from the menu on the GitHub website.
    new repository menu on github.com

  • Enter project name and description and also you could make the repository public or private.
    Creating a new repository on github.com/new

Create the repository and you should see a page like this:

new repository created view with instructions

That’s it. We are now ready to push(upload) our files into this GitHub repository. It already has the instructions on how to do it in the above image.

  1. Pushing project in to the hosting service (GitHub)

I am mentioning hosting service because there are other hosting services also available to use git with such as Bit Bucket or GitLab etc.

Now lets push the project onto GitHub. Since we already has created a git repository in our local system from the steps 1 to 5. Now its time to push it on the cloud. The steps include:

  1. Add the remote URL of the GitHub project.
git remote add origin https://github.com/Username/git-demo-project.git

This step is only needed once for the first time when the project is uploaded on GitHub.

  1. Push the changes to the origin master.
git push -u origin master

That’s it. Your project files will be uploaded on GitHub. It will look like this on your GitBash:

~/Documents/Workspace/my-tutorials/git-demo-project (master)
$ git remote add origin https://github.com/Tobibur/git-demo-project.git

~/Documents/Workspace/my-tutorials/git-demo-project (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 237 bytes | 237.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Tobibur/git-demo-project.git
 * [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Asus@LAPTOP-2F2HDSP8 MINGW64 ~/Documents/Workspace/my-tutorials/git-demo-project (master)
$

Note: The first time git push might ask you for GitHub username and password. So you must enter it to confirm your identity.

That’s it and when you go over to your GitHub repository page you should see your project files. Example:

my demo project repository on GitHub

After this any changes you make on your project hosted on GitHub, you just need to follow the steps 4, 5 and 7(2).

Conclusion

Git and GitHub is a very crucial tool for developing your software development journey. It will save you a lot of time and effort while working on your projects. It’s a skill set every developer must know. I hope this article provides you the basic on how to get started and make it easy for you the initial steps. I also left out certain topics such as branches, collaborations etc for making it easy for beginners. Please comment below if you have any queries and I would be happy to help out.

Hi, I am Tobibur. I am a Software Engineer from India. I like to write blogs, have discussions about topics that interest me and I tend to seek knowledge upon. Topics include such as Technology, Science, Philosophy and Religion. View all posts by Tobibur

Discover and read more posts from Tobibur Rahman
get started
post commentsBe the first to share your opinion
Show more replies