Skip to main content

Command Palette

Search for a command to run...

Git Commands

Updated
7 min read
Git Commands
B

BSc CSIT Graduate

Git commands are instructions that we give to git to perform action such as tracking changes, switching branch, creating commits etc (Rebase ko barem pani yesmia lekha).

Following are some usefull git commands

git init

This initializes a new git repository in an existing project. This command creates a new hidden folder (.git). It contains all the necessary repository files and subdirectories to manage version control for your project.

To initialize the git in your project, go to root folder and run following command

git init

git status

This command is used to check and display the current status of your project. This command tells the informaiton like which files are modified, which files are staged for the next commit, and which files are untracked. This is very helpful command to know the state of your project.

git status

git add

The git add command is used in order to begin tracking a new file or stage changes in the working directory so that the changes can be included in the next commit. Staging is an essential step in the Git workflow because it allows you to choose which changes should be included in the commit and which ones should be excluded.

#1 to add all the changes and new files
git add . 
#2 to add single file
git add filename
#3 to add multiple file 
git add filename filename

git commit

Commit is a snapshot of the project at any specific point in time. Each commit has a unique identifier (a hash) and includes a message that describes the changes made in that commit. The git commit command is used to save staged changes in your Git repository as a new commit.

git commit -m "message that describes the change made"

git log

This command is used to list the commit history of a repository. It shows information about past commits, including hashes, authors, dates, and commit messages.

#1 displays history commits
git log

#2 Displays commits history in short
git log -oneline

#3 Displays details of a commit
git log -p <commit_has>

#4 Display last n number of commits
git log -n <number_of_commit>

#5 display for specific branch
git log <branch_name>

#6 display commits by auther
git log --author <author_name>

git diff

This command is used to compare various parts of your git repository

#1 Compares working direcotry to last commit 
git diff

#2 compares two commits
git diff <commit_has> <commit_hash>

#3 compare staged changes to last commit
git diff --staged

git pull

The git pull command is used to fetch changes from a remote repository (usually the origin) and merge them into your current branch. It essentially combines two operations git fetch and git merge.

Fetching changes (git fetch) - Git fetches new changes from the remote repository (usually named "origin" by default) into your local repository without immediately merging them into your current branch. Merging Changes (git merge) - After fetching changes, Git automatically tries to merge them into your current branch.

#1 fetch form remote main branch and merge to current branch
git pull
#2 fetch from another remote branch to current brach 
git pull origin branchname

git clone

The git clone command is used to make a copy of a remote Git repository on a local machine. It allows us to start working on a project that is hosted on a remote server by downloading all the project’s files, commit history, and branches

git clone <url>

git push

This command is used to upload local commits to a remote repository, allowing developers to share their work and synchronize changes across multiple machines.

git push -u origin main # for first time then  git push

git stash

The git stash is a useful Git command used to temporarily save the changes in your working directory and index without committing them. This is particularly handy when you need to switch to a different branch or perform some other operation that requires a clean working directory.

The main purpose of git stash is to save changes that are not ready to be committed yet but need to be temporarily stored. This allows you to switch branches or perform other operations without committing to halffinished work.

#to save changes without commit
git stash

# retrieve stashed change
git stash apply

#list all the stashed changes
git stash  list

git checkout

This is versatile command with the multiple uses.

#1 to switch to existing branch
git checkout branch_name

#2 create and switch to a new branch
git checkout -b branch_name

git branch

The git branch command in Git is used to list, create delete, and manage branches with a Git repository. Branches are used to isolate development work and manage different lines of code.

#1 list all branchs
git branch

#2 create a new barnch
git branch <branchName>

#3 delete 
git branch -D <branchname>

#4 rename
git branch -m newname

git reset

The git reset is a powerful Git command that allows you to reset the current branch to a specified state. It’s used to undo changes, unstaged files, or move the branch to a pointer to a different commit.

It is a Git command used to move the HEAD (and the current branch pointer) to a specified commit and optionally update the staging area and working directory.

HEAD points to the current branch, while the branch pointer points to a commit; when a branch moves, HEAD follows it.

There are three primary options for git resets
1. Soft reset: This moves HEAD and the current branch pointer to the specified commit without changing the staging area or working directory. All changes remain staged for the next commit.

  1. Mixed reset: Moves HEAD and the branch pointer to the specified commit, unstages all changes, but keeps them in the working directory. Useful when you accidentally staged files.

  2. Hard reset : Moves HEAD and the branch pointer to the specified commit, removes all staged and unstaged changes from the working directory, effectively discarding all changes. Use with caution.

Use cases are give below

#1 Undo the last commit(When you commit to early or with the wrong message
git reset -- soft HEAD^
(Keeps changes staged)

#2 Unstage files accidentally added ( you did git add. and wanna unstage some )
git reset <file_name>

#3 Discard all local changes.....( you tried some changes but want to start fresh)
git reset --hard HEAD 
(Removes all staged and unstaged changes in the current branch.)

#4 Move the branch to a specific commit
git reset --<commit_hash>

etc,..

git merge

The git merge is a Git command that is used to integrate changes from one branch to another branch. This is typically used to combine the changes made in a features branch back into the main branch.

git merge <branch_name>

git squash

The git squash is not a standalone Git command, but it refers to a technique used to combine multiple Git commits into a single, more meaningful commit. This is typically done to maintain a cleaner and more organized Git history.

git merge --squash <branch_name>

//Squash combines changes ... you need a single commit to save them.

git rebase

Git rebase is a Git command used to move or reapply commits from one branch onto another, creating a linear commit history. It rewrites commit hashes for the rebased commits. After rebasing, you won’t see a merge commit, but all the changes from your branch will appear as if they were applied on top of the target branch from the beginning. In other words, it acts as if the branch was created from the latest state of the main branch, even though it originally started earlier. Rebase is used instead of merging to avoid unnecessary merge commits and keep the history clean.

#suppose you have a branch feature  and you want to rebase on top of the main branch

#1. make sure you are on the main branch
git checkout main
git pull origin main

#2. switch to the feature brahcn
 git checkout feature

#3 rebase the feature branch on the main branch
git rebase main

git revert

The git revert command is used to create a new commit that undoes the changes made by a previous commit or a range of commits. It effectively applies the inverse of the specified commit(s) to the current branch, allowing you to ‘revert the changes without altering the commit history

#single commit  revert
git rever <commit_hash>

#range of commit 
git revert <commit_hash> .. <commit_hash>