DEV Community

Cover image for Git & GitHub Collaboration
Silas Ochieng
Silas Ochieng

Posted on

Git & GitHub Collaboration

Hey there, fellow devs! πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»

Wondering how to get started with Git and GitHub? I’ve got you covered! If you're new to software development, you might have heard about Git and GitHub but aren't quite sure what they are or how to use them. This guide will explain these tools in simple terms and show you how to set them up, especially using Visual Studio Code (VS Code).

What Are Git & GitHub?

Git
Git is a version control system. Think of it like a time machine for your code. It tracks changes you make to your files, allowing you to go back to previous versions if something goes wrong. This is incredibly useful when multiple people are working on the same project, as it helps prevent conflicts and keeps everyone’s work organized.
GitHub
GitHub is a website that hosts Git repositories. It’s like a social network for developers, where you can store your code, collaborate with others, and share your projects. GitHub provides tools for code review, issue tracking, and project management, making it easier for teams to work together.

Setting Up Git and GitHub

Step 1:

Install Git

Download Git: Go to the https://git-scm.com/ and download the installer for your operating system.
Install Git: Run the installer and follow the prompts. You can usually keep the default options.
Step 2:

Create a GitHub Account

Go to https://github.com/ and sign up for a free account.
Follow the instructions to set up your profile.
Step 3:

Configure Git

After installing Git, you need to set up your identity. This is important for tracking who made what changes.

Open Git Bash (or Command Prompt).

Run the following commands, replacing the placeholders with your information:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
To check your configuration, type:

git config --list

Basic git commands

Start tracking a project

git init

Clone a repository

git clone https://github.com/username/repo.git

Stage changes

git add .

Commit changes

git commit -m "Descriptive commit message"

Check repo status

git status

View commit history

git log

Push to GitHub

git push origin main

Using Git with Visual Studio Code

Step 1:

Install VS Code

Download VS Code from the https://code.visualstudio.com/.
Install it by following the setup instructions.

Image description
Step 2:

Open Your Project in VS Code

Launch VS Code.
Open the folder containing your project by selecting File > Open Folder.
Step 3:

Initialize Git in Your Project

Open the integrated terminal in VS Code by pressing Ctrl + ` .

Initialize Git by running:

git init
This command creates a new Git repository in your project folder.

Step 4:

Making Changes and Tracking Them

Add Files: Create or edit files in your project.

Stage Changes: To prepare your changes for committing, use:

git add .
Commit Changes: Save your changes with a message describing what you did:
git commit -m "Your descriptive message"
Step 5:

Pushing Changes to GitHub

Create a New Repository on GitHub: Go to GitHub and click on the "+" icon in the top right corner. Select "New repository" and follow the prompts.

Link Your Local Repository to GitHub: In the VS Code terminal, run:

git remote add origin https://github.com/your-username/your-repo.git
Push Your Changes: To upload your local changes to GitHub, run:

git push -u origin main
Collaborating with Others
Forking a Repository
If you want to contribute to someone else's project:

  • Go to the original repository on GitHub.

  • Click the "Fork" button to create a copy in your account.

  • Clone your forked repository using:

git clone https://github.com/your-username/forked-repo.git

  • Creating a Pull Request After making changes in your fork, push them to GitHub. Go to the original repository and click "Pull Requests." Click "New Pull Request" and follow the instructions to submit your changes.

Handling Merge Conflicts

Sometimes, if two people edit the same part of a file, Git will have trouble merging. If this happens:

  • Open the conflicted file in VS Code. Look for lines marked with <<<<<<<, =======, and >>>>>>>. Decide which changes to keep. Save the file, stage it, and commit the resolved changes.

Conclusion

Understanding Git and GitHub is essential for modern software development. By following this guide, you can set up your environment and start collaborating effectively. Remember, practice is key, so don’t hesitate to experiment and explore these tools further!

If you have any questions or need help, feel free to reach out! Happy coding!

Top comments (0)