Previously, I wrote a blog post listing all the basic Git commands: from initializing a repository to pushing your code to a remote repository. After re-reading that post, I realized that I also need to show these commands in action and discuss how they integrate with a remote repository. That’s exactly why I’m writing this blog.
For this post, I’ll be using GitHub as the remote repository since it’s the most accessible option for most developers, including myself. I’ll also cover practical workflows that developers use daily, from setting up a Git repository to collaborating with others using GitHub.
Now, let’s start by answering two fundamental questions: What is Git? and What is GitHub?
Git is a version control system that allows developers to track changes, manage different versions of their code, and collaborate efficiently without overwriting each other’s work. It works locally, meaning you can commit changes, create branches, and revert to previous versions even without an internet connection.
GitHub, on the other hand, is a cloud-based platform that hosts Git repositories, enabling developers to store, share, and collaborate on projects more effectively. It acts as a remote home for your Git repositories, providing tools for code reviews, issue tracking, and team collaboration. Together, Git and GitHub streamline development workflows, making it easier to manage projects, whether you’re working solo or as part of a team.
Setting Up Git
Before diving into Git commands, it’s essential to set up your local environment correctly. This ensures that all changes are tracked properly and attributed to the right user. Proper setup also lays the groundwork for seamless collaboration when working with others.
To check if Git is installed on your system, run:
git --version
If Git is not installed, download and install it from git-scm.com. After installation, configure your user details:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
This setup ensures that all your commits will be associated with your identity, which is especially important when collaborating on shared repositories.
Initializing a Git Repository
Once Git is installed and configured, the next step is to start tracking a project. Whether you’re starting a new project or adding version control to an existing one, Git makes it easy to track changes over time.
To initialize a new Git repository, navigate to your project folder and run:
git init
This creates a hidden .git
folder, which allows Git to track changes in the project.
To check the status of your repository and see which files are being tracked, use:
git status
This command provides a clear overview of which files have been modified, staged, or are untracked.
Staging and Committing Changes
Once a repository is initialized, it’s time to start tracking changes. Every modification to a file doesn’t automatically get saved in Git. Instead, changes must first be staged before being committed.
To stage a specific file for commit:
git add <filename>
To stage all modified and new files:
git add .
After staging, commit your changes with a descriptive message:
git commit -m "Added feature X"
Each commit creates a snapshot of your code, allowing you to roll back to previous versions if necessary.
Branching and Merging
Branching allows developers to work on different features or fixes simultaneously without interfering with the main project. Once changes are complete, they can be merged back into the main branch.
To create and switch to a new branch:
git checkout -b new-feature
Once work is completed, merge the branch back into the main branch:
git checkout main
git merge new-feature
To delete a branch after merging:
git branch -d new-feature
Fetching and Pulling Changes
To keep your local repository updated with the latest changes from the remote repository, you can use fetch
and pull
.
Fetch updates your local repository with remote changes but doesn’t apply them:
git fetch origin
Pull fetches updates and merges them automatically:
git pull origin main
Checking Differences Between Versions
To compare changes between commits, branches, or the working directory:
git diff
To see differences between the last commit and current changes:
git diff HEAD
To compare two branches:
git diff main new-feature
Viewing Commit History
Git keeps a log of all commits, making it easy to track changes over time.
To view commit history:
git log
For a more compact log with one-line messages:
git log --oneline
To see commit history for a specific file:
git log -- <filename>
Connecting to a Remote Repository on GitHub
Now that your project is tracked with Git, you’ll likely want to store it remotely to collaborate with others or ensure it’s backed up. This is where GitHub comes in.
To connect your local repository to GitHub:
- Create a repository on GitHub.
- Copy the repository URL.
- Link your local Git repository to GitHub:
git remote add origin https://github.com/your-username/your-repo.git
Verify the remote connection:
git remote -v
Push your code to GitHub for the first time:
git push -u origin main
Now your project is safely stored on GitHub, making it accessible to you and your team from anywhere.
Working with Pull Requests on GitHub
When working in a collaborative environment, direct changes to the main branch are discouraged. Instead, best practice involves using feature branches and pull requests to manage code updates.
To create a new feature branch:
git checkout -b feature-branch
Once changes are made and committed, push the branch to GitHub:
git push origin feature-branch
On GitHub, navigate to your repository and create a pull request (PR). Request reviews from teammates, and once approved, merge the PR into main
and delete the feature branch.
By now, you should have a solid understanding of how Git and GitHub work together to manage code effectively. From setting up your local environment to collaborating via pull requests, these tools play a crucial role in modern software development.
Start implementing these workflows in your projects, and over time, mastering Git will become second nature. Happy coding!