Git Shortcuts to increase the productivity

Git Shortcuts to increase the productivity

  1. git checkout -

If you are switching back and forth different branches multiple times, you would have to write

2. git add -p

Most likely you have used git add . before. That command means that you add every file you changed to the commit. But what if you want to be more selective of what you want to commit? Or what if you want to go one by one of every changes you have made before committing?

For this you have git add -p . The -p stands for “patch”. Using git add -p you don’t add all your changes at once but in small “patches” that you can decide whether you want to add to your commit or not. Git will ask you for every patch whether you want to add or not. You decide by typing y for “yes” or n for “no”.

3. git bisect :-

Searching for the last working commit can be tedious if you have to checkout every single commit. git bisect makes it easy for you to find the last working commit by basically using binary search.

In order to do that, you have to activate git bisect by typing git bisect start . After that you type in git bisect good for a commit that you know is working correctly. Once you have done that you type in git bisect bad for a commit that you know is not working anymore (usually this is your last commit).

When you write git bisect bad Git will checkout an old commit that is in the middle of your history between the first good commit and the last bad commit until it finds the first bad commit.

4. git commit --amend

Sometimes you want to change a commit message that you already pushed or you forgot to add a small commit that is so related to your last commit that you don’t want to create a commit of it’s own. In this case you can use git commit --amend .

Using git commit --amend you can either change the last commit message or you can add an additional change to the last commit. If you want to push your changes, though, you have to use git push -f in either case.

5. git rebase -i HEAD~n

git commit --amend allows you to change the last commit message. What if you want to change a commit message that was made way before that? git rebase -i HEAD~n to your rescue. By typing git rebase -i HEAD~n you can go back to the n th commit and change it by typing edit to the commit you want to edit.

Once you have done and want to save your changes, you have to use git push -f in order to overwrite the changes.


 

To view or add a comment, sign in

More articles by 🧿 🟨Saral Saxena 🧑‍💻🏆

  • Run .http Files in a Intellij idea

    When developing APIs in a Spring Boot project, testing endpoints is a crucial part of the workflow. Tools like Postman…

  • Mastering API Versioning in Spring Boot: Stop Breaking Your Endpoints

    Introduction API versioning is one of the most overlooked yet critical aspects of API development. Many developers…

  • Blazing Fast Performance in Spring Boot 3 with CDS

    Spring Boot 3 brings several performance optimizations, but one of the most exciting ones is Class Data Sharing (CDS)…

  • Java 21 Docker Optimization

    Java 21 introduces significant improvements and features, particularly in containerization. This article explores five…

  • Java Optimization: 7000ms → 90ms

    When working with two separate lists and trying to match data based on a common id, many developers often default to…

  • Spring Boot Red Flags

    Spring Boot is a powerful framework designed to simplify Java application development by offering production-ready…

  • Stop Writing != null

    Null pointer exceptions are a familiar pain point in Java development. The go-to solution for many developers? Add a…

  • Spring boot Apps getting optimized

    Before making any changes, established clear performance baselines. Here’s what our initial metrics looked like:…

  • Validating Payloads with Spring Boot 3.4.0

    First, let’s examine a controller that receives a object. This object contains fields such as: first name, last name…

  • Limitations of Java Executor Framework.

    The Java Executor Framework has inherent limitations that affect its performance in high-throughput, low-latency…

Insights from the community

Others also viewed

Explore topics