Skip to content

Instantly share code, notes, and snippets.

@j471n
Last active January 4, 2023 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j471n/6d2e74468615675eabc1e88540bacfe2 to your computer and use it in GitHub Desktop.
Save j471n/6d2e74468615675eabc1e88540bacfe2 to your computer and use it in GitHub Desktop.
This contains the important and useful commands that you need while using git

Useful Git commands

Change branch Name in git

// If you're currently on the branch you want to rename-
git branch -m new_name 

// Or else-
git branch -m old_name new_name 

// You can check with-
git branch -a

// As you can see, only the local name changed Now, to change the name also in the remote you must do-
git push origin :old_name

//This removes the branch, then upload it with the new name:
git push origin new_name

Initialize Git repository with different name than main/master

// "hello" will be the name of the repo
git init -b hello

Remove remote url

git remote remove origin

Set new remote url

git remote set-url origin git://new.url.here

Revert last commit after pushed to remote

git revert <previous label or sha1>

Reset fork to Upstream

git remote add upstream /url/to/original/repo
git fetch upstream
git checkout main
git reset --hard upstream/main  
git push origin main --force 

Visualize git graph

git log --all --decorate --oneline --graph

Create a new branch and push to remote

git checkout -b <branch>

# Edit files, add and commit. Then push with the -u (short for --set-upstream) option:
git push -u origin <branch>

# Git will set up the tracking information during the push.

Force Push on Github

git push origin main -f
#or
git push -f

Git Squash using rebase -i

git checkout <branch_name>
git rebase -i HEAD~<Count> # count could be anything like HEAD~4

# Then editor will show up 
# replace "pick" with "s" or "squash" and then Press ESC and type :wq to save and exit
# then another edior will show up to edit the commit messages
# remove all the commit messages except the first one
# save it using ESC forward by :wq 
# then you need to force push it to the github
git push -f # Force push

Find unused npm packages in package.json

Reference

You can use an npm module called depcheck (requires at least version 10 of Node).

Install the module:

 npm install depcheck -g

 or

 yarn global add depcheck

Run it and find the unused dependencies:

depcheck

The good thing about this approach is that you don't have to remember the find or grep command.

To run without installing use npx:

npx depcheck 

Remove any directory-

# To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:
rm -rf dirname

Exit from Vim editor

#press "Esc" and than 
#if esc doesnt work it means you are on easy mode use "control" + "o"
:wq #to save and quit
:q #to quit
:q! #to force quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment