Git common use cases/commands

Assuming that you are familiar with git and have some idea about what is git and why we use it. Here I list down some common and frequently used command and some use cases.

Create a new branch

You can create a git branch by simple command git branch followed by branchName to be created.
git branch <branchName>

Check current branch

If you want to check the current branch you are on, you can use command:

git branch

Push branch to remote repository: When you create a new branch, it will be on your local machine and will not be accessible to teammates until you commit it to the remote repository. Pushing a branch to remote is easy as creating a branch:

git push origin <branchName>

When you push a new branch on the remote, teammates can fetch the branch to their local repository by using “git fetch” command.

Switch between branch

Let’s assume that you are on ‘branchA’ and want to work on ‘branchB’. You can change current branch by checkout to the  branchB.

git checkout <branchName>

Check status

you can check status of modified, deleted, untracked and staged files by command:

git status


Commit changes

Once you have done with your work and want to push your changes to the remote. You have to commit your files and than push changes to the remote.

If you check git status after modifying the files, It will show you the file status whether it is modified or a new file(untracked).  Your modified files are marked as “Changes not staged for commit”. Here git introduces a new term staged. We can assume Staged as an area between local and the remote repository. When you want to commit your changes, you need to move your changes to staged first. To do so, you can use git add command followed by the file name(s).

git add filename

If there are multiple files to be added, You can add multiple files at once instead of executing git add command for each file.

git add file1 file2 file3 and so on

At this stage, if you check git status, then you will see that all added files are marked as  “Changes to be committed”. Now you can commit your staged files & directories.

git commit -m “commit message for your reference”

When you make a commit, that commit is only on your local repository. To move it to remote, you have to push your commits to remote.

git push origin <branchName>

Use Case: Add all files and ignore specific

There may be a case when you have a large number of files to add except some files. In this scenario, adding files is frustrating. There a way by that you can add all files at once to move them to staged. In this case we also added those files which are not to be committed. So we have to move back those files to their state(modified/untracked) by rest command.

Add all Files or directories

git add -u

Reset a file

git reset -- main/dontcheckmein.txt

reset a directory

git reset -- main/*


Check modifications in a file

you can use git diff command to check the differences in a file before and after the modifications.

git diff <fileName>

Revert all changes to a file

If you want to discard all changes made in a file than you can use checkout command.

git checkout <fileName>

Discard all changes on the branch

git reset --hard


You can check git commit history by command “git log”. It will show a list of commits on the branch with some details about the commit like Author, commit date & time, commit message and hash.

git log

Revert a commit

Git provides a feature to take your steps back if you made a wrong step. There may be a case when you need to revert you commit. You can revert a commit by using commit hash. If you doesn’t have the commit hash than you can find it in the “git log” command.

git revert <commitHash>

Use Case: Create new branch(replica) from existing branch

Another use case is when it is required to create a new branch ‘branchB’ which is exact copy or we can say replica of another branch ‘branchA’, what you have to do is just check to the existing branch ‘branchA’ and than create branch ‘branchB’.

Check current branch you are working on:

git branch

git checkout branchA

Take pull for the existing branch to update it in your working directory.

git pull origin branchA

create a new branch ‘branchB’ which is a replica of branchA. This command will create a new branch as well as checkout to the newly created branch.

git checkout -b branchB

When you will check the current branch using git branch, is will show that you are on branchB. Now we can push it to remote.

git push origin branchB

Delete a branch

Deleting a git branch can be of two types. Either you want to delete a local branch or you want to delete a remote branch. To delete a remote branch you can use:

git push origin --delete <branchName>

You can delete a local branch by following command:

git branch -d <branchName>

You can also use -D, which is an alias for –delete –force, which deletes the branch “irrespective of its merged status”.

git branch -D <branchName>


Hope it will help you.


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top