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.

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

Stashing all modified 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

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

Hard resetting

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

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 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

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

git fetch --all

It will fetch all remote branches.

Removing unwanted files

Removing 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

git clean -d -n {src}

Removing ignored files

git clean -d -n -X

Full stack developer