Git and SCM fundamentals

ADITYA PAWARADITYA PAWAR
12 min read

What is Git?

Git is a distributed version control system (DVCS) that is commonly used for tracking changes in source code during software development. It was created by Linus Torvalds in 2005 and has since become one of the most popular tools for managing and collaborating on code among developers.

Here's a breakdown of some key concepts and features of Git:

  1. Version Control System: Git allows multiple developers to work on a project simultaneously while keeping track of changes to the codebase. It enables you to maintain a history of modifications, which is useful for collaboration, bug tracking, and understanding how the code has evolved over time.

  2. Distributed: Git is a distributed version control system, which means that each developer has their own complete copy of the code repository, including its full history. This allows developers to work independently, even offline, and later synchronize their changes with others.

  3. Commits: In Git, a commit is a snapshot of the code at a specific point in time. Each commit represents a set of changes made to the code. Developers create commits to save their work and to provide a clear and organized history of the project.

  4. Branches: Git allows developers to work on different aspects of a project simultaneously by creating branches. Each branch is a separate line of development. Branches make it possible to experiment with new features or fixes without affecting the main codebase. Once changes on a branch are complete, they can be merged back into the main branch.

  5. Merging: Merging is the process of combining changes from one branch into another. Git provides tools to manage and automate this process, ensuring that changes are integrated smoothly and conflicts are resolved.

  6. Pull Requests (or Merge Requests): In collaborative development environments, developers often use pull requests (or merge requests) to propose changes to the main branch. These requests provide an opportunity for code review and discussion before changes are merged.

  7. Remote Repositories: Git allows for the creation of remote repositories, which are hosted on servers. Developers can push their changes to these remote repositories and pull changes made by others. Popular platforms for hosting Git repositories include GitHub, GitLab, and Bitbucket.

  8. GitHub and GitLab: GitHub and GitLab are web-based platforms that provide hosting for Git repositories along with additional collaboration features. They offer tools for code review, issue tracking, and project management.

  9. Command Line and GUI: Git can be used through the command-line interface (CLI) or graphical user interfaces (GUIs) that provide a more user-friendly way to interact with Git's features.

In summary, Git is a powerful tool that enables developers to collaborate effectively, track changes, and manage the development process of software projects. Its distributed nature, branching model, and integration with platforms like GitHub have made it an integral part of modern software development practices.

What is GitHub?

GitHub is a web-based platform that provides hosting for Git repositories and offers a wide range of collaboration and project management features. It is one of the most popular platforms for hosting and managing software development projects, both for open-source and private codebases. Here are some key aspects of GitHub:

  1. Git Repository Hosting: GitHub allows you to create Git repositories for your projects. These repositories can store your codebase, including all the commits, branches, and history.

  2. Collaboration: GitHub facilitates collaboration among developers by providing tools for code review, issue tracking, and discussions. Multiple developers can work on the same repository simultaneously, contributing changes through branches and pull requests.

  3. Pull Requests: A fundamental feature of GitHub is the pull request (PR). A pull request is a way to propose changes made in one branch of a repository to be merged into another branch, often the main branch. Pull requests provide a space for code review and discussions about the proposed changes before they are merged.

  4. Code Review: GitHub allows developers to review code changes within pull requests. Reviewers can leave comments, suggest improvements, and approve changes before they are merged. This process helps maintain code quality and catch potential issues.

  5. Issue Tracking: GitHub provides an issue tracking system where you can create, assign, and manage tasks, bugs, and feature requests. This is useful for keeping track of the development process and organizing work.

  6. Project Boards: GitHub's project boards allow you to organize tasks and issues into customizable boards with columns representing different stages of the development process. This visual approach helps teams manage their workflow.

  7. Wikis and Documentation: Repositories on GitHub can have associated wikis and documentation, which can be helpful for providing project-related information, guidelines, and usage instructions.

  8. Actions and Workflows: GitHub Actions allows you to automate various tasks, such as building, testing, and deploying your code, by creating custom workflows. This feature streamlines development and ensures consistent processes.

  9. GitHub Pages: GitHub Pages is a feature that enables you to publish web content directly from your repository. It's often used for hosting project documentation, websites, and other static content.

  10. Security and Integrations: GitHub offers security features like vulnerability scanning and dependency management to help maintain the security of your code. It also integrates with various third-party tools and services to enhance the development workflow.

GitHub has played a significant role in the open-source community and has become a central platform for collaboration and code sharing among developers. It provides a user-friendly interface, extensive features, and a vibrant community, making it a valuable tool for software development teams of all sizes.

Source Code Management (SCM)

Source code management (SCM) refers to the practice of tracking and managing changes to software source code throughout its lifecycle. It involves using version control systems (VCS) to keep track of modifications, facilitate collaboration among developers, and maintain a structured history of code changes. The primary goal of source code management is to streamline development processes, improve collaboration, and ensure code quality. Here are some key aspects of source code management:

  1. Version Control System (VCS): A version control system is a software tool that helps manage changes to source code over time. It tracks different versions of files and enables developers to work on code simultaneously while keeping track of changes, including who made the changes and when.

  2. History Tracking: SCM tools maintain a chronological history of all changes made to the codebase. Each change is represented as a commit or changeset, which includes a description of the changes and references to the files affected.

  3. Collaboration: Source code management tools enable multiple developers to work on the same project without interfering with each other's work. Developers can create branches to work on specific features or fixes independently and later merge their changes back into the main codebase.

  4. Branching and Merging: Branching allows developers to create separate lines of development within a project. This is especially useful for isolating new features, bug fixes, or experiments. Merging is the process of integrating changes from one branch into another, allowing for the incorporation of new code while maintaining the project's integrity.

  5. Conflict Resolution: When multiple developers make changes to the same file or lines of code, conflicts can arise during merging. SCM tools provide mechanisms to detect and resolve these conflicts, ensuring that changes are incorporated correctly.

  6. Code Review: Source code management systems often integrate with code review processes. Developers can create pull requests or merge requests to propose changes, and these changes are reviewed by other team members before being merged into the main codebase.

  7. Traceability: SCM tools provide traceability by associating code changes with issues, feature requests, and other work items. This helps developers understand the context of a change and the reasons behind it.

  8. Rollbacks and Revisions: In case of issues or regressions, SCM tools allow you to revert the codebase to a previous state, effectively rolling back to a known working version.

  9. Audit Trails: SCM tools maintain an audit trail of who made changes, what changes were made, and when they were made. This information is valuable for accountability, troubleshooting, and compliance.

  10. Backup and Recovery: SCM tools act as a centralized repository for your code. This offers a form of backup, ensuring that your codebase is protected and can be recovered if necessary.

Popular source code management tools include Git (along with platforms like GitHub, GitLab, and Bitbucket), Mercurial, Subversion, and Perforce. The choice of tool depends on the specific needs and preferences of your development team and organization.

How to setup a Github repository?

To set up a GitHub repository, follow these steps:

  1. Sign Up or Log In: If you don't have a GitHub account, sign up for one at https://github.com/. If you already have an account, log in.

  2. Create a New Repository:

    • Once logged in, click on the "+" sign in the upper-right corner of the GitHub interface.

    • From the dropdown menu, select "New repository."

  3. Configure Repository Settings:

    • Choose a name for your repository. This should be a descriptive name that reflects the project.

    • Optionally, add a description to provide more details about the repository.

    • Choose whether the repository should be public (visible to everyone) or private (visible only to you and collaborators).

    • Select options for initializing the repository with a README, .gitignore (to exclude certain files from version control), and a license.

  4. Create Repository:

    • Click the "Create repository" button.
  5. Clone the Repository:

    • Once the repository is created, you'll be taken to the repository's main page. You can find the repository URL in the "Quick setup" section. It will look like https://github.com/username/repository-name.git.

    • Open your terminal or command prompt.

    • Navigate to the directory where you want to store the local copy of the repository.

    • Clone the repository using the following command, replacing the URL with the actual repository URL:

        git clone https://github.com/username/repository-name.git
      
  6. Add and Commit Code:

    • Navigate into the cloned repository directory using the cd repository-name command.

    • Add your code files to the repository directory.

    • Use the following commands to stage and commit your changes:

        git add .
        git commit -m "Initial commit"
      
  7. Push to GitHub:

    • Now, push your local changes to the GitHub repository using the following command:

        git push origin main
      
    • If you created a branch other than "main," replace "main" with the branch name.

  8. Add Collaborators (Optional):

    • If you're working with others, you can add collaborators to your repository.

    • Go to the repository page on GitHub.

    • Click on "Settings," then navigate to the "Collaborators & teams" section.

    • Enter the usernames of the collaborators you want to add.

  9. Start Developing and Collaborating:

    • With the repository set up, you can now start developing, making changes, and collaborating with others.

    • Create branches for new features or bug fixes, make changes, and create pull requests to propose changes for review.

Git Commands

Here's a list of some commonly used Git commands along with a brief description of each:

  1. git init: Initializes a new Git repository in the current directory.

  2. git clone [repository_url]: Creates a local copy of a remote repository.

  3. git add [file(s)]: Stages changes for commit. You can specify specific files or use . to stage all changes.

  4. git commit -m "[commit_message]": Commits staged changes along with a descriptive message.

  5. git status: Shows the status of the working directory, including staged and unstaged changes.

  6. git diff: Displays the differences between the working directory and the last committed version.

  7. git branch: Lists all branches in the repository. The currently active branch is highlighted.

  8. git checkout [branch_name]: Switches to a different branch.

  9. git checkout -b [new_branch_name]: Creates a new branch and switches to it.

  10. git merge [branch_name]: Merges the specified branch into the current branch.

  11. git pull [remote] [branch]: Fetches changes from a remote repository and merges them into the current branch.

  12. git push [remote] [branch]: Pushes local commits to a remote repository.

  13. git remote add [remote_name] [repository_url]: Adds a remote repository with a given name and URL.

  14. git log: Displays a chronological list of commits, along with their details.

  15. git reset [commit]: Resets the repository to a specific commit, discarding subsequent commits.

  16. git revert [commit]: Creates a new commit that undoes the changes made in the specified commit.

  17. git fetch [remote]: Fetches changes from a remote repository without merging them.

  18. git stash: Temporarily saves changes that are not ready to be committed, allowing you to switch branches.

  19. git tag [tag_name]: Creates a lightweight tag at the current commit.

  20. git show [commit]: Displays the details of a specific commit, including changes and metadata.

  21. git remote -v: Lists all remote repositories along with their URLs.

  22. git config --global user.name "[your_name]": Sets your name for use in commits.

  23. git config --global user.email "[your_email]": Sets your email for use in commits.

Git Branching

Git branches are a fundamental feature of version control systems like Git. They allow developers to work on different aspects of a project simultaneously, isolate changes, and experiment without affecting the main codebase. Here's an overview of Git branches and related commands:

  1. Creating a Branch:

    • git branch [branch_name]: Creates a new branch with the given name.

    • git checkout -b [branch_name]: Creates and immediately switches to the new branch.

  2. Listing Branches:

    • git branch: Lists all branches in the repository. The current branch is highlighted.
  3. Switching Between Branches:

    • git checkout [branch_name]: Switches to the specified branch.
  4. Merging Branches:

    • git merge [branch_name]: Merges the specified branch into the current branch. It's typically used to integrate changes from one branch into another.
  5. Deleting Branches:

    • git branch -d [branch_name]: Deletes the specified branch. Use -d for a safe delete (prevents deletion if changes are not merged). Use -D to force delete.
  6. Renaming a Branch:

    • You can't directly rename a branch using a single Git command. Instead, you create a new branch with the desired name and delete the old one.
  7. Viewing Merged and Unmerged Branches:

    • git branch --merged: Lists branches that are already merged into the current branch.

    • git branch --no-merged: Lists branches that are not yet merged into the current branch.

  8. Viewing a Remote Branch:

    • git branch -r: Lists remote branches.

    • git branch -a: Lists both local and remote branches.

  9. Pushing a Branch to Remote:

    • git push [remote] [branch_name]: Pushes the specified branch to the remote repository.
  10. Tracking a Remote Branch:

  • git checkout --track [remote]/[branch_name]: Creates a new local branch that tracks a remote branch.
  1. Creating a Branch from a Specific Commit:
  • git checkout -b [new_branch_name] [commit_hash]: Creates a new branch starting from a specific commit.

    This is all for this blog about SCM and VCS.

10
Subscribe to my newsletter

Read articles from ADITYA PAWAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ADITYA PAWAR
ADITYA PAWAR

I am a DEVOPS ENGINEER from Pune, Maharashtra. Currently a working professional with 2 years of experience.