Introducing Git In A New Light

Introducing Git In A New Light

posted 4 min read

Version control, also known as source control, is the practice of recording and managing changes to software code. It’s a crucial aspect of software development that allows teams to collaborate, track revisions, and revert to previous versions when needed. While there are many version control systems available—like Apache Subversion, Concurrent Versions System (CVS), and BitKeeper—Git has become the preferred choice for most developers. As a free and open-source distributed version control system, Git is designed for speed, efficiency, and handling projects of all sizes.

This cheat sheet covers the most commonly used Git commands to help you get started and master version control in your development workflow.


Installation

To install Git, use the command for your specific operating system:

  • On Fedora/RedHat systems:
    dnf install git
    
  • For other operating systems, you can download Git from git-scm.com.

Setup

Before using Git, configure your user information, which will be used across all local repositories.

Command Description
git config --global user.name "[firstname lastname]" Set your name to be used in the version history
git config --global user.email "[valid-email]" Set your email for your commits
git config --global color.ui auto Enable colorized output for easier command line review

Initialization

Set up and initialize new repositories or clone existing ones.

Command Description
git init Create a new Git repository in an existing directory
git clone [url] Clone an existing repository from a remote URL

Stage & Snapshot

Use snapshots and the staging area to manage changes.

Command Description
git status Show the status of changes in your working directory
git add [file] Add the specified file to the staging area (stage)
git reset [file] Unstage a file, keeping the changes in your working directory
git diff Show changes in the working directory that haven't been staged
git diff --staged Show changes between the staging area and the last commit
git commit -m "[message]" Commit the staged changes with a descriptive message

Branch & Merge

Manage branches and integrate changes effectively.

Command Description
git branch List all branches in the repository; current branch is marked with *
git branch [branch-name] Create a new branch
git checkout [branch-name] Switch to the specified branch
git merge [branch-name] Merge the specified branch into the current branch
git log View the commit history for the current branch

Inspect & Compare

Analyze logs, diffs, and object data to better understand your code changes.

Command Description
git log Show the commit history for the current branch
git log branchY...branchX List commits that are in branchX but not in branchY
git log --follow [file] Show changes to a file across different commits
git diff branchY...branchX Show the difference between two branches
git show [SHA] Display the content of a specific commit in a readable format

Share & Update

Keep your local repository in sync with a remote repository.

Command Description
git remote add [alias] [url] Add a remote repository under a specified alias
git fetch [alias] Download all branches from the remote repository
git merge [alias]/[branch] Merge a remote branch into your current branch
git push [alias] [branch] Push commits from the local branch to the remote branch
git pull Fetch and merge changes from the remote repository into the current branch

Tracking Path Changes

Manage file removals and relocations.

Command Description
git rm [file] Remove a file from the working directory and stage the removal
git mv [existing-path] [new-path] Rename or move a file
git log --stat -M Show commit logs with details on files that have been moved

Ignoring Patterns

Avoid staging or committing files unintentionally.

Pattern Examples Description
logs/, *.notes, pattern*/ Patterns to add to .gitignore to avoid tracking files
git config --global core.excludesfile [file] Set a global ignore file for patterns across all repositories on your system

Rewrite History

Modify commits and clean up your repository's history.

Command Description
git rebase [branch] Reapply commits on top of another base commit
git reset --hard [commit] Reset the current branch to the specified commit, discarding changes

Temporary Commits

Store your changes temporarily to switch branches without committing.

Command Description
git stash Save your changes and clean the working directory
git stash pop Reapply the most recently stashed changes
git stash list List all stashed changes

Mastering these Git commands can help you effectively manage your codebase, collaborate with team members, and keep your projects organized. Whether you’re working on a solo project or a massive open-source initiative, understanding Git’s capabilities will empower you to develop software more efficiently. Happy coding!

Git Workflow Infographic

REFERENCES

  1. https://git-scm.com
  2. https://git.kernel.org/pub/scm/git/git.git
  3. https://about.gitlab.com/images/press/git-cheat-sheet.pdf (Motivation to Write this Article)
  4. https://gitlab.com/
  5. https://github.com/
If you read this far, tweet to the author to show them you care. Tweet a Thanks
This is truly a beginner friendly way to learn git

More Posts

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7, 2024

Introducing GitCorteX – The AI-Powered GitHub Automation Tool!

agm024 - Feb 19

The Ultimate Guide to Mastering Git Commands: Everything You Need to Know

Hanzla Baig Dev - Feb 21

Automating MySQL Backups on a VPS with Telegram Alerts Part 2

Gift Balogun - Mar 22

A Web App to Showcase all your GitHub Projects

2KAbhishek - May 6, 2024
chevron_left