/
Version Control with GitHub: Command Guide

Version Control with GitHub: Command Guide

GITHUB

GitHub is a cloud-based platform for version control and collaboration that allows developers to work on projects together. It uses Git, a distributed version control system, to track changes in code and manage software development/testing efficiently.

 

Key Features of GitHub:

  • Version Control: Keep track of changes in your codebase, enabling easy rollback to previous versions.

  • Collaboration: Work on code with teams using branches, pull requests, and code reviews.

  • Remote Repositories: Store code in the cloud, making it accessible to team members anywhere.

  • Issue Tracking: Log and track bugs, enhancements, or tasks for a project.

  • Integration Support: Integrates with tools like CI/CD pipelines (e.g., Jenkins, GitHub Actions) to automate testing and deployment.

  • Open Source Hosting: Share projects with the community and contribute to others' projects.

 

Common Git Workflow

This detailed guide will walk you through the typical steps of using Git in a project, covering configuration, branch management, collaboration, and pushing/pulling code.


1. Initial Setup

Before starting, configure your Git environment:

Commands:

git config --global user.name "Your Name"     # Sets your Git username

git config --global user.email "you@example.com" # Sets your Git email

Purpose:
These commands set up your Git user identity globally on your machine. The name and email you configure will appear in your commits. Use the --global flag to make these settings apply to all repositories on your system. If you omit --global, the settings will apply only to the current repository.


2. Start a New Project

You can either create a new Git repository or clone an existing one.

Option 1: Create a new repository

Command:

git init

  • Initializes a Git repository in the current directory.

  • A .git folder will be created to track changes.

Option 2: Clone an existing repository

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

  • Creates a local copy of the repository and pulls all existing commits, branches, and files.


3. Work on Code

Make changes to your project files and track them with Git.

Check Repository Status:

git status

  • Displays the state of your working directory (e.g., modified, staged, or untracked files).

Stage Files for Commit:

git add filename       # Stage a specific file

git add .              # Stage all changes in the directory

  • Moves changes into the staging area, preparing them for the next commit.

Commit Your Changes:

git commit -m "Add a meaningful commit message"

  • Saves the staged changes as a snapshot in the Git history.


4. Create and Work on a Branch

Branches allow you to work on features or fixes in isolation.

Create a New Branch:

git branch feature-branch

  • Creates a branch named feature-branch.

Switch to the New Branch:

git switch feature-branch   # Recommended

OR

git checkout feature-branch # Alternative method

  • Switches to the feature-branch, enabling you to work there.


5. Collaborate with Team Members

Share your work with teammates and incorporate their changes.

Push Changes to Remote Repository:

git push origin feature-branch

  • Send your commits in feature-branch to the remote repository.

Pull Changes from Remote Repository:

git pull origin main

  • Updates your local branch with the latest changes from the main branch in the remote repository.

Merge Branches:

git switch main

git merge feature-branch

  • Combines the changes from feature-branch into main.

Delete a Branch:

git branch -d feature-branch

  • Removes the branch after merging. Use -D to force delete unmerged branches.


6. Final Workflow Summary

Here’s a step-by-step example of a typical Git workflow:

  1. Initialize or Clone a Repository:

    git init             # For a new project

git clone <repo-url> # For an existing project

  1. Check the Current Branch:
    git branch

  2. Create and Switch to a New Branch:

    git branch feature-branch

git switch feature-branch

  1. Work on the Code:

Modify files as needed.

  1. Stage and Commit Changes:
    git add file-name(s)

git commit -m "Implemented a new feature"

  1.  Pull Changes from the Main Branch (to stay updated):
    git pull origin main

  2. Push Changes to the Remote Repository:

git push origin feature-branch

  1. Merge Changes Back to Main:
    git switch main

git merge feature-branch

git push origin main