Understanding some useful GIT commands.

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
git stash push

Fetching last stashed files and directories

git stash apply
git stash pop

Checking all last stashed files and directories

git stash show

Checking all stashes in queue

git stash list
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

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}

Stashing specific file

git stash push {filepath}

Stashing multiple files

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

Clearing stash queue

git stash clear

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}
git reset --hard HEAD
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.

git reset --soft {commit-id}

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.

git reset --mixed {commit-id}
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}

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

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

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



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