Do you need to redo all of your work, or copy and paste between the two branches? Nope, thankfully there’s a way to do it. We’ve all done it: Somehow, a single file was left in the wrong branch. # List branches along with commit ID, commit message and remote If you do it twice ( vv) then it will also show the upstream remote branch that it is linked to. If you are looking for a way to see what the last commit in each branch was, you can use option flags v to show a list of all branches with the last commit ID and message from each. Git checkout Information About All Branches # Checkout branch N number of checkouts ago So if, for example, you had checked out branch feature/thing-a then feature/thing-b then bugfix/thing-c, you can use to get back to feature/thing-a. The - acts as a shorthand for which is a syntax you can use for going back any given amount of checkouts. It turns out there is a way to do this with ease: # Checkout previous branch But what if it wasn’t a branch, if it was a “detached HEAD”, a specific commit. What can you do if you didn’t commit, switched branch then wanted to get back to it? You could probably work out frorm the branch list anyway, if you’ve some idea of the branch name. It’s not fancy or surprising but it has helped me many times. This outputs every single branch you’ve got locally beginning with the one most recently committed to. My first attempt at figuring out how to find lost work, in a short blog post titled “ How to find the branch you lost in git” was to sort the branches by date. Was it ever committed? Maybe it was stashed? Maybe it wasn’t committed and now the work is in the wrong branch and everything is going awful and I am awful at my job! We’ve all been there. However, I’m sure you can agree, there are often times when you’ve context switched and when you go back it’s impossible to find the right branch. We know life is easier, to some extent, with our updates neatly compartmentalised into git branches that can be shared with other team members. It would be awful to have to throw out v2 just because there was a bug in v1, it would be equally a shame to have files named like v1_final_bug_fixed which notoriously become an impossible mess. It could be to backup the code for the future, or to be able to make progress on a few different features asynchronously. The whole point of git is to be able to save your work, to switch context and do something else. In this article, we will specifically target the stuff that just makes your life better in a small way. There’s been a lot written about getting started with git, understanding how git works under the hood or techniques for better branching strategies. It is undoubtedly an important tool that many of us use on a daily basis, and yet it is often seen as magic: brilliant, but scary. In that time it has gone from underdog to unbeaten champion, git init is often the first command run on a new project. If you use Git every day and feel like it’s a juggling act, then here are some tricks and tips to help make your life a bit easier. You don’t need to know your trees from your dangling blobs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |