Git


Commit

  • How to revert last commit in the remote?

    git reset HEAD^ --hard

    git push -f

  • How to remove a file added in the most recent unpushed commit?

    • Stage our giant file for removal, but leave it on disk

    git rm --cached giant_file

    • Commit this change

    git commit --amend -CHEAD

    • Push our rewritten, smaller commit

    git push


  • How to remove a file from a Git repository without deleting it from the local filesystem git rm --cached file git rm -r --cached folderName

  • How to “reinit” repository?

# first, remove your initial repository by this code

rm -rf .git

# Then re-initiate git and commit
git init
git add README.md
git commit -m "first commit"
git branch -M main

Submodule

  • How to remove a Git submodule?

    • Delete the relevant section from the .gitmodules file
    • Stage the .gitmodules changes git add .gitmodules
    • Delete the relevant section from .git/config
    • Run git rm --cached path_to_submodule -r (no trailing slash)
    • Run rm -rf .git/modules/path_to_submodule (no trailing slash)
    • Commit git commit -m 'Removed submodule'
    • Delete the now untracked submodule files rm -rf path_to_submodule

Branch

# 1) Add a new branch from your origin/master branch
git branch new-branch origin/master
git checkout new-branch

# 2) Make a commit of your work (e.g. you added your name in the README.md)
git add README.md
git commit -m 'add README' 

# 3) If you had multiple commits, but you want to select a specific commit(e.g. commit id: bbb909f) for a pull request 
git cherry-pick bbb909f

# 4) Push to your new-branch
git push origin new-branch 

Find bugs

git-bisect

Use binary search to find the commit that introduced a bug


Git patch

Reference

  • git add -p

git add partial (or patch)

  • git add -i

Git will ask you which files you would like to partially stage Then, for each section of the selected files, it will display hunks of the file diff and ask if you would like to stage them one by one. Choose type 5 or p (for patch)

  • git reset -p

Reset in patch

  • git commit -p

Combines git add -p and git commit in one command.

Patch mode allows you to stage parts of a changed file, instead of the entire file. This allows you to make concise, well-crafted commits that make for an easier to read history. This feature can improve the quality of the commits. It also makes it easy to remove parts of the changes in a file that were only there for debugging purposes - prior to the commit without having to go back to the editor.

It allows you to see the changes (delta) to the code that you are trying to add, and lets you add them (or not) separately from each other using an interactive prompt. Here’s how to use it:

here’s a visual

At each point, you will be asked whether you want to “stage this hunk”. Here are the commands you can use:


Commonly used commands

  • y - stage this hunk
  • n - do not stage this hunk
  • a - stage this and all the remaining hunks in the file
  • d - do not stage this hunk nor any of the remaining hunks in the file

More advanced commands

  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • j - leave this hunk undecided, see next undecided hunk
  • J - leave this hunk undecided, see next hunk
  • k - leave this hunk undecided, see previous undecided hunk
  • K - leave this hunk undecided, see previous hunk
  • s - split the current hunk into smaller hunks
  • e - manually edit the current hunk
  • ? - print help

Some cool tips from the internet:

  • If git presents you with a chunk larger than what you would like to add, you can use the “e” interactive command to specify the exact lines that you want added or removed. This is probably the most powerful option. As promised, it will open the hunk in a text editor and you can edit it to your hearts content
  • Split the hunk into smaller hunks. This only works if there’s unchanged lines between the changes in the displayed hunk, so this wouldn’t have any effect in the example above

Git ignore locally deleted folder

git ls-files --deleted -z | git update-index --assume-unchanged -z --stdin