Skip to content

Instantly share code, notes, and snippets.

@aldookware
Last active August 20, 2020 08:03
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 aldookware/d18fd90bdd1eef6e448ccff9570ad010 to your computer and use it in GitHub Desktop.
Save aldookware/d18fd90bdd1eef6e448ccff9570ad010 to your computer and use it in GitHub Desktop.
git commands I use often

To initialize a new local repo.

$ git init

To add newly created files to staging

$ git add <file-name> or . for all new files.

To clone a repo

$ git clone <name of the remote repo> # you can also specify which directory you want it to go to. 

To reset a file to an earlier commit: lets say you made changes you unintensionally.

$ git checkout <head of the commit> --  <file-path> <file-path>

To commit without message and not edit current commit message

$ git commit -a --amend --no-edit  # -a is to add new changes to currrent commit. 

To switich from one branch to another

$ git checkout <branch-name>

To switch from one branch to a new branch in one line

$ git checkout -b <new-branch-name> 

To rename a branch both locally and remote: say on github

$ git branch -m old-branch-name new-branch-name

$ git push origin :old-branch-name new-branch-name

To update the current branch

$ git fetch

To check which branches have been merged to a current branch

$ git branch --merged

To check which branches have not been to merged to a current branch

$ git branch --no-merge

To check if a particular branch is merged to a current branch

$ git branch --merged <branch-name>  # you can also specify a HEAD

To remove files from a tree.

$ git rm --cached </path/file-name> # for a single file. 

$ git rm --cached /path/directory/ # for a directory/folder

To check difference between master and feature branch

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

To delete a remote branch and local branch respectively

$ git push origin --delete feature/login  
$ git branch -d feature/login

To delete merged branches except master/develop

$ git branch --merged| egrep -v "(^\*|master|develop)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment