DevOps

Ultimate Git Guide: From Basics to Advanced Version Control

 Git is a distributed version control system (VCS).
That means it helps you keep track of changes in your code (or any files), collaborate with others, and manage versions of your project.

Think of it like a time machine for your code:

  • You can save snapshots of your project at any point (commits).
  • You can go back to any previous version.
  • You can create branches to experiment with new features without breaking the main code.
  • Multiple developers can work on the same project without messing up each other’s work.

Example:

  • You’re building a website.
  • On Monday, you write the homepage and save it in Git (commit).
  • On Tuesday, you try a new design in a branch.
  • If you like it ? merge into the main project.
  • If you don’t ? discard without affecting the main code.

GitHub is a cloud-based hosting platform for Git repositories.

Git is the tool, and GitHub is a service that makes it easier to:

  • Store your Git repositories online.
  • Collaborate with other developers from anywhere.
  • Share your open-source or private projects.
  • Use features like Pull Requests, Issues, Actions (CI/CD), Project Boards, etc.

Example:

  • You write code on your laptop with Git.
  • You push it to GitHub so your team can see it.
  • Your friend clones the repository, adds a new feature, and makes a Pull Request.
  • You review and merge it.

 

  • Git = Your personal notebook (you write, erase, and track changes).
  • GitHub = A library where you store and share your notebook so others can read, contribute, or collaborate.

 

git-scm.com
git config user.name
git config user.email
git config --global username.name <user-name>
git config --global user.email <user-email>


# general git commands
git init
git status
git add .
git commit -m "message"
git remote add origin <url>
git push -u origin master


# git branch related commands
git branch
git branch <branch_name> # create a new branch
git branch -d <branch_name> # delete the branch
git checkout -b <branch_name>  # switch to branch if not exist it create and switch to the branch

# other useful git commands
git remote # to see all the remote origin
git remote origin
git remote show origin
git remote add <remote> https://www.github.com
git branch -M <branch_name>
git merge <branch_name>

to push the project from your cloned reportitory to a new repo in your own account:
1.git remote set-url origin <new repor url>
2.git push origin master


you may also need to configure your local repor to use your new repor as the defsult push location :
git push --set-upstream origin master


 

Other advance git commands 

Git log

git log

This shows:

  • commit hash
  • author
  • date
  • commit message
git log --oneline
# See only commit messages (short output)
git log --author="panta"
# See commit messages made by YOU only

# Or if your Git email is used:
git log --author="[email protected]"
# See commit messages with a graph
git log --oneline --graph --decorate --all
# See commit messages with full patch changes
git log -p

 

 

git diff

git diff shows changes between:

  • your working directory and staged files
  • staged files and the last commit
  • two commits
  • two branches
  • two specific files in different branches

It highlights:

  • lines added (+)
  • lines removed (-)
  • exact code differences

See changes you haven’t staged yet

git diff
# Shows changes in working directory vs. last commit.

See changes you have staged (git add)

git diff --cached

OR

git diff --staged

Compare your current branch with another branch

git diff <currrent_branch>..<another_branch>

Compare a specific FILE between two branches

git diff branch1 branch2 -- path/to/app.py
# This shows only differences in app.py.

# See side-by-side diff (more readable)
git diff main feature-branch -- app.py | diff-so-fancy

# (or install diff-so-fancy)

Compare two commits

git diff <commit1> <commit2>

Compare HEAD with previous commit

git diff HEAD~1

Show the diff of a single commit

git show <commit-hash>

See a summary (which files changed only)

git diff --stat

 

 

 

Some useful git commands

1 . git clone – Clone a project

Scenario:Your team shares a repository on GitHub/GitLab, and you need to start working.

git clone https://github.com/company/project.git

2 . git branch – Manage branches

Scenario:You want to create a feature branch for new functionality.

git branch dev
git checkout dev

or shortcut:

git checkout -b dev

3 . git status – Check current changes

Scenario:Before committing, you want to see what files changed.

git status

4 . git add – Stage files

Scenario:You modified multiple files but want to commit only one.

git add app.py

Add everything:

git add .

5 . git commit – Save changes

Scenario:You completed the login feature.

git commit -m "Add login functionality with JWT"

6 . git push – Push branch to remote

Scenario:You need to publish your feature branch for a merge request

git push origin dev

7 . git pull – Sync your local repo

Scenario:Before starting work each day, you sync with the team.

git pull origin <branch_name>

8. git diff – See changes

Scenario:You're reviewing what changed before committing or merging.

git diff

Compare branches:

git diff main dev

9. git stash – Save work temporarily

Scenario:You are working on Feature A, but suddenly the manager asks for an urgent bug fix on main.

git stash
git checkout main

After fixing:

git checkout feature-a
git stash pop

10 . git merge – Merge branches

Scenario: Your dev branch is ready to be merged into main.

git checkout main
git merge dev

11 . git rebase – Clean commit history

Scenario: Your branch has many small commits and you want to clean them before PR/MR.

git rebase -i main

Combine multiple commits into one (squash).

 

12 . git revert – Undo a commit without losing history

Scenario:  commit broke production, but you do NOT want to rewrite history.

git revert <commit-hash>

13 . git reset – Undo changes (dangerous on shared branches)

Scenario: You committed something by mistake on your own branch.

Soft reset (keep changes):

git reset --soft HEAD~1

Hard reset (erase changes completely):

git reset --hard HEAD~1

Never use hard reset on a shared branch like main/production.

14 . git clean – Remove untracked files

Scenario:Your repo is full of unwanted temporary files.

Preview:

git clean -n

Delete untracked files:

git clean -f

 

15 .  git log – View commit history

Scenario: You want to see who made what changes.

git log --oneline --graph --decorate

16 . git blame – See who changed a line

Scenario: A function is failing and you want to know who wrote it and when.

git blame app.py

17 . git tag – Versioning (e.g., v1.0.0 release)

Scenario: Production release tagging.

git tag v1.0.0
git push origin v1.0.0

18 . git cherry-pick – Copy a commit from another branch

Scenario: A bug fix on dev is also needed on main.

git checkout main
git cherry-pick <commit-hash>

19 . git bisect – Find which commit broke the system

Scenario: Production broke but you don’t know which commit caused it.

git bisect start
git bisect bad
git bisect good <old-working-commit>
 

Git will help you find the exact broken commit.

20 . git reflog – Recover deleted commits

Scenario: You did a hard reset and lost work — but you want it back.

git reflog

Find the commit and restore:

git reset --hard <commit-id>

 

21 . git restore – Restore files (newer alternative to checkout)

Scenario: You messed up a file and want to restore it to last commit.

Restore one file:

git restore app.py

Restore entire working directory:

git restore .

Restore a staged file:

git restore --staged app.py

 22 . git switch – Easier way to switch branches

Instead of git checkout

git switch dev

Create and switch:

git switch -c feature/login

23 . git shortlog – Who contributed what

Scenario: Managers ask for contribution summary.

git shortlog -s -n

24. git grep – Search inside the repository

Scenario:  You want to find where a variable or function is used.

git grep "login_user"

Search by pattern:

git grep -n "TODO"

25 . git show – Show details about a commit

git show <commit>

Shows:

  • diff
  • author
  • message
  • files touched

26. git ls-files – List all tracked files

Scenario: Useful for automation scripts or debugging.

git ls-files

27 . git rm – Remove file and stage removal

git rm old_code.py
git commit -m "Remove deprecated file"

Remove but keep the file in the working dir:

git rm --cached secrets.json

28 . git mv – Rename files with history

git mv old.py new.py

29 . git archive – Export project as ZIP

Scenario: Client wants code without .git folder.

git archive --format zip HEAD > project.zip

30. git worktree – Work on multiple branches at the same time

Scenario: You want to open different branches in different folders (super useful!).

git worktree add ../dev-branch dev

Now you can open two different branches simultaneously.

 

31. git apply – Apply patches

Scenario: Team member sent a patch file.

git apply fix.patch

32 . git format-patch – Generate patches

git format-patch -1 HEAD

 

33. git log --patch-with-stat – Better log

More readable for code reviews:

git log -p --stat

34.  git config – Configure Git

Set your username

git config --global user.name "Your Name"

Set editor:

git config --global core.editor "nano"

Show all config:

git config --list

 

35 . git remote -v – See connected remotes

git remote -v

Rename remote:

git remote rename origin upstream

36 . git fetch – Download changes without merging

git fetch origin

Useful before rebase/merge.

 

37 . git merge --abort – Cancel a merge

Scenario: Merge conflicts? Want to stop?

git merge --abort

38. git cherry – See commits in your branch that are not in another

Example: see what commits in dev are not in main:

git cherry main dev

39.  git log --since "2 weeks ago"

Time-based filtering:

git log --since="2025-01-01"
git log --since="1 week ago"
git log --until="yesterday"

40. git whatchanged – See changed files per commit

git whatchanged

41. git tag -a – Annotated tag (production releases)

git tag -a v1.0.0 -m "Production Release"
git push origin v1.0.0

 

 

Mastering Remote-to-Remote Git Branch Operations

Managing code across multiple Git repositories is a common real?world requirement—especially when migrating projects, syncing branches between two repos, or managing code for clients from different GitHub accounts.

In this guide, we cover:

  • How to push a branch from one remote repository to another
  • How to compare differences between local and remote branches
  • When and why each command is needed

1. Scenario: You Have Two Repositories

Repo A (original repo)
User: panta77
Branch: master
Repo B (new repo)
User: panta147
Branch: develop (target)

Goal
You want to push code from:
Repo A ? master ? Repo B ? develop
without changing or deleting local branches.

Steps to follows:

1 . Add Repo B as a Second Remote

git remote add neworigin https://github.com/panta147/repo-testing.git

When do you need this?

  • When you want to push your code to another GitHub repository
  • When you want to migrate the project
  • When you want to maintain multiple remotes

Verify remotes:

git remote -v

2 . Push Local master ? Remote develop (Repo B)

git push neworigin master:develop

When do you need this?

  • When you want to push code from one branch into another branch on a remote
  • When your local and remote branch names are different
  • When the destination branch does not exist (Git will create it)

Example Use Case:

You have completed your work in master, but Repo B uses develop as their integration branch. Instead of renaming branches locally, push directly.

3 . View What You Pushed to the Remote develop Branch

Before comparing, update your remote references:

git fetch neworigin

What does this do?

  • Downloads latest branch updates from Repo B
  • Does not merge with your local files
  • Safe & clean

4 . Compare Local master vs Remote develop

A. See all file changes

git diff master neworigin/develop

B. See only filenames changed

git diff --name-only master neworigin/develop

C. See type of changes (Added/Deleted/Modified)

git diff --name-status master neworigin/develop

When do you need this?

  • After pushing code to ensure the remote branch matches your local work
  • When verifying what changes went into the new repo
  • During code review

 

5 . See Remote develop Commit History

git log neworigin/develop --oneline

When is this useful?

  • When verifying if your push was successful
  • When tracking what commits exist on Repo B

 

 

 


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top