🤖 My top 10 Git commands I use
- Published on
- • 4 min read
- Authors
- Name
- Kien Dang
There are a ton of commands and many things you can do with Git. Here are the ones I commonly use for my workflow. These include fixing commit messages, debugging with Git and finding shortcuts to improve my workflow.
Table of Contents
- 1. Changing commit messages
- 2. Uncommit and remove some files that was accidentally pushed
- 3. Quickly bounce back to a previous branch
- 4. Rebasing your branch to remote origin or on another branch
- 5. Resetting your changes to remote branch
- 6. Git stash saving with name
- 7. Taking all of main branches changes
- 8. Renaming your branch after its been pushed
- 9. Using Git to debug using Git bisect
- 10. Git worktree to jump between branch builds
- Conclusion
1. Changing commit messages
git commit --amend -m "new commit message"
git push -f
This command allows you to edit your commit messages if you've made a typo or a mistake in the convention you use to create commit messages.
2. Uncommit and remove some files that was accidentally pushed
git reset --soft HEAD^1
This brings back all the files to staging and remove whatever files you need
git restore --staged <individual_file>
3. Quickly bounce back to a previous branch
git checkout --
Shorthand for going back to the previous checked out branch
4. Rebasing your branch to remote origin or on another branch
git fetch origin master:master && git rebase -i master
- If you have conflicts, fix them and then
git add .
and do step 3. git rebase --continue
git push -f
after you successfully rebase
Allows me to start right at the tip of main branch or helps me resolve any conflicts I have in the main branch
5. Resetting your changes to remote branch
git reset --hard origin/branch-name
or
git reset HEAD~1
git clean -f -d
These steps are destructive, you will not be able to retrieve your changes
6. Git stash saving with name
git stash save "WIP: making progress on foo" --include-untracked
Then run git stash list
and find what you saved
git apply stash@{n}
7. Taking all of main branches changes
git checkout --ours .
When you simply just want to take all the changes made from master.
8. Renaming your branch after its been pushed
If you are already on the branch:
git branch -m new-name
If you're on different branch
git branch -m old-name-branch new-name
git push origin:old-name new-name
Then reset the upstream branch:
git push origin -u new-name
This comes in handy when I accidentally have the wrong branch name
9. Using Git to debug using Git bisect
This actually comes in super handy, there are times where a bug randomly appears and you have no idea what caused it and it could have been a specific package bump. I have used this command multiple times and found bugs that have been difficult to find or when they have unknown origins.
Go to the bad branch and start it:
git bisect start
git bisect bad
git bisect good <commit-number>
- find the commit number that you know the bug was not there before- Continue checking whether the commit was bad or good by typing
git commit bad/good
As the bisect goes through, it does a binary search and tell you the commit where the bug was introduced.
10. Git worktree to jump between branch builds
Ever wanted to quickly switch branches but didn't want to stash your changes and then re-apply them when you switched branches?
Git worktree solves this issue by creating a brand new folder for that specific branch. It has been super helpful for context switching in between workflows as you don't have to stash any changes at all and have two separate builds installed seamlessly.
git worktree add ../worktree-name-folder <branch-name>
This will add a folder outside of your project rootcd ..
andcd worktree-name-folder
code .
to open a new instance of VSCode in that foldernpm install
or whatever command used to install all your project files
Now you can start up your project as if you've checked into this branch and do what you need to do.
Once you're done you can just change back to your original folder (yay no more need for stashing changes!).
Remove your worktree by:
git worktree list
- shows a list of different worktree namesgit worktree remove worktree-name-folder
, you might need to add--force
if you have unstashed changes
Conclusion
And thats it! Those are my most common used Git commands I use at work.