git

Git tricks

gcdcoder

Gustavo Castillo

Posted on December 27, 2020

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
πŸ’– πŸ’ͺ πŸ™… 🚩
gcdcoder
Gustavo Castillo

Posted on December 27, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related