DEV Community

Cover image for Git tricks
Gustavo Castillo
Gustavo Castillo

Posted on

Git tricks

Some useful git tricks I use all the time, the list can be updated later.

# Stash track/untrack changes
$ git stash -u or git stash --include-untracked
Enter fullscreen mode Exit fullscreen mode
# Rename local branch
# If you are on the branch you want to rename:
$ git branch -m new-name

# If you are on a different branch:
$ git branch -m old-name new-name 

# Delete the old-name remote branch and push the new-name local branch.
$ git push origin old-name new-name

# Reset the upstream branch for the new-name local branch. Switch to the branch and then:
$ git push origin -u new-name
Enter fullscreen mode Exit fullscreen mode
# Add file to the last commit (avoid this if the commit is already public 🙅🏼‍♂️)
$ git add my_missing_file
$ git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode
# List all origin from the remote repo
$ git remote prune origin --dry-run

# Prune origin in your local repo
$ git remote prune origin

# Prune when you fetch from a remote
$ git fetch --prune

# List all branches which have been merged
$ git branch --merged

# Prune local branches which have been merged
$ git branch -d branch-to-delete

# List all branches which have been NOT merged
$ git branch --no-merged

# Prune all branches which have been NOT merged
$ git branch -D un-merged-branch-to-delete
Enter fullscreen mode Exit fullscreen mode

Update a local fork at the terminal Explanation

$ git remote -v
$ git remote add upstream [github url]
$ git remote -v
$ git fetch upstream
$ git checkout master
$ git merge upstream/master
$ git push origin master
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker

An additional tip, when I am getting ready to run a sketchy git command I make a backup branch.

git branch backup
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sq5rix profile image
Tom

Nice list worth expanding

Collapse
 
deven96 profile image
Diretnan Domnan

Nice one!