Git Commands Every Developer Should Know

Git is a version control system that helps developers track code changes, manage branches, collaborate with teams, and maintain project history. Whether you're working on a personal project or a production application, Git plays an important role in managing your codebase.

Here are the most useful Git commands categorised by their purpose.

1. Repository Setup

Command

Purpose

git init

Initialize a new Git repository

git clone <url>

Clone an existing repository

git config --global user.name "Name"

Configure Git username

git config --global user.email "email"

Configure Git email

git config --list

View Git configuration

2. Checking Changes

Command

Purpose

git status

Check current repository status

git diff

View unstaged changes

git diff --staged

View staged changes

git log

View commit history

git log --oneline

View compact commit history

git show

View details of a commit

3. Adding & Committing Changes

Command

Purpose

git add <file>

Stage a specific file

git add .

Stage changes in the current directory

git add -A

Stage all changes, including deletions

git commit -m "message"

Commit staged changes

git commit --amend

Modify the previous commit

git reset

Unstage changes or move HEAD

git reset --soft HEAD~1

Undo the previous commit but keep changes staged

git reset --mixed HEAD~1

Undo the previous commit but keep changes unstaged

git reset --hard HEAD~1

Remove the previous commit and its changes

4. Branch Management

Command

Purpose

git branch

List local branches

git branch <name>

Create a branch

git checkout <branch>

Switch branches

git checkout -b <branch>

Create and switch to a new branch

git switch <branch>

Switch branches

git switch -c <branch>

Create and switch to a branch

git branch -d <branch>

Delete a merged branch

git branch -D <branch>

Force-delete a branch

git merge <branch>

Merge another branch into the current branch

5. Remote Repository Commands

Command

Purpose

git remote

List remote repositories

git remote -v

View remote URLs

git remote add origin <url>

Add a remote repository

git remote remove origin

Remove a remote

git fetch

Download remote changes without merging

git pull

Fetch and integrate remote changes

git push

Push commits to the remote repository

git push -u origin <branch>

Push branch and set upstream

git push origin --delete <branch>

Delete remote branch

6. Undoing & Reverting Changes

Command

Purpose

git restore <file>

Discard working-directory changes

git restore --staged <file>

Unstage a file

git revert <commit>

Create a commit that reverses a previous commit

git reset HEAD

Reset staging area

git reset --hard

Reset working directory and index

git clean -n

Preview untracked files to be removed

git clean -f

Remove untracked files

git clean -fd

Remove untracked files and directories

7. Stashing Changes

Command

Purpose

git stash

Temporarily save uncommitted changes

git stash push -m "message"

Save changes with a message

git stash list

View saved stashes

git stash apply

Apply a stash without deleting it

git stash pop

Apply and remove the latest stash

git stash drop

Delete a stash

git stash clear

Remove all stashes

8. Removing & Renaming Files

Command

Purpose

git rm <file>

Remove a file and stage the deletion

git rm -r <directory>

Remove a directory recursively

git mv <old> <new>

Rename or move a tracked file

git rm --cached <file>

Remove a file from Git tracking but keep it locally

9. Tags

Command

Purpose

git tag

List tags

git tag <name>

Create a tag

git tag -a <name> -m "message"

Create an annotated tag

git show <tag>

View tag details

git push origin <tag>

Push a specific tag

git push origin --tags

Push all tags

git tag -d <tag>

Delete a local tag

10. Advanced & Useful Commands

Command

Purpose

git cherry-pick <commit>

Apply a specific commit to the current branch

git rebase <branch>

Reapply commits on top of another branch

git reflog

View the history of HEAD movements

git blame <file>

See who changed each line and when

git bisect

Identify the commit that introduced a bug

git grep <text>

Search for text inside tracked files

git archive

Create an archive of repository files

git ls-files

List tracked files

git diff HEAD

Compare working directory against HEAD

Conclusion

Git is more than just commit, push, and pull. It provides powerful tools for managing source code, recovering changes, collaborating with teams, and maintaining a clean development history.

Learning these commands gradually will make everyday development and Git-based workflows significantly easier.

More Dynamics 365 and X++ blogs