Written by Abdul Rafay

Managing Your Code with Git and GitHub

As a Software Engineer, managing your code is a critical part of the development process. From adding new features to squashing bugs, all while ensuring your code is readable and ready for others to contribute, you need a robust system to keep track of all these changes. Enter Git and GitHub.

Before Git

Before adopting Git and GitHub, my workflow was chaotic. Every time I made a change, I’d end up creating a new folder to track that change. But keeping track of which feature was added in which folder was a nightmare. It was only after much frustration that I decided to learn Git and GitHub to streamline my development process.

The Significance of Git in Development

Git and GitHub offer indispensable benefits for developers:

Personal Insight

The real value of Git and GitHub became clear after I lost a significant amount of work due to poor version control. After a computer crash wiped out my work, I adopted Git and GitHub religiously. This change has safeguarded my code and projects ever since.

Understanding Git

Git, a powerful version control system, empowers collaboration and project management. Created by Linus Torvalds in 2005, Git is now widely used in both commercial and open-source projects. Its versatility across various operating systems and IDEs ensures that it’s deeply embedded in the development community.

Unveiling GitHub

GitHub, a dynamic code hosting platform, makes collaboration and version control effortless.

By allowing users to publish and share their projects, GitHub enables teams to collaborate, innovate, and collectively contribute to various software development projects.

Here are some key concepts you’ll need to master GitHub and version control for a smooth development experience:

Repositories

Branches

Commits

Pull Requests

Push Requests

Git Commands for Version Control

Here are some essential Git commands to get you started with version control:

Creating a Repository

To create a repository on GitHub:

  1. Sign up or log in to GitHub.
  2. Click the “New” button to create a new repository.
  3. Fill in the repository details, including the name, description, and license.
  4. After submission, your repository will be created, and you’ll be redirected to the repository page.

Cloning a Repository

To clone an existing repository to your local machine:

git clone <repository URL>

Creating a Branch

Create a new branch with the following command:

git checkout -b <branch_name>

Switching Branches

To switch between branches:

git branch
git checkout <branch_name>

Checking Status

To check the status of your repository:

git status

Adding Files

To stage files for a commit:

git add <file_name>
git add *

Committing Changes

To commit your changes with a message:

git commit -m "Your message here"

Pushing Changes

To push your changes to the remote repository:

git push --set-upstream origin <branch_name>
git push

Merging Branches

To merge one branch into another:

git checkout main
git merge <branch_name>

Pulling Changes

To pull the latest changes from the remote repository:

git pull origin main    # Pull changes from the main branch

By mastering these Git commands, you’ll improve your development workflow, simplify collaboration, and effectively manage your code.

Reverting Commits in Git

At some point, you might need to revert a commit that breaks your code. Here’s how you can revert commits from the main branch or any other branch.

Reverting a Commit from the Main Branch

If you need to revert a commit on the main branch, especially when it causes issues, follow these steps:

  1. Identify the commit you want to revert by using:

    git log
  2. Once you find the commit hash, you can revert it using:

    git revert <commit_hash>

    This will create a new commit that undoes the changes from the selected commit.

  3. Push the changes to the remote repository:

    git push

Reverting a Commit from Any Branch

To revert a commit from any branch, you can use the same git revert command:

  1. Checkout the branch where the commit exists:

    git checkout <branch_name>
  2. Identify the commit you want to revert using git log.

  3. Revert the commit:

    git revert <commit_hash>
  4. Push the changes to the remote branch:

    git push

Reverting Multiple Commits from the Main Branch

If you’ve committed multiple changes directly to the main branch and need to revert them, you can follow these steps:

  1. To revert multiple commits, use the git log command to find the commit hash of the first commit you want to undo.

  2. Use the git revert command with the range of commits to undo:

    git revert <commit_hash_1>^..<commit_hash_n>

    This command reverts all the commits between <commit_hash_1> and <commit_hash_n>, including both ends of the range.

  3. Push the changes to the remote repository:

    git push

Reverting Changes to Match a Backup Branch

If the code in your main branch breaks but you want to revert to the state of a backup branch, follow these steps:

  1. Checkout the backup branch:

    git checkout <backup_branch>
  2. Merge the changes from the backup branch into the main branch:

    git checkout main
    git merge <backup_branch>
  3. Push the changes to the remote repository:

    git push

This will restore the code from your backup branch to the main branch.

Managing Application Versions with Release Pages

Understanding Versioning

Branches represent different versions of your application. Once features are developed and tested, they are merged into the main branch for deployment.

Leveraging Release Pages

Release pages help document each version of your application, including its features, bug fixes, and improvements.

Creating a Release

  1. Go to the release page and fill in the relevant details for the new version.
  2. After you’ve filled out all the information, hit the “Publish” button to make the release available on GitHub.

Final Thoughts

Git and GitHub are invaluable tools for developers. They help streamline code management, enable seamless collaboration, and ensure that you always have control over your project’s history. Don’t hesitate to reach out if you have any questions or thoughts on this.

I’d love to hear from you! Until next time! ❤️

💬 Join the Discussion

Share your thoughts and engage with the community

Loading comments...