5 Essential Git Commands for Developers

One wrong #Git command can wipe your 𝐭𝐞𝐚𝐦𝐦𝐚𝐭𝐞𝐬 work. Here are 5 essential Git commands every developer should know. If you work with Git regularly, mistakes will happen. A broken feature, a wrong commit or a file that should never have been pushed. Here are 5 Git commands that help recover quickly. 1. 𝐒𝐭𝐨𝐩 𝐭𝐫𝐚𝐜𝐤𝐢𝐧𝐠 𝐚 𝐟𝐢𝐥𝐞 𝐭𝐡𝐚𝐭 𝐰𝐚𝐬 𝐚𝐥𝐫𝐞𝐚𝐝𝐲 𝐩𝐮𝐬𝐡𝐞𝐝 𝐭𝐨 𝐆𝐢𝐭𝐇𝐮𝐛 : Imagine you committed 𝐬𝐜𝐫𝐢𝐩𝐭.𝐣𝐬 and pushed it to #GitHub. Later you decide the file should not be tracked anymore, for example you add it to .𝐠𝐢𝐭𝐢𝐠𝐧𝐨𝐫𝐞 But Git will still track the file because it was already committed. Run : 𝐠𝐢𝐭 𝐫𝐦 --𝐜𝐚𝐜𝐡𝐞𝐝 𝐬𝐜𝐫𝐢𝐩𝐭.𝐣𝐬 This removes the file from Git tracking but keeps it in your local project. 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭: It disappears from GitHub after you commit and push the next change. 𝟐 𝐓𝐞𝐦𝐩𝐨𝐫𝐚𝐫𝐢𝐥𝐲 𝐬𝐚𝐯𝐞 𝐜𝐡𝐚𝐧𝐠𝐞𝐬 𝐰𝐢𝐭𝐡 𝐆𝐢𝐭 𝐒𝐭𝐚𝐬𝐡 : If you made some changes that broke a feature or you want to switch to another branch quickly, you can temporarily save your changes. Run : 𝐠𝐢𝐭 𝐬𝐭𝐚𝐬𝐡 This temporarily removes your local changes and returns your project to the previous clean state. Later you can restore the changes by running Run: 𝐠𝐢𝐭 𝐬𝐭𝐚𝐬𝐡 𝐩𝐨𝐩 𝟑 𝐔𝐧𝐝𝐨 𝐭𝐡𝐞 𝐥𝐚𝐬𝐭 𝐜𝐨𝐦𝐦𝐢𝐭 𝐚𝐧𝐝 𝐫𝐞𝐦𝐨𝐯𝐞 𝐭𝐡𝐞 𝐜𝐡𝐚𝐧𝐠𝐞𝐬 : If you committed locally but have not pushed yet and want to completely remove the commit and the code changes Run : 𝐠𝐢𝐭 𝐫𝐞𝐬𝐞𝐭 --𝐡𝐚𝐫𝐝 𝐇𝐄𝐀𝐃~𝟏 This deletes both the commit and the code changes. You can check the commit history by running: Run: 𝐠𝐢𝐭 𝐥𝐨𝐠 𝟒 𝐔𝐧𝐝𝐨 𝐭𝐡𝐞 𝐜𝐨𝐦𝐦𝐢𝐭 𝐛𝐮𝐭 𝐤𝐞𝐞𝐩 𝐭𝐡𝐞 𝐜𝐨𝐝𝐞 If you want to remove the commit but keep the code changes Run : 𝐠𝐢𝐭 𝐫𝐞𝐬𝐞𝐭 --𝐬𝐨𝐟𝐭 𝐇𝐄𝐀𝐃~𝟏 You committed some changes but want to rewrite or reorganize the commit. 𝟓 𝐑𝐞𝐯𝐞𝐫𝐭 𝐚 𝐜𝐨𝐦𝐦𝐢𝐭 𝐬𝐚𝐟𝐞𝐥𝐲 𝐟𝐨𝐫 𝐬𝐡𝐚𝐫𝐞𝐝 𝐫𝐞𝐩𝐨𝐬𝐢𝐭𝐨𝐫𝐢𝐞𝐬 : If a commit is already pushed and you need to undo it safely Run : 𝐠𝐢𝐭 𝐫𝐞𝐯𝐞𝐫𝐭 <𝐜𝐨𝐦𝐦𝐢𝐭_𝐡𝐚𝐬𝐡> After running this command Git may open a Vim editor in the terminal. Type :𝐰𝐪 This means write and quit, which saves the revert commit message. This creates a new commit that reverses the previous one. 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐧𝐨𝐭𝐞 Some developers try this instead 𝐠𝐢𝐭 𝐫𝐞𝐬𝐞𝐭 --𝐡𝐚𝐫𝐝 𝐇𝐄𝐀𝐃~𝟏 𝐠𝐢𝐭 𝐩𝐮𝐬𝐡 𝐨𝐫𝐢𝐠𝐢𝐧 𝐦𝐚𝐢𝐧 --𝐟𝐨𝐫𝐜𝐞 But this rewrites Git history and can break other developers' work. Use git revert instead when working with shared branches. Git mistakes are normal. Knowing how to recover quickly makes you a more productive developer. #Git #GitTips #DeveloperTips #SoftwareDevelopment #Programming #DevOps #WebDevelopment #Linux #Github

  • graphical user interface
Felipe Castillo

Backend developer | NodeJS | Typescript | Javascript | Express | PostgreSQL

1mo

Git stash -u -m to include untracked files and with a message

To view or add a comment, sign in

Explore content categories