Understanding Git: A Comprehensive Guide from Beginner to Advanced

Git has revolutionized the way developers collaborate on code. Created by Linus Torvalds, it’s a version control system that allows multiple people to work on different parts of a project simultaneously. With its many features and commands, Git can be overwhelming for newcomers, but it’s an essential tool for modern software development. This article will take you through a journey from beginner to advanced level concepts, focusing mainly on the commands you need to know at each stage.

Let’s start with commands.


🛠️ Setup and Configuration


1️⃣ git – The Main Command

  • Example: To start a new Git project.
  git init

Sample Output:

  Initialized empty Git repository in /path/to/directory/.git/Code language: PHP (php)

2️⃣ git config – Setting Up Your Info

  • Global Info: Set up your name and email for all projects.
  git config --global user.name "Your Name"
  git config --global user.email "youremail@example.com"Code language: PHP (php)

Sample Output: No output means the command was successful.

  • Just This Project: Set up your name and email for one project.
  git config user.name "Your Name"
  git config user.email "youremail@example.com"Code language: CSS (css)

Sample Output: No output means it worked.

  • Check Your Setup:
  git config --listCode language: PHP (php)

Sample Output:

  user.name=Your Name
  user.email=youremail@example.com

3️⃣ git help – Get Help

  • Example: To get help on setting up your info (git config).
  git help config

Sample Output: Opens the manual page for git config in your default text viewer.


4️⃣ git bugreport – Report Bugs

  • Example:
  git bugreport --output-directory ~/path/to/save/Code language: JavaScript (javascript)

Sample Output:

  Writing report to /path/to/save/git-bugreport-2023-09-12-xx-xx-xx.txt

5️⃣ Credential Helpers – Remember Login Info

  • Remember Temporarily:
  git config --global credential.helper cacheCode language: CSS (css)

Sample Output: No output means it worked.

  • Set Time Limit:
  git config --global credential.helper 'cache --timeout=3600'Code language: PHP (php)

Sample Output: No output means it worked.

  • Remember Forever:
  git config --global credential.helper storeCode language: CSS (css)

Sample Output: No output means it worked.

  • Mac and Windows:
  • For Mac: git config --global credential.helper osxkeychain Sample Output: No output means it worked.
  • For Windows:
    bash git config --global credential.helper wincred
    Sample Output: No output means it worked.

Git Cheat Sheet for Getting and Creating Projects


📚 Getting and Creating Projects


1️⃣ git init – Start a New Project

  • What it does: This command initializes a new Git repository. It creates a .git folder that stores all the configuration files and directories.
  • Example: To create a new Git repository in the current folder, you type:
  git init
  • Sample Output:
  Initialized empty Git repository in /path/to/directory/.git/Code language: PHP (php)

This tells you that a new repository has been initialized in the .git directory.


2️⃣ git clone – Copy an Existing Project

  • What it does: This command copies an existing Git repository. You usually get the URL of the repository you want to clone from a website like GitHub, GitLab, or Bitbucket.
  • Example: To clone a repository from a URL, you type:
  git clone https://github.com/example/repo.gitCode language: PHP (php)
  • Sample Output:
  Cloning into 'repo'...
  remote: Enumerating objects: 10, done.
  remote: Counting objects: 100% (10/10), done.
  remote: Compressing objects: 100% (8/8), done.
  remote: Total 10 (delta 1), reused 10 (delta 1), pack-reused 0
  Unpacking objects: 100% (10/10), done.Code language: JavaScript (javascript)

This output tells you that the repository is being cloned and provides a status of the operation.



Git Cheat Sheet for Basic Snapshotting 📸


1️⃣ git add – Stage Changes

  • What it does: Adds changes in specific files to the staging area, preparing them for a commit.
  • Example: To add a file named example.txt to the staging area, you type:
  git add example.txtCode language: CSS (css)
  • Sample Output: No output means the command was successful.

2️⃣ git status – Check Status

  • What it does: Shows the status of changes in the repository.
  • Example: To check the status, you type:
  git status
  • Sample Output:
  On branch main
  Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
      new file:   example.txtCode language: JavaScript (javascript)

3️⃣ git diff – Show Changes

  • What it does: Shows the changes between your working directory and the staging area.
  • Example: To see what changes have been made but haven’t been staged yet, you type:
  git diff
  • Sample Output:
  diff --git a/example.txt b/example.txt
  index 1234567..89abcde 100644
  --- a/example.txt
  +++ b/example.txt
  @@ -1 +1 @@
  -Hello, World!
  +Hello, Git!

4️⃣ git commit – Save Changes

  • What it does: Saves the changes you’ve staged to the repository history.
  • Example: To commit staged changes with a message, you type:
  git commit -m "Your commit message"Code language: JavaScript (javascript)
  • Sample Output:
  [main 9d8a7b6] Your commit message
   1 file changed, 1 insertion(+)Code language: CSS (css)

5️⃣ git notes – Add Notes

  • What it does: Adds notes to a specific commit.
  • Example: To add a note to the latest commit, you type:
  git notes add -m "Your note"Code language: JavaScript (javascript)
  • Sample Output: No output means the command was successful.

6️⃣ git restore – Unstage or Revert

  • What it does: Restores files to a previous state or even removes staged changes.
  • Example: To unstage changes to a file called example.txt, you type:
  git restore --staged example.txtCode language: CSS (css)
  • Sample Output: No output means the command was successful.

7️⃣ git reset – Reset Commits

  • What it does: Resets your current HEAD to a specific state.
  • Example: To uncommit the latest commit and keep changes in your working directory, you type:
  git reset HEAD~1
  • Sample Output: No output means the command was successful.

8️⃣ git rm – Remove Files

  • What it does: Removes files from the staging area and your working directory.
  • Example: To remove a file named example.txt, you type:
  git rm example.txtCode language: CSS (css)
  • Sample Output:
  rm 'example.txt'Code language: JavaScript (javascript)

9️⃣ git mv – Move or Rename Files

  • What it does: Moves or renames a file both in the working directory and the staging area.
  • Example: To rename a file from oldname.txt to newname.txt, you type:
  git mv oldname.txt newname.txtCode language: CSS (css)
  • Sample Output: No output means the command was successful.


Git Cheat Sheet for Branching and Merging 🌿


1️⃣ git branch – List or Create Branches

  • What it does: Lists all the local branches in the repository. Can also be used to create a new branch.
  • Example: To list all branches:
  git branch
  • Sample Output:
  * main
    feature-branch
    another-branch

To create a new branch:

  git branch new-branchCode language: JavaScript (javascript)

Sample Output: No output means the command was successful.


2️⃣ git checkout – Switch Branches or Restore Files

  • What it does: Checks out a branch or paths to the working tree.
  • Example: To switch to a branch named feature-branch:
  git checkout feature-branch
  • Sample Output:
  Switched to branch 'feature-branch'Code language: JavaScript (javascript)

3️⃣ git switch – Switch Branches

  • What it does: More modern and intuitive way to switch between branches.
  • Example: To switch to a branch named feature-branch:
  git switch feature-branchCode language: JavaScript (javascript)
  • Sample Output:
  Switched to branch 'feature-branch'Code language: JavaScript (javascript)

4️⃣ git merge – Merge Branches

  • What it does: Merges another branch into your active branch.
  • Example: To merge feature-branch into main:
  git merge feature-branch
  • Sample Output:
  Updating ae24b4a..31c6a2d
  Fast-forwardCode language: CSS (css)

5️⃣ git mergetool – Conflict Resolution Tool

  • What it does: Opens a GUI that helps resolve merge conflicts.
  • Example: To open the merge tool:
  git mergetool
  • Sample Output: GUI opens, no terminal output.

6️⃣ git log – Show Commit Logs

  • What it does: Shows a listing of commits.
  • Example: To show commit logs:
  git log
  • Sample Output:
  commit ae24b4a8d3e85...
  Author: Your Name <email@example.com>
  Date:   Mon Sep 11 13:55:51 2023 +0000

      Your commit messageCode language: CSS (css)

7️⃣ git stash – Stash Changes

  • What it does: Stashes the changes in a dirty working directory away.
  • Example: To stash your changes:
  git stash
  • Sample Output:
  Saved working directory and index state WIP on main: ae24b4a Your last commit message

8️⃣ git tag – Add Reference Tags

  • What it does: Marks specific points in history as important, often used for releases.
  • Example: To add a new tag:
  git tag v1.0.0Code language: CSS (css)
  • Sample Output: No output means the command was successful.

9️⃣ git worktree – Manage Multiple Working Trees

  • What it does: Allows you to have multiple branches checked out in different directories simultaneously.
  • Example: To create a new worktree:
  git worktree add ../new-worktree-dir feature-branchCode language: JavaScript (javascript)
  • Sample Output:
  Preparing worktree (checking out 'feature-branch')Code language: JavaScript (javascript)


Git Cheat Sheet for Sharing and Updating Projects 🔄


1️⃣ git fetch – Download Objects and Refs

  • What it does: Downloads objects and references from another repository but doesn’t merge changes.
  • Example: To fetch changes from the remote repository:
  git fetch origin
  • Sample Output:
  remote: Enumerating objects: 5, done.
  remote: Counting objects: 100% (5/5), done.
  remote: Compressing objects: 100% (3/3), done.

2️⃣ git pull – Fetch and Merge

  • What it does: Downloads changes and merges them into your current local repository.
  • Example: To pull changes from the remote repository:
  git pull origin main
  • Sample Output:
  Updating 34e91da..b0d13d2
  Fast-forward
   README.md | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)

3️⃣ git push – Update Remote Repository

  • What it does: Uploads all local branch commits to the corresponding remote repository.
  • Example: To push your changes to the remote repository:
  git push origin main
  • Sample Output:
  Counting objects: 5, done.
  Writing objects: 100% (3/3), 247 bytes | 247.00 KiB/s, done.
  Total 3 (delta 0), reused 0 (delta 0)
  To https://github.com/yourusername/your-repo.git
     34e91da..b0d13d2  main -> mainCode language: JavaScript (javascript)

4️⃣ git remote – Manage Remote Repositories

  • What it does: Allows you to set, view, or manage remote repositories.
  • Example: To view remote repositories:
  git remote -v
  • Sample Output:
  origin  https://github.com/yourusername/your-repo.git (fetch)
  origin  https://github.com/yourusername/your-repo.git (push)Code language: JavaScript (javascript)

5️⃣ git submodule – Initialize, Update or Inspect Submodules

  • What it does: Manages subprojects within a Git repository.
  • Example: To add a new submodule:
  git submodule add https://github.com/username/repo.git path/to/submoduleCode language: JavaScript (javascript)
  • Sample Output:
  Cloning into '/path/to/submodule'...
  remote: Counting objects: 1305, done.
  remote: Compressing objects: 100% (546/546), done.Code language: JavaScript (javascript)


Git Cheat Sheet for Inspection and Comparison 🔍


1️⃣ git show – Show Various Types of Objects

  • What it does: Shows information about a Git object, usually a commit.
  • Example: To show the latest commit:
  git show HEAD
  • Sample Output:
  commit 39aef3d8b2e7
  Author: Your Name <you@example.com>
  Date: Mon Sep 12 12:34:56 2023 -0400

      Your commit messageCode language: CSS (css)

2️⃣ git log – Show Commit Logs

  • What it does: Shows a listing of commits.
  • Example: To show commit logs:
  git log
  • Sample Output:
  commit 39aef3d8b2e7
  Author: Your Name <you@example.com>
  Date: Mon Sep 12 12:34:56 2023 -0400

      Your commit messageCode language: CSS (css)

3️⃣ git diff – Show Changes

  • What it does: Shows changes between commits, the staging area, and working directory, etc.
  • Example: To see the changes not yet staged:
  git diff
  • Sample Output:
  diff --git a/file.txt b/file.txt
  index 1234567..89abcde 100644
  --- a/file.txt
  +++ b/file.txt
  @@ -1,2 +1,2 @@
  -Hello, World!
  +Hello, Git!

4️⃣ git difftool – External Diff Viewer

  • What it does: Serves the same purpose as git diff but allows you to view the differences via an external tool.
  • Example:
  git difftool
  • Sample Output: GUI opens, no terminal output.

5️⃣ git range-diff – Compare Ranges

  • What it does: Compares two sequences of commits, such as two branches.
  • Example:
  git range-diff master topic
  • Sample Output:
  1:  abcd123 ! 2:  efgh456 Another change

6️⃣ git shortlog – Summarize Logs

  • What it does: Summarizes git log output in a format suitable for release notes.
  • Example:
  git shortlog
  • Sample Output:
  Your Name (3):
      Fixed bug #132
      Added feature #21
      Updated documentationCode language: CSS (css)

7️⃣ git describe – Describe Tags

  • What it does: Produces a human-readable name based on the most recent tag.
  • Example:
  git describe
  • Sample Output:
  v1.0-2-g1ab3189Code language: CSS (css)


Git Cheat Sheet for Patching 🛠️


1️⃣ git apply – Apply a Patch

  • What it does: Applies a patch that was created with git diff.
  • Example: To apply a patch from a file called mypatch.patch:
  git apply mypatch.patchCode language: CSS (css)
  • Sample Output:
  // No output means the command was successful.Code language: JSON / JSON with Comments (json)

2️⃣ git cherry-pick – Apply Changes from Existing Commits

  • What it does: Takes a commit from another branch and applies it onto your current branch.
  • Example: To cherry-pick a commit with SHA abcdef1:
  git cherry-pick abcdef1
  • Sample Output:
  [main 1bd23f2] Commit message here
   1 file changed, 1 insertion(+), 1 deletion(-)Code language: CSS (css)

3️⃣ git diff – Show File Differences

  • What it does: Shows changes between commits, commit and working tree, etc.
  • Example: To see differences not yet staged:
  git diff
  • Sample Output:
  diff --git a/file.txt b/file.txt
  index 1234567..89abcde 100644
  --- a/file.txt
  +++ b/file.txt
  @@ -1,2 +1,2 @@
  -Hello, World!
  +Hello, Git!

4️⃣ git rebase – Reapply Commits

  • What it does: Moves or combines a sequence of commits to a new base commit. Useful for making linear histories.
  • Example: To rebase the current branch onto main:
  git rebase main
  • Sample Output:
  Successfully rebased and updated refs/heads/feature-branch.

5️⃣ git revert – Undo Existing Commits

  • What it does: Creates a new commit that undoes changes from a specific existing commit.
  • Example: To revert the last commit:
  git revert HEAD
  • Sample Output:
  [main a123456] Revert "commit message to revert"
   1 file changed, 1 insertion(+), 1 deletion(-)Code language: JavaScript (javascript)

📘 That’s it for the Git patching section! This cheat sheet includes explanations, example usages, and sample outputs for each command. I hope you find it helpful! 🎉

Additional commands to published in next article!

Leave a Reply

Scroll to Top

Discover more from DevOps AI/ML

Subscribe now to keep reading and get access to the full archive.

Continue reading