DEV Community

Cover image for Git tricks
Gustavo Castillo
Gustavo Castillo

Posted on

15 2

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post β†’

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!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay