DEV Community

Cover image for A Beginner's Guide to Git, GitHub
Feven Seyfu
Feven Seyfu

Posted on

A Beginner's Guide to Git, GitHub

Git and GitHub have saved my forgetful self on multiple occasions. Yet, if you had asked me about them when I first started coding, my answer might have been different. For beginners on the coding journey, Git and GitHub can be intimidating. However, grasping version control and collaboration tools makes code management and sharing easier. Reflecting on my programming learning curve, one of my regrets is not diving into Git and GitHub earlier. This delay has led to the loss of some projects. Hence, I am writing this article to guide you on the basics and how to set up and use Git and GitHub effectively.

What is Git and Github?

Although Git and GitHub may sound similar, they serve different purposes.Git is a version control system that tracks changes in your code by maintaining a history of modification. So basically, if you have a working program and want to add a new feature, Git allows you to save your code history. This way, if you encounter issues with your changes, you can easily revert to a point where your code was functioning correctly..

GitHub, on the other hand, is a platform that utilizes Git for collaborative coding, allowing multiple developers to work on projects concurrently.It is a web-based platform which allows you to host, share, and contribute to code repositories(project folders).

Setting Up Git

Before diving into Git commands, you need to set it up on your machine:

  1. Installation: Download and install Git from git-scm.com.
  2. Configuration: Set your name and email using the following commands: git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

Git Basics

  1. Repository (Repo): A Git repository is a storage space that contains your project's files and the history of changes. To create a new repository, use:

git init

2.** Cloning a Repository** : To copy an existing repository to your local machine, use:

git clone repository_url
Enter fullscreen mode Exit fullscreen mode
  1. Committing Changes: After making changes, commit them(Committing in Git is like taking a snapshot of your project at a specific point in time) use the command:

   git add .
   git commit -m "Your commit message"

Enter fullscreen mode Exit fullscreen mode

Tip: when committing, use commit messages that are descriptive and meaningful. Read this article for more.

  1. Branching: Create a new branch to work on a feature or bug fix:
   git branch -b  new_branch
   git checkout new_branch
Enter fullscreen mode Exit fullscreen mode

Tip: replace new_branch with you preferred branch name

  1. Merging: once done with your feature and happy with result merge/combine changes from one branch to another usually it will be main or any development branch:
 git checkout destination_branch
   git merge source_branch
Enter fullscreen mode Exit fullscreen mode

GitHub Basics

So far we have worked on our project locally and tracked the changes with git but now we want to host our project online so that it can be accessible to everyone else in order to be able to collaborate, below are the steps we can take.I assume You already have a Github account if now visit github to register.

1.** Repository Creation:** First thing is first visit GitHub, click "New" to create a new repository, and follow the instructions to insert name,description to your project and you can also setup a readme.

  1. Cloning from GitHub: If you want to work on an existing project you can copy the repository URL and use:

git clone repository_url.git

Tip: the URL has the format https://github.com/username/repository_name.git navigate to repository->code(drop down) and copy link

Image description

  1. Pushing Changes to GitHub: once you have made your changes and committed in git you can push to github. When you push in Git, you are essentially uploading your local commits to the remote repository. This ensures that others can see the latest version of your project.
   git push origin branch_name
Enter fullscreen mode Exit fullscreen mode

Tip: please use descriptive branch names and explore more here.

4.** Pull Requests:**Once you push your changes before merging to the main branch you need to create a pull request explaining the changes which serves as a formal request for the repository maintainers to review the proposed changes before they are merged.

Git and Github are essential tools for developers. With the basics covered, you can confidently manage your projects, collaborate with others, and contribute to open-source initiatives and Inspired explore more commands and collaborate with the vast developer community to enhance your skills.Happy coding!

Top comments (0)