Mastering Git: Lesser-Known Tricks for Efficient Version Control

Mastering Git: Lesser-Known Tricks for Efficient Version Control

Welcome Techies 👩🏾‍💻🧑🏾‍💻
If you are new here, please check other blogs on git where I have covered Basic to Advance (simply click on each to navigate to those blogs) and then continue here to know additional tips and tricks that can enhance your Git proficiency.
For the tech-heads 🦸🏾🦸🏾‍♀️, keep the ball rolling 🔥

Introduction

  • Git is a powerful version control system widely used by developers to manage their projects. While many Cloud /Devops Engineers and Developers are familiar with basic Git commands, there are several lesser-known shortcuts and tricks that can greatly enhance productivity.

  • In this article, we'll explore some uncommon Git shortcuts that can help you streamline your workflow and become more efficient.

  • Let's delve deeper into some additional tips and tricks that can enhance your Git proficiency and streamline your development workflow.

Uncommon Git Short Tricks Every one Should Know

  1. Revert a file to its state in a specific commit:

    • Sometimes you may need to revert a file to its state in a previous commit without affecting other files. Git provides a convenient command for this purpose.

    • Run the command:

        git checkout <commit_hash> -- <file_path>
      
        # Replace <commit_hash> with the hash of the commit you want to revert to, and <file_path> with the path to the file you want to revert.
      
  2. Create a new branch and switch to it in one step:

    • Instead of creating a new branch and then switching to it separately, you can do both in a single command.

    • Run the command:

        git checkout -b <branch_name>
      
        #Replace <branch_name> with the name of the new branch you want to create.
      
  3. Stash changes for later:

    • If you need to temporarily save your changes without committing them, you can use Git's stash feature.

    • Run the command:

        git stash save "Description of changes"
      
    • This will stash your changes with an optional description for later retrieval.

  4. Apply only specific stashed changes:

    • When you have multiple stashed changes and want to apply only specific ones, you can use the stash apply command with the stash index.

    • Run the command:

        git stash apply stash@{<index>}
      
        # Replace <index> with the index of the stash you want to apply.
      
  5. Search for a commit by message or content:

    • Git provides powerful tools for searching commits based on commit messages or content changes.

    • To search for a commit by message, run:

        git log --grep="<search_term>"
      
    • To search for a commit by content changes, run:

        git log -S"<search_term>"
      
        # Replace <search_term> with the desired search term.
      
  6. Squash multiple commits into one:

    • If you have multiple commits that you want to combine into a single commit, you can use the interactive rebase feature of Git.

    • Run the following command:

        git rebase -i HEAD~n
      
        # Replace n with the number of commits you want to squash.
      
    • In the interactive rebase editor, change pick to squash or s for all but the first commit.

    • Save and close the editor to combine the commits into one.

  7. Quickly switch between branches:

    • Instead of typing out the full branch name every time you want to switch branches, you can use the shorthand git checkout - to switch to the previous branch.

    • Run the command:

        git checkout -
      
  8. Stage portions of a file:

    • Git allows you to stage specific portions of changes within a file, rather than staging the entire file.

    • Use the git add -p command to interactively stage portions of changes.

    • Git will prompt you with each change and give you options to stage, skip, or split the changes.

  9. Amend the previous commit:

    • If you realize you forgot to include a file or made a mistake in the previous commit, you can easily amend it without creating a new commit.

    • Add the changes you want to include in the previous commit.

    • Run the command:

        git commit --amend --no-edit
      
    • This will add the changes to the previous commit without changing the commit message.

  10. Quickly view the Git log:

    • Instead of typing out git log every time you want to view the commit history, you can use the git lg alias.

    • Set up the alias by running the command:

        git config --global alias.lg "log --oneline --decorate --graph --all"
      
    • Now you can simply run git lg to view a compact and visually appealing Git log.

Conclusion

By incorporating these additional Git tricks into your toolkit, you can further optimize your version control workflow and become more efficient at managing your projects. Experiment with these techniques and discover how they can streamline your development process and improve collaboration within your team.

Congratulations on your Milestone🏆 Now you known more tricks, but don't hold on to yourself share it your tech-buddies. Thanks for reading my Blog till the end, I hope it was helpful. Here is a BONUS to make you smile #DevOps_Memes:

If you like my work, Let's connect and collaborate😃. I am available on the below platforms and very much active there:

Linkedinℹ️
GitHub😻
Blogs👩🏾‍💻
Topmate🏆

If you find the blogs helpful, you can sponsor me too. Letting you know Just in case 😶‍🌫️🤭

NOTE:All my Blogs are written in technical as well as in easy-to-understand language. Please do not copy as this is my original work. In case you want to use it, please tag, mention or ask me. Thanks!!

Did you find this article valuable?

Support Varsha Verma by becoming a sponsor. Any amount is appreciated!