10 powerful Git tricks every developer should know – including how to force push with practical code examples

Table of content

  1. Introduction
  2. Trick 1: Amending the previous commit with a new message
  3. Trick 2: Ignoring files that have already been committed
  4. Trick 3: Reverting a commit or a range of commits
  5. Trick 4: Squashing multiple commits into one
  6. Trick 5: Stashing changes for later use
  7. Trick 6: Finding the commit that introduced a bug
  8. Trick 7: Working with multiple Git repositories
  9. Trick 8: Using force push with caution and practical code examples
  10. Conclusion


Are you a developer looking to master Git and take your coding skills to the next level? Look no further! In this article, we will be exploring 10 powerful Git tricks that every developer should know – from basic tips to more advanced techniques. And, the best part is, we'll be using practical code examples to illustrate each trick!

Before we dive into the world of Git, let’s first explore what it is and why it’s important. Git is a version control tool that allows developers to keep track of changes made to their code over time. It consists of several commands that can be used to manage your code, track changes, and collaborate with other developers. By mastering Git, you’ll be able to work more efficiently, collaborate more effectively, and avoid costly mistakes.

If you’re new to Git, don’t worry! We’ll start with some basic tricks to help you get started. And, if you’re already familiar with Git, you’ll still find some advanced tips and tricks that you may not know about. So, let’s get started and explore the world of Git together!

Trick 1: Amending the previous commit with a new message

Mistakes are inevitable, even for the most experienced developers. One common mistake is forgetting to add a file or adding unnecessary files to the previous commit. Fortunately, Git provides an easy way to amend the previous commit using the --amend option.

To amend the previous commit with a new message, follow these steps:

  1. Stage the changes to be committed using git add.

  2. Run the command git commit --amend -m "New commit message".

This will open the default text editor with the previous commit message. Edit the message as necessary and save the changes.

  1. Run git status to ensure that the changes are staged correctly.

  2. Finally, push the changes using git push --force. Note that this command will overwrite the previous commit, so use it with caution.

Using the --amend option is a powerful way to fix mistakes and keep your Git history clean and organized. With practice, you'll find that it becomes a natural part of your Git workflow.

Trick 2: Ignoring files that have already been committed

One of the most useful Git tricks that every developer should know is how to ignore files that have already been committed. This trick can save you a lot of time and headaches when working on complex projects with many files, as it allows you to keep files that are specific to your local environment or personal preferences from being pushed to the repository.

To ignore files that have already been committed, you can create a .gitignore file in your project directory and add the filenames or paths of the files you want to ignore. Git will then exclude these files from further commits and pushes to the repository.

For example, let's say you have a file named "secrets.txt" that you accidentally committed to the repository. To ignore this file, you can create a .gitignore file and add "secrets.txt" to it. Git will now ignore this file in all future commits and pushes.

It's important to note that ignoring files that have already been committed requires a bit more care and attention than ignoring files that have not yet been committed. You should make sure to remove the ignored files from the repository using git rm or git rm --cached before adding them to the .gitignore file, to ensure that they are not accidentally included in future commits.

Overall, ignoring files that have already been committed is a powerful Git trick that can help you manage your project more effectively and save time and effort in the long run.

Trick 3: Reverting a commit or a range of commits

Sometimes, we realize that a commit we made is not correct, and we need to undo it. No worries, with Git, you can easily do it! Reverting a commit or a range of commits is also easy to do using Git.

To revert a single commit, you can use the git revert command followed by the commit hash. Git will create a new commit that undoes the changes made by the previous commit. This way, you can keep track of the changes you made and still undo them.

To revert a range of commits, you can use the git revert command with the range of commits in which the changes were made. Git will create a new commit for each commit being reverted, to keep track of the changes made.

It is important to note that reverting a commit is not the same as deleting it. The commit and its changes will still be present in the Git history, but with a new commit that undoes those changes.

With this trick, you can quickly and easily undo a commit or a range of commits, without losing any of the work already done. Keep exploring the other Git tricks to discover more useful capabilities that will improve your workflow.

Trick 4: Squashing multiple commits into one

Sometimes, our Git commit history can get messy, with lots of small commits that make it difficult to keep track of changes or revert to an earlier version. That's where the "git rebase" command comes in handy, allowing us to squash multiple commits into one, creating a clean and organized commit history.

To squash commits, first, make sure you're on the branch you want to modify, and then run the following command:

git rebase -i HEAD~<number_of_commits_to_squash>

This will open up an interactive mode, where you can specify which commits to squash, edit the commit messages, or delete unnecessary commits. You should see something like this:

pick 1f6a3dc Add feature A  
pick 2c6f7ed Fix bug B  
pick 3bf67ed Update documentation  

To squash commits 2 and 3 into one commit, change their prefix to "squash" or "s" (for short), like this:

pick 1f6a3dc Add feature A  
s 2c6f7ed Fix bug B  
s 3bf67ed Update documentation  

Then, save and exit the editor. Git will prompt you to edit the commit message, which will now include all the changes from the squashed commits.

# This is a combination of 2 commits.  
# This is the 1st commit message:  

Add feature A  

# This is the commit message #2:  

Fix bug B  
Update documentation  

# Please enter the commit message for your changes. Lines starting  
# with '#' will be ignored, and an empty message aborts the commit.  
# On branch master
# Changes to be committed:  
# modified:   fileA  
# ...  

Finalize the commit message, and you're done! The squashed commit replaces the original commits in the history, making it easier to read and maintain.

In summary, squashing commits is a great way to keep the Git history clean and organized. Remember to use the interactive mode of "git rebase" and prefix the commits you want to squash with "squash" or "s". And don't forget to write clear and concise commit messages, describing the changes made in the commit. Happy coding!

Trick 5: Stashing changes for later use

Sometimes, you may be working on a feature or fixing a bug, and suddenly realize that you need to switch to another branch, but you don’t want to commit your incomplete changes yet. That’s where Git’s stash command comes in handy.

To stash your changes, use the command git stash. This will take all the changes in your working directory that haven’t been committed yet and save them to a “stash”. You can then switch to another branch or do whatever else you need to do.

To retrieve your stashed changes, use the command git stash apply. This will retrieve the latest stash and reapply the changes to your working directory. If you have multiple stashes, you can apply a specific one by appending its index to the command, like so: git stash apply stash@{1}.

You can also choose to delete a stash using the command git stash drop. If you want to apply and drop a stash in one command, use git stash pop instead of git stash apply.

The git stash command is a powerful tool to temporarily save your changes without committing them. With this trick under your belt, you’ll be able to switch between branches more easily and keep your working directory clutter-free.

Trick 6: Finding the commit that introduced a bug

To find the commit that introduced a bug in Git, you can use the git bisect command. This tool is especially useful when you have a large codebase and need to trace a bug to its source. The command works by performing a binary search between two points in your Git history, trying to determine which commit introduced the bug.

The workflow for using git bisect is as follows:

  1. Start the bisect process by running git bisect start.
  2. Mark the current commit as "bad" with git bisect bad.
  3. Checkout an older commit that you know is "good" with git checkout <commit>.
  4. Mark the commit as "good" with git bisect good.
  5. Git will now automatically checkout a new commit. Test if the bug is still present in this commit.
  6. Depending on whether the bug is present, mark the commit as "bad" or "good" using git bisect bad or git bisect good.
  7. Git will repeat the process, automatically checking out a new commit until it finds the commit that introduced the bug.

By the end, git bisect will have narrowed down the bug to a specific commit, making it much easier to fix. This is just one of the many powerful tricks you can use with Git to improve your workflow, so keep experimenting and learning!

Trick 7: Working with multiple Git repositories

As a developer, you'll often work on multiple projects, each with their own Git repository. How do you manage them all efficiently? Here are a few tips:

  • Clone all repositories into a parent directory: Create a parent directory and clone all your repositories into it. This way, you can easily navigate between them and push/pull changes without having to cd into each repository individually.
  • Use Git submodules: If you have a Git repository that requires files from another repository, you can use submodules to link them together. This way, when you clone the main repository, it will include a reference to the submodule, and you can update it separately when necessary.
  • Create Git aliases: If you find yourself running the same Git commands over and over again, create aliases for them. For example, instead of typing git pull origin master every time, you could create an alias called gpom.
  • Use Git worktrees: Git worktrees allow you to have multiple working trees (i.e., directories containing a checked-out copy of your repository) from a single repository. This is useful if you need to work on multiple branches or commits simultaneously.
  • Use Git hooks: Git hooks are scripts that run automatically before or after certain Git actions (e.g., before committing). You can use them to automate tasks like linting or running tests across multiple repositories.

By following these tips, you can work more efficiently across multiple Git repositories. Experiment with each one and see what works best for you!

Trick 8: Using force push with caution and practical code examples

One of the most powerful Git tricks at your disposal is force push. With force push, you can override changes in the remote repository and push your local code as the new version. However, it's important to use this feature with caution as it can cause irreversible damage to the repository if not used correctly.

To force push, you need to use the -f or --force flag with the git push command. For example, git push origin master -f will force push your local changes to the master branch of the remote repository.

Before using force push, make sure you have a backup of your code in case something goes wrong. It's also important to consider the impact on other team members who may be working on the same repository.

If you need to undo a force push, you can use git reflog to find the previous commit and git reset to revert to that commit.

In summary, force push can be a powerful tool in your Git arsenal, but it should be used with caution and only after careful consideration of the potential impact on your code and team members. Always make sure to have a backup and be prepared to undo the push if necessary.


In , Git is a powerful tool that every developer must know how to use effectively. The 10 Git tricks discussed in this article can help you become more efficient and productive in your coding workflow. From using Git stash to saving your work temporarily to performing a force push with practical code examples, these tricks can help you overcome common challenges developers face when working with code.

However, it is important to remember that Git is a tool that requires practice to master. To really hone your skills, try experimenting with different commands and options, and learn from your mistakes. Don't be afraid to ask for help or seek out resources like online tutorials or forums.

Ultimately, the more you use Git, the more comfortable you will become with its features and functionalities. So keep practicing, keep learning, and keep pushing yourself to become a better developer.

My passion for coding started with my very first program in Java. The feeling of manipulating code to produce a desired output ignited a deep love for using software to solve practical problems. For me, software engineering is like solving a puzzle, and I am fully engaged in the process. As a Senior Software Engineer at PayPal, I am dedicated to soaking up as much knowledge and experience as possible in order to perfect my craft. I am constantly seeking to improve my skills and to stay up-to-date with the latest trends and technologies in the field. I have experience working with a diverse range of programming languages, including Ruby on Rails, Java, Python, Spark, Scala, Javascript, and Typescript. Despite my broad experience, I know there is always more to learn, more problems to solve, and more to build. I am eagerly looking forward to the next challenge and am committed to using my skills to create impactful solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top