This effectively replaces the last commit with your "edited" version, correcting the wrong author information. In case you want to change just the very last commit, Git offers a very easy way to do this: git commit -amend -author="John Doe " There are three basic ways to edit your past commits: Using -amend for the Very Last Commit Therefore, think twice before you rewrite your commit history! This is nothing to take lightly: you will create new commit objects in this process, which can become a serious problem for your collaborators - because they might have already based new work on some of the original commits. No matter how exactly we change the information of past commits, there's one thing to always keep in mind: if we do this, we are effectively rewriting commit history. Usually I don't care to keep such information, so I wipe out the default message and use something more self-explanatory like Implemented feature Z.Note Editing Past Commits Rewrites History! You can leave it as it is and the commit message will result in a list of all the intermediate commits, as follows: Prepare the workbench for feature Z It's now time to give it a name: your editor pops up again with a default message, made of the names of all the commits you have squashed. You have just told Git to combine all seven commits into the first commit in the list. S 871adf OK, feature Z is fully implemented - newer commit The result would be: pick d94e78 Prepare the workbench for feature Z - older commit You mark a commit as squashable by changing the word pick into squash next to it (or s for brevity, as stated in the comments). Our task here is to mark all the commits as squashable, except the first/older one: it will be used as a starting point. You can do many smart tricks during an interactive rebase, let's stick with the basics for now though. Pick 871adf OK, feature Z is fully implemented - newer commitīelow the commit list there is a short comment (omitted in my example) which outlines all the operations available. pick d94e78 Prepare the workbench for feature Z - older commit I've added - older commit and - newer commit to make it clear, you won't find those notes in the editor. Note that it might be confusing at first, since they are displayed in a reverse order, where the older commit is on top. Way easier, isn't it? Step 2: picking and squashingĪt this point your editor of choice will pop up, showing the list of commits you want to merge. So in my example the command would be: git rebase -interactive 6394dc Where is the hash of the commit just before the first one you want to rewrite from. Luckily, there is another way: git rebase -interactive I have tons of commits to squash, do I have to count them one by one?Ī downside of the git rebase -interactive HEAD~ command is that you have to guess the exact number of commits, by counting them one by one. So in this case the command would be: git rebase -interactive HEAD~īecause I want to combine the last seven commits into one, and d94e78 Prepare the workbench for feature Z is the seventh one. Notice how a rebase generates a new commit with a new hash ( 84d1f8 in the example above). Obtaining: 84d1f8 Feature Z - newer commit (result of rebase) For example, this is a hypothetical list of commits taken from the git log command, while I'm working on a generic feature Z: 871adf OK, feature Z is fully implemented - newer commitĭ94e78 Prepare the workbench for feature ZĪnd this is what I would like to do: 871adf OK, feature Z is fully implemented - newer commit -┐Ħ43d0e Code cleanup |- Join these into oneĭ94e78 Prepare the workbench for feature Z -┘ Where N is the number of commits you want to join, starting from the most recent one. The first thing to do is to invoke git to start an interactive rebase session: git rebase -interactive HEAD~N It's a handy tool I use quite often I usually tidy up my working space by grouping together several small intermediate commits into a single lump to push upstream. In Git you can merge several commits into one with the powerful interactive rebase.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |