1 minute read

Overview

Notes

Tips

  • How to change the commit a branch is pointing to (adviced by ChatGPT 🥳

    • Option 1: Using `git branch -f`
      
      First, ensure you have checked out the branch you want to modify. Then, use the `git branch -f` command to forcibly move the branch pointer to a new commit.
      
      ```bash
      git checkout <branch_name>
      git branch -f <branch_name> <new_commit>
      ```
      
      Replace `<branch_name>` with your desired branch and `<new_commit>` with the target commit's hash or any other way of specifying the commit (like `HEAD~3`).
      
      Option 2: Using `git reset`
      
      First, check out the branch you want to update, and then reset the branch to the target commit:
      
      ```bash
      git checkout <branch_name>
      git reset --hard <new_commit>
      ```
      
      Replace `<branch_name>` with your desired branch and `<new_commit>` with the target commit's hash or any other way of specifying the commit (like `HEAD~3`).
      
      Note: When using `git reset --hard`, be mindful that it **may discard uncommitted changes** in your working directory.
      
      After updating the branch with one of the previous methods, if you want to push the updated branch to a remote repository, you'll need to force-push using the `--force` option:
      
      ```bash
      git push --force origin <branch_name>
      ```
      
      Keep in mind that force-pushing can cause issues for other collaborators. Make sure you communicate the changes to your entire team to avoid conflicts.

References


Updated: