and afterwards, you'll see the branch in the exact state it was 5 minutes ago: Ahhhh, good ol' un-touched branches! Simply pick the commit before our destructive changes, do a hard reset to the gitrevision number (the one with the curly braces), and everything is reset to the way it was before the rebase! Git reflog shows everywhere our current working HEAD was at, including commits that were removed with destructive commands such as git reset and git rebase. Instead, try running git reflog to see some helpful points in history: We could manually create a new branch off the old commit and re-add/delete the files that were there, but what a hassle! How do we go back? We could delete everything and re-clone, but we probably don't have a remote copy of our most recent changes to clone. and all our my-new-branch files are combined with the master files:Īpparently, our original commit ( 6cde040) is gone. Either way, we want things to be the way they were before, but here is what our new git log shows: Maybe there were some weird merge conflicts during the rebase and everything got messy. Let's say we're on my-new-branch and we rebase onto master:īut Agh! We realize this was a mistake! Maybe the code stopped working after the rebase. When we mess it up in the examples below, our goal is to bring the repository back to this state. This branch also has the first two files, but then splits off from master and has its own third file.Īnd here are all the files in my-new-branch: There is also a separate my-new-branch branch. There is a master branch with 3 files, each added in a separate commit. Here's an overview of the starting git history (using the command git log -oneline -graph -all): & git commit -m "add file3" git checkout my-new-branch Examples & git commit -m "add newBranchFile" git checkout masterĮcho "file 3 content" > file3.txt & git add. & git commit -m "add file2" git checkout -b my-new-branchĮcho "new branch file content" > newBranchFile.txt & git add. & git commit -m "add file1" echo "file 2 content" > file2.txt & git add. Mkdir reflog-demo & cd reflog-demo & git initĮcho "file 1 content" > file1.txt & git add. But if you want to follow along with the examples on your own terminal, run each line below as a command in your terminal to set up the demo repository: If you only want to read the examples, you can skip to the next section. In this article, I'll show you how to use these nifty shortcuts to undo some of those history-changing moves and get back to the proper timeline! Setup I used to do this all the time!īut what if I told you there was a better way? What if you could time travel back to a point before you did those destructive changes without deleting anything? It turns out, git has built-in tools for this such as reflog, ORIG_HEAD, and gitrevisions, and they're surprisingly easy to use! Then, since your changes rewrote history, the only way to go back is to delete everything and re-clone the repository to start over again. We've all been there! It's easy to do a well-meaning git rebase or git reset only to find everything broken afterwards. Me: “Sometimes I fuck up my rebase so badly I delete the repo and clone a new copy □□♀️”- Emma Bostian □ February 25, 2020 Interviewer: “What’s your biggest weakness?”
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |