DEV Community

Lucas Barret
Lucas Barret

Posted on

May the (Push) Force be with you

Introduction

As a novice developer, I used to fear the power of the push force command. However, I've come to realize that it's an incredibly useful tool as long as it's used wisely.

When I commit my code, I always try to include a meaningful message. If I make additional changes but don't want to create another commit, I simply amend the existing commit and push it using the force command.

Example

For example, if I make changes to the coffee.rb file in my app, I can use the following commands to avoid creating another commit:

Supposing you made changes in your coffee.rb file, on your feature branch you can do the following command to avoid adding another commit :

git add coffee.rb
git commit --amend --no-edit
git push -f
Enter fullscreen mode Exit fullscreen mode

Of course do not push force on main or develop.
Most of the time you will be not able or authorize to do it anyway.
Push force is not a bad tool, as all tools you have to learn how to use it the right way. If you do so you will improve your workflow a lot.

By the way if you rebase you already push force a lot.

Tips

If you are not sure where you are on the branch you can use the following git command, this will show you where is your head commit.

git log --oneline --graph
Enter fullscreen mode Exit fullscreen mode

Conclusion

As a junior developer, I still have a lot to learn. I often feel apprehensive about using certain tools or making changes to the codebase. My advice to others in the same boat is to take the plunge and ask more experienced colleagues for their reviews and advice.

Top comments (6)

Collapse
 
spo0q profile image
spO0q

revert is usually a better approach when the "damage" is done, a.k.a. you have pushed your commits to the remote.

Otherwise, use rebase interactive before pushing your work to reorganize your commits and improve the git history.

push --force is only useful in very edge cases. As a junior dev, you'd better not have that habit.

Collapse
 
yet_anotherdev profile image
Lucas Barret • Edited

I see your point, I really need to dig in rebase interactive. But as I said I think all tools are good or bad it is what we do with them.
In my workflow it works well.
Plus it is hard to work with your team with the workflow you give.

Collapse
 
spo0q profile image
spO0q

revert should not be hard to use. It just adds a commit. I don't blame you, as it's pretty common for devs to think the Git History should reflect some kinda of perfection, like "I'm only committing 100% efficient modifications all the time."

To me, it's unrealistic, and the risk is high to mess up with the history when you try to rewrite it.

In the end, you'll do whatever you want, but I've seen so many incidents with what you described that I wanted to share that approach with you.

Cheers.

Thread Thread
 
yet_anotherdev profile image
Lucas Barret

I totally agree, with you. And thanks for your POV. It one of the motivation for me to read article. Having feedback from other (more experienced) people.
And trying to improve my workflow !
Thanks again !
Cheers :D

Collapse
 
jingxue profile image
Jing Xue • Edited

--force-with-lease is much safer than --force. If your push is rejected by --f-w-l, it's almost certain that you don't want to push -f.

Collapse
 
yet_anotherdev profile image
Lucas Barret

Thank you I did not know this one !
I will test it and make an edit if necessary ! :D