Opinions Tutorials

TIL, Handy Git Commands, npm ls and more

This week of Things I learned, I would like to write about some git commands. These commands are parts of my daily usage. I would end up googling them every time they needed and go through a lot a links until land on the correct result. To save time I have gathered them at one place.

  1. Remove Git branch locally and remotely
  2. Configure Git username and email
  3. Look for dependency in a project or npm search dependency tree
  4. Discard Unstaged Changes

1. Remove Git branch locally and remotely

The excerpt for this Stack overflow link is as follows

    # to remove local branch
git branch -d branch_name

# to remove remote branch
git push <remote_name> --delete <branch_name>

# git push origin --delete updateTong9

2. Git command to configure user name and email

When you create a new git project and push it to github, the global user name and email is picked up. If you haven’t configure them globally you can test them by running following command in the terminal.

    # to list global configuration
git config --global --list

# to list git configuration of current project
git config --list

If no name or email is configure than we have a dummy gravatar icon on git hosting platforms i-e github, gitlab. When you set you email either globally or locally ( repsitory specific) the avatar is picked from

Related Links

Git/Github push tip

There is one more use case to setup local git configurations. For example at work I have different global user and email. When I work on personal toy project and push commits, the are pushed with my office identity i-e global configs. May be I want the commits to be tracked by different user. In that case the only solution to setup repository specific configuration using following git commands. Excerpt taken from atlassian article.

    # globally
git config --global "FIRST_NAME LAST_NAME"
git config --global "[email protected]"

# repository specific
git config "[email protected]"

3. NPM Search dependency tree

The urgency to search through your node_modules for certain npm project is when github alerts us about potential security vulnerability

Security alert from github about potential security vulnerabilities in our repos, git commands from
Security alerts for github to update the dep

In our case I had to search for minimist dependency. I haven’t used it directly. May be one of my dependency have used or may be dep of dep have used. One can imagine the nested hierarchy here. So npm command to help in such condition is

    npm ls 

npm ls minimist

For me I got following result.

A nice tree is generated by npm showring where exactly the dependency is used file in git commands by
In my case eventually firebase is using the vulnerable minimist package

Now updating firebase might fix it. (I think github can also auto fix by creating a pull request).

4. Git Discard unstaged changes

Visual Studio has this nice feature to discard changes. You can doddle around and just discard all the changes you made. Especially when you don’t want to use stash

Git discard unstaged changes in Visual Studio Code from

Yet another stackoverflow answer for the r.escue This time I extracted the following git command which helps in my case.

git checkout -- .

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.