Git and GitHub — EVERY CONCEPT

Bhavyansh @ DiversePixel
5 min readJun 2, 2024

--

Git and GitHub have become indispensable tools in modern software development. Let’s explore everything you need to know to get started and become proficient.

Photo by Roman Synkevych on Unsplash

01 What is Git?

Git is a version control system that allows you to track changes to your code over time. This means you can:

  • Revert to previous versions if you introduce bugs.
  • Collaborate effectively with others by merging their changes.
  • Understand the evolution of your codebase.

Why is Git Essential for Software Engineers?

  • Reliability: Git ensures a secure backup of your code’s history.
  • Collaboration: Makes teamwork seamless, allowing for easy merging and conflict resolution.
  • Experimentation: Git enables you to create branches for trying out new ideas without affecting the main codebase.

02 Install Git

  • Windows: Download and install Git from the official website: https://git-scm.com/
  • Mac: Git may already be pre-installed. You can check by opening the terminal and typing git --version. If not, use a package manager like Homebrew: brew install git
  • Linux: Use your distribution’s package manager (e.g., apt-get install git on Debian/Ubuntu).

03 git init

To create a Git repository in your project folder, open a terminal and run:

git init

This creates a hidden .git directory to store version history.

04 git status

Check what files have been changed:

git status

05 git add

To track changes to specific files or all files:

git add <filename> 
git add . # Adds all changed files

Add specific files to staging:

git add -i

06 git commit

Commit your staged changes with a descriptive message:

git commit -m "Your commit message here"

07 VS Code Tips

  • GitLens: This extension provides excellent visualization and navigation of your Git history.
  • Built-in Terminal: Use VS Code’s terminal for Git commands without leaving the editor.
  • Source Control Tab: Easily stage, commit, and manage your Git workflow.

08 git remote

Connect your local repository to a remote one (e.g., on GitHub):

git remote add origin <repository_url>

09 git push

Upload your committed changes to the remote repository:

git push origin <branch_name>

10 git merge & 11 git pull

  • git merge: Combine changes from different branches.
  • git pull: Fetch the latest changes from a remote repository and merge them into your local branch.

12 git clone

Download a remote repository to your local machine:

git clone <repository_url>

13 GitHub Codespaces

GitHub Codespaces allows you to work on your code from any device with a browser. It spins up a cloud-powered VS Code instance with your entire project environment.

14 git branch

Create a new branch to work on a specific feature or bug fix:

git branch <branch_name>

15 git checkout

Switch between branches:

git checkout <branch_name>

16 Merge Conflicts

Merge conflicts occur when Git cannot automatically combine changes from different branches. You’ll need to resolve these conflicts manually by editing the files.

17 Fork

A fork is your copy of a repository. It allows you to experiment freely without affecting the original project. You can then make changes and submit them back to the original repository as a pull request.

18 Pull Request (PR)

A PR is a way to propose changes to a repository. It initiates a discussion and review process before your changes are merged.

19 git reset

Git configurations to add to .gitconfig:

[alias]
undo = git reset --soft HEAD^

This alias creates a shorthand command undo that can be used in place of the longer git reset --soft HEAD^ command. The git reset --soft HEAD^ command undoes the last commit without losing any changes.

You can use more carets (e.g., HEAD^^) to go back further (two commits in this case).

Undo changes to files or your commit history:

git reset <commit_hash>   # Resets to a specific commit
git reset --hard HEAD~1 # Discards the last commit

20 git revert

Undo a commit by creating a new commit that reverses its changes. This is a safer way to undo commits, especially in collaborative environments.

git revert <commit_hash>

21 git commit — amend

Modify the last commit message or include additional changes:

git commit --amend -m "Updated commit message"

22 git stash

Temporarily save changes without committing them:

git stash
git stash pop # Restore stashed changes

23 git rebase & 24 Squash

  • git rebase: Re-apply commits onto a different base branch.
  • Squash: Combine multiple commits into a single, more meaningful commit.

25 GitHub Actions

Automate your workflows with GitHub Actions. This includes continuous integration (CI) and continuous deployment (CD) pipelines for testing, building, and deploying your code.

26 Advanced Git Tips

  • git bisect: Find the commit that introduced a bug.
  • git cherry-pick: Apply a specific commit from one branch to another.
  • git reflog: View a log of all references that have been changed in your repository.
  • Interactive Rebase: Reorder, edit, or squash commits during a rebase.

Roll Back Changes

Git provides several ways to undo changes, depending on their stage in your workflow:

If changes are not staged:

  • Check changes:
git checkout <filename>
git diff # Shows the differences between the current file and the last committed version
  • Discard changes:
git restore <filename>

If changes are staged (but not committed):

  • Check staged changes:
git diff --cached
  • Unstage changes:
git restore --staged <filename>

If changes are committed:

  • View commit history:
git log --oneline
  • See changes in a specific commit:
git diff <second_last_commit_id>..<last_commit_id>
  • Two ways to roll back:
  • Revert (safe): Creates a new commit that undoes the changes. Preserves history.
git revert HEAD  # Undo the last commit
git revert <commit_id> # Undo a specific commit
  • Reset (potentially destructive): Rewrites history, as if the commit never happened. Use with caution!
git reset --hard HEAD  # Undo the last commit and discard changes
git reset --hard <commit_id> # Reset to a specific commit

Important Note: Resetting a commit can be dangerous, especially if the commit has been pushed to a remote repository. It can cause issues for collaborators who have already pulled those changes. Always proceed with caution and communicate with your team when using git reset.

Naming Branches

  1. Feature Branches: Use the feature/ prefix.
  • Example: feature/user-authentication, feature/menu-management

2. Bug Fix Branches: Use the bugfix/ prefix.

  • Example: bugfix/fix-login-issue, bugfix/correct-menu-display

3. Improvement Branches: Use the improvement/ prefix.

  • Example: improvement/refactor-order-model, improvement/update-styles

4. Hotfix Branches: Use the hotfix/ prefix for urgent fixes.

  • Example: hotfix/security-patch, hotfix/payment-gateway-fix

5. Chore Branches: Use the chore/ prefix for maintenance tasks.

  • Example: chore/update-dependencies, chore/add-docs

Naming Commits

  1. Use Imperative Mood: Write commit messages in the imperative mood.
  • Example: Add user authentication, Fix menu display issue

2. Be Concise and Descriptive: Summarize the changes clearly.

  • Example: Implement Stripe payment integration, Refactor order processing logic

3. Include References if Necessary: Include issue or ticket numbers.

  • Example: Add user authentication (#45), Fix menu display issue (#78)

4. Separate Subject and Body: If the commit message is longer, separate the subject and the body with a blank line.

Implement Stripe payment integration


This commit adds the necessary configurations and code to integrate Stripe
for payment processing. It includes handling payment intents and webhook
events.

5. Keep Subject Line Under 50 Characters: If possible, keep the subject line concise and to the point.

Example: Update user model, Refactor billing system

--

--

Bhavyansh @ DiversePixel
Bhavyansh @ DiversePixel

Written by Bhavyansh @ DiversePixel

Hey I write about Tech. Join me as I share my tech learnings and insights. 🚀

No responses yet