How do I undo 'git reset'?
2022-11-02
Question How do I undo this command? git reset HEAD~ Answer Short answer: git reset 'HEAD@{1}' Long answer: Git keeps a log of all ref updates (e.g., checkout, reset, commit, merge). You can view it by typing: git reflog Somewhere in this list is the commit that you lost. Let's say you just typed git reset HEAD~ and want to undo it. My reflog looks like this: $ git reflog 3f6db14 HEAD@{0}: HEAD~: updating HEAD d27924e HEAD@{1}: checkout: moving from d27924e0fe16776f0d0f1ee2933a0334a4787b4c [.…
How to get back to the latest commit after checking out a previous commit?
2022-11-02
Question I sometimes check out some previous version of the code to examine or test. I have seen instructions on what to do if I wish to modify previous commits -- but suppose I make no changes. After I've done e.g. git checkout HEAD^, how do I get back to the tip of the branch?.. git log no longer shows me the SHA of the latest commit. Answer If you know the commit you want to return to is the head of some branch, or is tagged, then you can just…
Push local Git repo to new remote including all branches and tags
2022-11-01
Question I have a local Git repo that I would like to push to a new remote repo (brand new repo set up on Beanstalk, if that matters). My local repo has a few branches and tags, and I would like to keep all of my history. It looks like I basically just need to do a git push, but that only uploads the master branch. How do I push everything so I get a full replica of my local repo on the remote?…
Can you issue pull requests from the command line on GitHub?
2022-10-31
Question It seems like you have to interact with github.com to initiate a pull request. Is this so? Answer UPDATE: The hub command is now an official github project and also supports creating pull requests ORIGINAL: Seems like a particularly useful thing to add to the hub command: http://github.com/defunkt/hub or the github gem: http://github.com/defunkt/github-gem I suggest filing an issue with those projects asking for it. The github guys are pretty responsive.…
.gitignore all the .DS_Store files in every folder and subfolder
2022-10-30
Question I've added .DS_Store to the .gitignore file, but it seems that it is only ignoring .DS_Store in the root directory, not in every folder and subfolder. How do I fix this? Answer I think the problem you're having is that in some earlier commit, you've accidentally added .DS_Store files to the repository. Of course, once a file is tracked in your repository, it will continue to be tracked even if it matches an entry in an applicable .…
Git: How to squash all commits on branch
2022-10-30
Question I make new branch from master with: git checkout -b testbranch I make 20 commits into it. Now I want to squash those 20 commits. I do that with: git rebase -i HEAD~20 What about if I don't know how many commits? Is there any way to do something like: git rebase -i all on this branch Answer Another way to squash all your commits is to reset the index to master:…
git: Your branch is ahead by X commits
2022-10-30
Question How does this actually come about? I am working in one repo by myself at the moment, so this is my workflow: Change files Commit Repeat 1-2 until satisfied Push to master Then when I do a git status it tells me that my branch is ahead by X commits (presumably the same number of commits that I have made). Is it because when you push the code it doesn't actually update your locally cached files (in the .…