DEV Community

Cover image for Git and Github for beginners
zainaboyedeji
zainaboyedeji

Posted on • Edited on

Git and Github for beginners

Introduction

Git and GitHub are essential tools in modern software development. They allow developers to manage different versions of their code, collaborate effectively, and maintain a streamlined workflow.

What is Git?

Git is a distributed version control system (VCS) that helps developers track changes in their codebase, collaborate efficiently, and manage project versions. The term "distributed" means that each developer has a complete copy of the project repository, allowing them to work independently and merge changes when needed.

Key Features of Git:

  • Version Control: Track and revert changes in your project.
  • Collaboration: Multiple developers can work on the same project without conflicts.
  • Branching & Merging: Developers can create separate branches for features and merge them into the main project.
  • Offline Access: Work on your code without an internet connection.

What is GitHub?

GitHub is a web-based platform built on Git that provides a space for developers to store, manage, and share code. It enhances Git by offering a graphical interface, project management tools, and collaboration features.

Key Features of GitHub:

  • Repositories (Repos): Store your project files and history.
  • Branching & Merging: Create branches for new features and merge them after review.
  • Pull Requests: Submit code changes for review before merging.
  • Issues & Project Management: Track bugs and tasks.
  • GitHub Actions: Automate workflows like testing and deployment.

Other alternatives to GitHub include Bitbucket and Gitlab.

What is Git Bash?

Git Bash is a command-line interface (CLI) that enables interaction with Git. It is especially useful for Windows users as it provides a Unix-like terminal for executing Git commands.

Getting Started with Git and GitHub

1. Installing Git

To start using Git, you need to install it on your local machine.

You can download Git here. Ensure to choose based on your operating system.

Image description

2. Setting Up Git

Once installed, configure Git with your name and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

This associates your commits with your identity.

3. Creating a GitHub Account

  • Go to GitHub enter the same email you configured above then click on the Sign up for GitHub button.

Image description

4. Creating a Repository on GitHub

a. Log in to GitHub.
b. Click the "+" icon and select New repository.

Image description

c. Enter a repository name, a description (optional), and choose the visibility (public or private).

Image description

d. Click Create repository.

Image description

e. After creating a repository, you are directed to this page.
Image description

5. Connecting a Local Repository to GitHub

If you have an existing project, navigate to its directory and initialize Git:

cd /path/to/your/project
git init
Enter fullscreen mode Exit fullscreen mode

Then, connect it to your GitHub repository:

git remote add origin https://github.com/zainaboyedeji/git-github-tutorial.git
Enter fullscreen mode Exit fullscreen mode

Image description

6. Uploading Files to GitHub

a. Add all files to the staging area:

   git add .
Enter fullscreen mode Exit fullscreen mode

b. Commit the changes:

   git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Image description

c. Push to GitHub:

   git push
Enter fullscreen mode Exit fullscreen mode

Image description

d. Your new repository will contain your initial commit, just like the screenshot below.

Image description

GitHub Flow: A Simple Workflow for Collaboration

a. Create a Branch

git checkout -b feature-branch
Enter fullscreen mode Exit fullscreen mode

This ensures that changes are made separately without affecting the main branch.

Image description

b. Make Commits

git add .
git commit -m "Add new feature"
Enter fullscreen mode Exit fullscreen mode

Frequent commits help keep track of changes.

Image description

c. Push Your Branch to GitHub

git push origin feature-branch
Enter fullscreen mode Exit fullscreen mode

Image description

d. Open a Pull Request

On GitHub, navigate to your repository and open a Pull Request (PR). Add a description and request reviews.

a. Click on the New Pull Request button
Image description

b. Select the branch you want to compare against the main branch, then click on 'Create Pull Request' button.
Image description

Image description

c. Add an appropriate description to your PR explaining the changes made in that branch, then click on the 'Create Pull Request' button.
Image description

d. You are then directed to this page, where you can review the changes and click on 'Merge Pull Request' once you're done.
Image description

e. You review before merging, ensuring all checks have passed and there are no conflicts.

Image description

f. The pull request has been successfully merged.

Image description

Advanced Git Features

1. Rebasing

Rebasing moves or combines commits to a new base, making your commit history cleaner.

git rebase main
Enter fullscreen mode Exit fullscreen mode

2. Resolving Merge Conflicts

If conflicts arise when merging, Git will notify you. Open the conflicting files, make necessary changes, then:

git add .
git commit -m "Resolved merge conflict"
git push origin main
Enter fullscreen mode Exit fullscreen mode

3. Forking and Contributing to Open Source

  1. Fork a repository on GitHub.
  2. Clone your forked repo:
   git clone https://github.com/your-username/forked-repo.git
Enter fullscreen mode Exit fullscreen mode
  1. Make changes and push them.
  2. Open a pull request to contribute.

Resources to Learn Git and GitHub

If you want to dive deeper into Git and GitHub, here are some helpful courses and articles:

Conclusion

Git and GitHub are powerful tools that streamline version control and collaboration. Whether you're working on personal projects or contributing to open-source, mastering Git and GitHub will significantly improve your development workflow.

🤝 Contributing

Found this helpful?
Consider sharing it with your network!
If you spot any errors or have suggestions for improvement, feel free to leave a comment below.

Happy coding! 🚀

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more