Working with Git

Git States




Basic Git Commands

  • git remote -v
    [shows the remote repository URLs linked to your local repo]

  • git clone "<repository_url>"

  • git add .
    [add all new and modified files (excluding deleted files)]
    OR,
    git add -A
    [add all changes, including deleted files]
    git add <file>
    [add specific file]

  • git status
    [shows the status of your working directory (tracked/untracked/staged changes)]

  • git commit -m "Commit message here"

  • git log
    [displays commit history (press q to quit log view)]

  • git push

Working with Branches

  • git branch
    [shows local branches]
    git branch -r
    [shows remote branches]
    git branch -a
    [shows both local and remote branches]

  • git branch devfeaturebranch

  • git checkout devfeaturebranch
    OR,
    git checkout -b devfeaturebranch
    [create & switch]

  • git push -u origin devfeaturebranch
    [first-time push when the branch is not present in the remote]
    OR,
    git push
    [for subsequent pushes]

  • git branch -d featurebranch
    [delete locally, use -D for forcefully delete if the branch is not merged]
    git push origin --delete featurebranch
    [delete remote branch]

Merge the Latest Code from Main to the Feature Branch

  • git checkout main
  • git pull
  • git checkout devfeaturebranch
  • git merge main
    OR,
    git merge --squash main

Revert a Commit

  • git checkout devfeaturebranch
  • git log
    [find commit ID]
  • git revert <commit_id>
    [creates a new commit to undo changes]
  • git push

Additional Useful Git Commands 

  • git reflog
    [shows a log of all actions (checkouts, commits, resets, merges, etc.) performed in the repository, even if those commits are not visible in git log]

  • git rm <file> --cached
    [will remove only from the staging area, not from the hard disk]

  • git rm <file> -f
    [will delete a file both from the staging area and from the hard disk]

  • git restore <file>
    [It's undo, remove all the changes from the modified state to the original one]

  • git restore --staged <file>
    [removes a file from the staging area but keeps the changes in our working directory, basically an "unstage"]


  • git reset --soft <commit_id>
    [moves HEAD to the given commit]
    git push -f
  • git reset --hard <commit_id>
    [Moves HEAD to the given commit.
    Deletes all changes in the staging and working directory (irreversible unless recoverable via git reflog).
    Use with extreme caution.

  • git diff branch1..branch2

Popular posts from this blog

Introduction to Docker

SOLID Principles

Nuget package | Pushing it to Azure Artifacts

WiX - Windows Installer XML

C# Memory Tricks: Learn How To Master The Garbage Collector

gRPC - Protobuf / Protocol Buffers

Custom Azure DevOps pipeline task extension

C# Questions Part 1