It's been a while since I last posted about Git, but I wanted to share some insights from my earlier experiences. In my post on committing to no branch, I tackled the frustrating "detached HEAD" state that can occur, especially when working with submodules. I provided a straightforward solution to help others avoid the confusion I faced.
Additionally, I discussed the common error of "failed to push some refs" in my article on pushing changes, where a simple git pull
can save the day. I also compared Git clone and Git submodule, sharing tips on managing submodules effectively. I hope these posts can help you navigate Git more smoothly!
git: what to do if you commit to no branch
Using git, you'll sometimes find that you're not on any branch. This usually happens when you're using a submodule inside another project. Sometimes you'll make some changes to this submodule, commit them and then try to push them up to a remote repository:
Er, what? Everything is not up to date - I just made changes! The clue is in the first part of the commit response - [detached HEAD d2bdb98]. This just means that we've made a commit without actually being on any branch.
Luckily, this is easy to solve - all we need to do is checkout the branch we should have been on and merge in that commit SHA:
When Git tells you it failed to push some refs
I received an unhelpful error while trying to push to a repository on Github today:
In case you ever have the same problem, all you have to do is a quick git pull first, then you can carry on as normal. Easy when you know how...
Git clone vs Git submodule
Having recently made the switch from svn to git, I wanted to achieve what svn externals did (and what Piston did better). Turns out this is pretty simple, for example to get rails on edge:
A couple of other default submodules you'll want:
What submodule does is to check out the submodules as their own repositories, so they are tracked independently of the repository you made them submodules of. The submodules you have are tracked in the .gitmodules file, which might look something like this: