Awesome site for learning git

via Johansen Sidabutar: http://think-like-a-git.net/

Advertisements

How to delete a git submodule

I am confused to why the git submodule command is not symmetrical. The command supports adding a submodule, but it doesn’t have a delete submodule command. Why? The argument for “preventing deleting changes” in the stack overflow post is not very convincing. Nevertheless, I found it useful to know how to delete a submodule, so I am just pasting John Douthat / philfreo’s answer here:

———————————————————————–

To remove a submodule you need to:

  1. Delete the relevant line from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit and delete the now untracked submodule files.

How to undo a git push

I issued a github pull request for a non-profit / charity project I was working on. I have already forked his git repo, and I pushed my changes to my fork in github. After looking at how other contributors issue their pull requests (request to merge a branch to jorgej’s master branch), I realised that I issued a pull-request from my master branch. It was actually a bit awkward to develop and make changes on my master branch, it makes it difficult to merge updates from jorgej’s upstream remote master branch.

So what I wanted to do was:

  1. close my initial pull request
  2. create a branch starting from jorgej’s latest commit (e.g. fix_branch)
  3. cherry-pick my commit to that branch
  4. issue a pull request to merge from my ‘fix_branch’ to jorgej’s master branch
  5. reset my master branch (on my github fork) to jorgej’s latest commit
  6. also needed to reset my master branch in my local machine, i.e.: git reset --hard HEAD^
step 1 – 4 was pretty trivial, but I’m not sure how to reset my master branch to a previous state. (Since I have already pushed this commit)
Googling — found this really helpful post. Essentially I can force a git push to point to a certain head. Awesome! I thought this was pretty useful, so I’m just going to paste Charles Bailey’s response here:
——————————————————————————–

You need to make sure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits that you want removed because you are about to rewind history.Then you need to ‘force’ push the old reference.

git push -f origin cc4b63bebb6:alpha-0.3.0 

You may have receive.denyNonFastForwards set on the remote repository. If this is the case, then you will get an error which includes the phrase [remote rejected].

In this scenario, you will have to delete and recreate the branch.

 
git push origin :alpha-0.3.0 
git push origin cc4b63bebb6:refs/heads/alpha-0.3.0

If this doesn’t work – perhaps because you have receive.denyDeletes set, then you have to have direct access to the repository. In the remote repository, you then have to do something like the following plumbing command.

git update-ref refs/heads/alpha-0.3.0 cc4b63bebb6 83c9191dea8