Day 5 | Git & GitHub Fundamentals

Dhruv RajvanshiDhruv Rajvanshi
4 min read

In the world of software development, version control is crucial for tracking changes, collaborating with others, and maintaining the integrity of codebases. Git, a distributed version control system, and GitHub, a platform for hosting Git repositories, are among the most popular tools for these tasks. Today, we'll delve into the fundamentals of Git and GitHub, exploring essential commands and concepts.

What is Git?

Git is a distributed version control system that allows multiple developers to work on a project simultaneously without overwriting each other's changes. It keeps track of changes to files and directories, enabling you to revert to previous versions and collaborate efficiently.

Git Bash

Git Bash is a command-line interface that provides an environment similar to the Unix shell. It's commonly used on Windows to interact with Git repositories, offering a familiar set of command-line tools.

Key Concepts in Git

1. Branch

A branch in Git is a separate line of development. By default, the main branch is called "master" or "main." You can create new branches to develop features or fix bugs without affecting the main codebase.

2. Hash

A hash is a unique identifier generated by Git for each commit. It ensures the integrity of the commit history by uniquely identifying each change.

File System vs. Git

FeatureFile SystemGit
VersioningChanges overwrite previous versions.Tracks changes with full history.
BackupManual backups needed.Automatic tracking of all changes.
CollaborationNot supported.Supports multiple collaborators with merge capabilities.
HistoryLimited or none.Complete history of all changes.

Essential Git Commands

  • git init: Initializes a new Git repository.

  • ls -a: Lists all files, including hidden ones like .git.

  • git status: Shows the status of the working directory and staging area.

  • git add: Adds changes to the staging area.

    • Example: git add . adds all changes.
  • git rm --cached <file>: Removes a file from the staging area without deleting it.

  • git commit: Commits staged changes.

    • Example: git commit -m "Initial commit".
  • git log: Displays the commit history.

    • Example: git log --oneline shows a concise view.
  • git restore: Restores working tree files.

  • git config: Sets configuration options, like username and email.

    • Example: git config --global user.name "Your Name".
  • git checkout -b <branch-name>: Creates and switches to a new branch.

  • git switch <branch-name>: Switches branches.

  • git branch -d <branch-name>: Deletes a branch.

  • Removing a Committed File:

    • Use git rm <file> followed by git commit.
  • git clone: Clones a repository to your local machine.

  • git remote -v: Displays the remote repositories.

  • git push origin <branch-name>: Pushes changes to the remote repository.

What is GitHub

GitHub is a platform for hosting Git repositories. It provides a web-based interface for managing repositories, collaborating with others, and using Git's version control features. GitHub also supports additional features like pull requests, issues, and project boards.

Git vs. GitHub

AspectGitGitHub
DefinitionA version control system for tracking code changes.A platform for hosting Git repositories online.
UsageUsed locally on your computer for version control.Provides cloud-based hosting and collaboration features.
FunctionalityTracks changes and manages versions.Offers additional tools like pull requests, issues, and project management.
AccessCan be used offline.Requires an internet connection for access and collaboration.

Creating a Personal Access Token (PAT)

  1. Sign in to GitHub.

  2. Go to Settings > Developer settings > Personal access tokens.

  3. Click "Generate new token".

  4. Select the scopes or permissions, then click "Generate token".

  5. Copy the token and save it securely.

Pushing from Git Using PAT and SSH

Using PAT:

  1. Clone the repository:

     git clone https://github.com/username/repository.git
    
  2. Navigate to the repository:

     cd repository
    
  3. Make changes and commit them:

     git add .
     git commit -m "Your commit message"
    
  4. Push changes:

     git push origin main
    

    When prompted, use your GitHub username and PAT.

Using SSH:

  1. Set up SSH keys:

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  2. Add the SSH key to your GitHub account.

  3. Clone the repository:

     git clone git@github.com:username/repository.git
    
  4. Make changes and commit them:

     git add .
     git commit -m "Your commit message"
    
  5. Push changes:

     git push origin main
    

Cloning a Private Repository Using PAT and SSH

Using PAT:

  1. Clone the repository:

     git clone https://github.com/username/private-repository.git
    
  2. When prompted, enter your GitHub username and PAT.

Using SSH:

  1. Ensure your SSH key is added to your GitHub account.

  2. Clone the repository:

     git clone git@github.com:username/private-repository.git
    

Conclusion

Mastering Git and GitHub fundamentals is essential for any developer. They provide powerful tools for version control, collaboration, and efficient code management. By understanding these tools, you can effectively manage your codebase, collaborate with others, and streamline your development workflow. Keep exploring and expanding your knowledge to become proficient in using Git and GitHub. Happy coding!

1
Subscribe to my newsletter

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

Written by

Dhruv Rajvanshi
Dhruv Rajvanshi