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.

fatal: git upload-pack: aborting due to possible repository corruption on the remote side.

When I fetch remote my private git repository via pure SSH, I saw this error and researched some articles, including:

It seems like it is related to a memory issue, and I investigated the remote site to find the oom-killer killing git process. As the remote instance was a small cloud instance with limited memory, I added a swap file.

$ sudo dmesg
...
[165437.371179] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=user.slice,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1001.slice/session-114.scope,task=git,pid=20097,uid=1001
[165437.371214] Out of memory: Killed process 20097 (git) total-vm:1118668kB, anon-rss:83372kB, file-rss:128kB, shmem-rss:0kB, UID:1001 pgtables:872kB oom_score_adj:0

$ sudo swapon /swapfile

References


Updated: