Skip to main content

Understanding Git and GitHub: The Backbone of Collaborative Coding

Published 10 min readMarkdown
Understanding Git and GitHub: The Backbone of Collaborative Coding

Git and GitHub are integral to the world of software and IT, transforming the way developers manage code and collaborate on projects. Git, a powerful version control system, manages and tracks changes to the codebase. At the same time, GitHub, a platform built on top of Git, provides a collaborative environment where developers can work together on projects. In this blog post, we'll delve into both tools, highlighting their key features and workflows, and explore how they work together to streamline the development process.

Understanding Git and GitHub

Git

Git is a distributed version control system developed by Linus Torvalds. A version control system like Git allows multiple developers to work on the same project without overwriting each other's changes. Git maintains the history of changes to codebase by different developers making it easy to track issues and revert to previous versions of code.

Key Feature of Git:

  • Version Control: Git provided robust version control capabilities making it easy for developers to track their changes over time. This helps them to keep a history of their work, and the work of other collaborators, making it easy to compare changes and and roll back to previous versions of code.

  • Distributed Nature: git is a distributed version control system that means each developer has a full copy of the code on their local machine. This helps them to work offline whenever they want and reduces the dependency on the server.

  • Branches for Parallel Development: Branching is one of the most useful features of Git. It allows developers to create their separate branches and work independently on new features, experimenting and fixing bugs. It keeps the main codebase safe and branches can be merged back to the main branch after the development of features or bug fixes are completed.

  • Commit: Commit is the snapshot of a project at a specific point in time. Each commit holds the record made to the files, along with the details of the person who made the commit, the timestamp of the commit, and the message describing the changes in the commit.

Github

GitHub is a web-based cloud platform that provides git repository hosting, tools for version control, and collaborative software development and management. GitHub is built on top of Git with the addition of a web interface and social networking features.

Key Feature of Github:

  • Repository Hosting: Github allows us to host our git repositories online making it accessible to collaborators and other team members. Repositories can be public or private. It makes the code available on the server making it easy for the users to work even if they don’t have access to their local machine.

  • Pull Requests: Collaborators or team members can propose changes, and request code reviews to code using the pull request feature of GitHub. It makes sure proper discussions and reviews of potential issues between the developers take place before merging any new feature in the main branch.

  • Issues: Github offers an issues feature that helps report bugs, suggest features, and discuss tasks. Different issues can be assigned to different team members making it easy to manage workflow.

  • GitHub actions: It is a powerful automation provided by GitHub that supports Continuous Integration(CI) and Continuous Deployment(CD). It can be used to automate the workflow enabling automatic development, testing, and deployment of applications.

  • Social Coding: GitHub incorporates social features like following other developers, starring repositories, and contributing to open-source projects. It’s a platform where developers can showcase their work, collaborate, and build a reputation.

Git Lifecycle

Git lifecycle refers to the journey of a project inside the repository, involving stages like creation, tracking changes inside files, adding them to the staging area, committing, collaborating through branches, merging, and pushing to remote. Let’s see in detail about each stage.

  • Initialization: The git lifecycle starts after we initialize a directory as a git repository using the git init command. This marks the start of the version control for that particular directory and all the changes are then tracked.
  • Working Directory: The working directory is the project directory in our local machine. In this directory, we create new files and make changes to existing files. These files have two stages:

    • Untracked Files: Files that are newly added to the directory.

    • Modified Files: Files that have been changed but not yet committed.

  • Staging Area: The staging area is the intermediate area between the working directory and the online repository, we can group changes here before committing them to the repository. We use the git add command to send changes made to a file from the working directory to the staging area. And git status command is used to view the status of the files in the directory.

    • git add .: sends all the files updated to the staging area.

    • git add [file-name]: sends the particular file mentioned to the staging area.

  • Local Repository: After changes are sent to the staging area, developers can commit the changes to the local repository. A local repository is where git stores the history of the project including all the changes committed.

    • git commit -m “commit message”: Used to commit the changes to the local repository from the staging area. The commit message that we add should be relevant to the changes we are committing so that it’s easier to track the changes.

    • git log: To view logs of all the changes made to the local repository. It has information for all the commits along with the author name, date and time of commit, commit ID, and the commit message.

  • Remote Repository: The remote repository is the version of our project that is hosted on the server. This the where GitHub enters the git lifecycle. We host our project on the server so that other developers can collaborate on the project.

    • git remote add [remote repo name] [repository url]: this command connects the remote repository to our local repository. We have to provide the URL of the remote repo and give a name to the remote repository so that it can be used afterward. We can give any name that we like.

    • git push [remote repo name] [branch]: this command is used to push all the committed changes from the local repository to the specified branch in the specified remote repository.

    • git pull [remote repo name] [branch]: this command fetches the changes from the remote repository to the local repository, this is important to check that the local repository and the remote repository are at the same level to avoid merge conflicts.

Git Branches

Git branches allow different developers to work on the same project on different features and functionality at the same time with the use of branches without affecting the main codebase. Once the feature they were working on is complete and all the code is tested for potential bugs the code can be merged into the main codebase which is generally the main branch in the repository.

  • git branch [branch-name]: command to create a new branch.

  • git checkout [branch-name]: command to switch to a different branch.

  • git branch -M [new-branch-name]: command to rename a branch.

  • git branch -d [branch-name]: command to delete a branch locally.

  • git merge [branch-name]: this command is used to merge changes from one branch to another. Using this we can combine changes from different branches and merge them into the main branch together.

Consider this example, here we are working on a project having the main codebase in the main branch and we have created a new branch new_feature to work on a feature or functionality. So After we have completed the development we can merge the feature to the main branch using the following steps.

  • Switch back to the main branch

    git checkout main

  • Then inside the main branch we run the following merge command.

    git merge new_feature

    This will merge all the changes from the new_feature branch to the main branch.

Merge Conflict

Merge conflict occurs when the two branches have made changes to the same line of code. Then there is a conflict when we merge the branches because git is not able to decide which changes it should keep. We then have to decide explicitly which changes we want to keep or which to discard or we also have the option to combine changes.

Consider we are working on a branch named dev and made some changes in the example.html file and someone else is working on the feature branch and they also made changes to the example.html file. Now when we try to merge the feature branch in the dev branch git will detect automatically that the same lines have been changed in both the branches and will mark the conflicting lines in the file with a special marker showing changes for those lines from both the branches and provides us the option to select which changes to keep or discard.

Pull Before Push: When multiple developers are collaborating on a project, it's recommended to always run ‘git pull’ before pushing your changes to the remote repository. This ensures that your local repository is up to date with all the changes made by other developers in the remote repository. By doing this, you reduce the likelihood of conflicts when you push your changes, as your local branch will already include the latest updates from others.

Git Reset

Git reset is a powerful Git command that helps to undo the changes we made in our repository and reset the repository to a specific commit. Whether we want to undo staged changes, modify the commit, or permanently remove the commit changes git reset can help us.

Git provides three modes of reset --soft, --mixed(default), and --hard with each having a different level of impact.

  • git reset --soft: This command is used to change the branch head pointer to a specified commit. This is used when we want to undo a commit but the changes made in those commits are not removed completely but sent back to the staging area.

    git commit head~”commit index”

    The changes in the commits before the provided commit index will be discarded from the local repository and sent back to the staging area.

  • git reset --mixed/ git reset: This is the default reset type that git provides. This changes the branch head pointer to the specified commit. This is used when we want to undo a commit and want to edit the changes in that commit before committing them.

    git commit HEAD~”commit index”

    All the changes in the commit before this commit index will be sent back to the working directory.

  • git reset --hard: This reset type is used when we want to undo a commit and remove the changes made in that commit from the working directory. This reset is destructive and irreversible so, it must be used with caution.

    git reset --hard HEAD~”commit index”

Git revert

Git revert is used to revert the changes we made in a particular commit and create a new commit to remove those changes. For this either we can pass the commit ID or we can use the head and pass the commit index number which we can check in the git log, the head points the last commit we just made.

git revert “commit ID”

git revert HEAD~”commit index number”

Git Rebase

Git rebase offers similar functionality to Git merge, but it operates differently. While both commands allow us to combine branches, Git rebase moves or combines a sequence of commits onto a new base commit. This approach enables you to integrate branches without creating unnecessary merge commits. However, unlike Git merge, which preserves the original order of changes in the file, Git rebase rewrites the commit history, potentially altering the order of changes.

Consider we are working on a project and we created a feature branch to add some new features to your project and after completing the code and testing of that feature we want to merge it into the main. So here are the steps to do it with rebase

  • Check out the feature branch and then rebase the feature branch to main branch.

    git rebase main

    This will apply each commit from the feature branch to the main branch one by one. Git will automatically merge all the commits to the main branch and if a conflict arises we have to resolve that conflict and then continue the rebase using the below command.

    git rebase --continue

  • Once the rebase is completed, we check out the main branch and merge the feature branch to the main branch.

    git rebase feature

Conclusion

Git and GitHub have revolutionized the way software is developed and shared. They offer a powerful combination of version control, robust collaboration tools, code management, and automation features that streamline the development process. Together, they form the backbone of modern software development, enabling developers to build, share, and maintain code collaboratively and efficiently. Whether you're working on a small project or a large one, understanding and mastering Git and GitHub is essential.

No comments yet. Be the first to comment.

More from this blog