Understanding some useful GIT commands.

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Image for post
Image for post

If you are working on any software project, then there are good chances that you already heard of GIT before. In this article we will deep dive into some useful but not so common git commands which can make out life easier.

Stashing in GIT

It is used to store all the local changes (staged and unstaged changes) for future use and reverts them from your working copy. It takes you back to the working directory HEAD (last commit).
NOTE: It will not change the untracked files and directories.

Stashing all modified files

For stashing all (staged and unstaged) files:

git stash

or

git stash push

It stores all the changes in a queue and these changes will be stored in your local repository queue, not in the remote repository.

Fetching last stashed files and directories

git stash apply

or

git stash pop

These commands will bring back the previous stashed change to the current working directory. The difference between ‘apply’ and ‘pop’ is that in case of ‘pop’, it will bring back the changes, but will not remove it from the stash queue, you have to manually remove it from the queue using ‘git stash drop’, in case of ‘apply’ it will bring back last changes and removes it from the stash queue.

Checking all last stashed files and directories

git stash show

Shows all the file which got saved in queue in the last stash.

Checking all stashes in queue

git stash list

Shows all the stashes present in the queue, It contains information like stash id (like stash@{0}, stash@{1}), branch name from where they got stashed and last commit description.

example:

stash@{0}: WIP on feature/TE-4312: 61307eaca last commit message on feature/TE-4312 branch
stash@{1}: WIP on feature/TE-4513: bba77bd0a last commit message on feature/TE-4513 branch
stash@{2}: WIP on feature/TE-1234: dc42acaf1 last commit message on feature/TE-1234 branch

stash@{0}, stash@{1}, and stash@{2} are the stash ids.

feature/TE-4312, feature/TE-4513, feature/TE-1234 are the branch names.

61307eaca, bba77bd0a, dc42acaf1 are the commit ids. and the remaining part are the commit messages.

Fetching older stash files

We can perform multiple stashes and can add them in the stash queue. We can pull out the changes of older stash using:

git stash pop {stash-id}

We can use ‘git stash list’ to check the stash-id of the stash which we want to
pull.

Stashing specific file

git stash push {filepath}

It will stash a given file.

Stashing multiple files

git stash push {filepath1} {filepath2} ... {filepathN}

This command is used when we want to stash multiple files, but not all the files.

Clearing stash queue

git stash clear

It will remove all the stash items from the queue.

Resetting branch

It resets the current branch HEAD to a specific state, it has 3 different forms --hard, --soft and --mixed.

Hard resetting

It is the most widely used option, it is used when we are 100% sure to go to a particular commit and want to discard all the changes done after than including the staged, unstaged changes and all the commit done after that commit.

git reset --hard {commit-id}

If you just want to remove all the staged changes and move back to the HEAD of the branch (most recently committed code) then:

git reset --hard HEAD

If you want to remove all the commits happen on any particular branch and resets the branch:

git reset --hard HEAD^

Soft resetting

Resets the pointer to the commit with the given commit id and removes all the commits done after that, but it will not delete them, it moves all the files and directories changed in those commits in staged state.

It is used in the scenarios like we made some mistake in the last commit or added some extra files. So we can reset the files and bring them back in staged state, remove few files and can commit again.

git reset --soft {commit-id}

Then we can unstaged the file which we want to update using ‘git reset {name of the file}’, update it and can push it again.

Mixed resetting

It is the default resetting policy, it is similar to --soft resetting, the only difference is that it takes all the files to unstaged state.

It is used in the scenarios like when we want to commit 1 or 2 files and by mistake we added all the files in the commit. So we can reset all the files in unstaged state and can select our desired files which we want to commit again.

git reset --mixed {commit-id}

or

git reset {commit-id}

Reverting commit

Reverts all the changes done in the given commit-id and add a new commit to maintain the history of deleting the commit. This command requires your working tree to be clean (no modifications from the HEAD commit).

git revert {commit-id}

Reverting undoes a commit by creating a new commit, where as git reset does alter the existing commit history. For this reason, git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch.

Fetching remote branches

It is used to fetch all the remote branches which may be created by other developers working on the same repository.

git fetch --all

It will fetch all remote branches.

Removing unwanted files

Used to remove untracked, ignored files from the repository. Sometimes IDE creates some files in our local project repository, or my mistake we adds some files. So to remove all these files we can use these commands.

Removing untracked files

To interactively delete untracked files:

git clean -d -i

This will ask us which all files and directories we want to delete. We can delete all files together, can delete using a pattern like (*.js) or can delete using a sequence number etc.

The -d option tells git to remove untracked directories too. If you don’t want to delete empty untracked directories, omit -d option.

-i is for interactive.

-n is used to do a dry-run, it will show all the files that will be deleted.

Removing untracked files from a directory

If you want to limit the clean operation to given directories, pass the paths to the directories to be checked for untracked files as arguments to the command. For example, to check for files under the src directory, you would run:

git clean -d -n {src}

Removing ignored files

If you want to remove only the ignored files and directories, use the -X option:

git clean -d -n -X

Written by

Full stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store