Sitemap

Git and Github — fundamentals

5 min readJul 29, 2023

--

Introduction

Git and GitHub have revolutionized the way developers work together, enabling version control and seamless collaboration. In this guide, we will explore the fundamentals of Git and GitHub, their significance in software development, and how they streamline the development process.

Press enter or click to view image in full size
git and github

What are Git and Github?

Git is a distributed version control system that tracks changes in code. It allows multiple users to contribute to a codebase, preserving its history across various versions. With Git, developers can work on the same project concurrently, enabling efficient collaboration.

GitHub is a web-based platform that provides hosting for Git repositories. It facilitates collaborative software development, version control, and seamless code sharing among developers worldwide.

Types of Version Control Systems

There are three types of version control systems:

  • Local VCS: In a local VCS, changes are tracked on a single local machine, making it suitable for individual projects.
  • Centralized VCS: A centralized VCS stores the code on a central server, allowing multiple developers to collaborate. However, a single point of failure can disrupt the entire workflow.
  • Distributed VCS: Git is a prime example of a distributed VCS, wherein each user has a complete repository with full history. This decentralization ensures robustness and redundancy.

Where is the GitHub Code Stored?

GitHub’s Arctic Code Vault is an extraordinary initiative to preserve open-source code for future generations. It houses a snapshot of every active public repository on GitHub, storing them in the Arctic World Archive, an ultra-long-term storage facility in Svalbard, Norway.

Stages of Files in Git

Understanding the stages of files in Git is vital:

  • Untracked: Files not yet added to staging or committed to version control.
  • Staged: Files ready for committing, placed in the staging area.
  • Unmodified: Files that have been committed and remain unchanged.
  • Modified: Files altered after the previous commit.
Press enter or click to view image in full size

Basic Git Commands

Let’s explore some essential Git commands for beginners:

we are required to initialize the local git repository in order to make further changes.

git init

Set user email and user globally.

git config — global user.email <youremail@.com>
git config — global user.name <github_id_here>

Set GitHub password globally.

git config — global user.password <github_password_here>

List the global configurations.

git config — list

Set the remote origin URL.

git remote set-url origin <url here>

Add a new remote origin URL.

git remote add origin <url here>

View all remote URLs.

git remote -v

Check the status of files in the repository.

git status

Add specific files to the staging area.

git add <file name>

Add all changes to the staging area.

git add .

Stage all changes, including deletions and new files.

git add -A

Stage all changes, including deletions and new files.

git add -u

Unstage all files from the staging area.

git reset

Unstage a particular file from the staging area.

git restore — staged file.html

Commit staged changes with a descriptive message.

git commit -m “commit message”

Retrieve the last commit for a specific file.

git checkout <filename.extention>

Retrieve the last commit for the entire folder.

git checkout -f

View the commit history.

git log

View the last 5 commits with changes.

git log -p -5

Commit changes directly without staging.

git commit -a -m “message here”

Branches in Git — A Powerful Version Control Feature

A branch in Git is a version of the repository that diverges from the main working project. It is a vital feature available in most modern version control systems, allowing developers to work on separate features or changes without affecting the main codebase.

Get Ashutosh Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Create a New Branch

git branch <branchname>

Explanation: Create a new branch with the specified name.

Switch to a New Branch

git checkout <branchname>

Explanation: Switch to the specified branch, making it the active branch for further changes.

Create a New Branch and Switch to It

git checkout -b <branchname>

Explanation: Create a new branch with the specified name and switch to it in a single step.

Merge Two Branches

git merge <branchname>

Explanation: Merge the specified branch into the current branch, integrating changes from both branches.

See Merged Branches

git branch --merged

Explanation: View a list of branches that have been successfully merged into the current branch.

See Unmerged Branch

git branch --no-merged

Explanation: View a list of branches that have changes not yet merged into the current branch.

Delete a Merged Branch

git branch -d <branch name>

Explanation: Delete the specified branch that has been successfully merged into the current branch.

Delete an Unmerged Branch

git branch -D <branch name>

Pushing

Pushing the local repo to remote

git push <repo name> <branch>

Cloning a Repository

Clone a remote repository to your local machine.

git clone <remote repository url>

Merge Conflict: Resolving Code Conflicts in Git

Merge conflict is a common challenge in collaborative software development, particularly when using version control systems like Git. It occurs when two or more developers make conflicting changes to the same file or code block, and Git is unable to automatically determine which version should be kept in the final merge.

Press enter or click to view image in full size
Example of Merge conflict

Resolving merge conflicts in Git involves identifying conflicting changes, manually editing the affected files to decide which version to keep, and then staging and committing the resolved changes to complete the merge process. Effective communication and task isolation can help prevent conflicts.

Branching Workflow: A Brief Overview

Branching in version control systems like Git is a powerful technique that allows developers to work on separate features or changes without affecting the main codebase. Two common types of branches used in branching workflows are Long-Running Branches and Topic Branches.

1. Long-Running Branches

  • Master (Main Branch): The primary branch that represents the stable and production-ready version of the codebase.
  • Develop (Development Branch): Used for ongoing development work. Developers merge their feature branches into this branch for integration testing.
  • PU (Proposed Update Branch): This branch is for testing new ideas and features in the development phase before merging them into the main codebase.

2. Topic Branches

Topic branches are created to isolate specific tasks, features, or bug fixes from the main development branch. Developers can experiment and test their changes independently without disrupting other ongoing work. Once the topic branch is complete and tested, it can be merged back into the main development branch.

Conclusion

Git and GitHub have become indispensable tools for modern software development. Understanding the basics of Git and GitHub empowers developers to work collaboratively, track changes efficiently, and build robust, high-quality codebases.

Note: The above content serves as a comprehensive guide to Git and GitHub’s basics. It covers essential concepts, commands, and the significance of version control systems in collaborative development.

--

--

Ashutosh Sharma
Ashutosh Sharma

Written by Ashutosh Sharma

An Explorer in the Field of Data Science and AI. Follow me to stay updated to my posts. LinkedIn https://www.linkedin.com/in/ashutoshsharma-xi/