![]() Let's assume you made the above change in error. Let's make some changes to the third file we created earlier (page3.txt).Įcho "making some changes to page3" > page3.txtĪfter running the above command, run git status. Whether you've staged these changes or not, what matters is that you haven't committed them. The first approach we're going to look at in undoing changes is how to undo changes you've made but not yet committed. ![]() You can use these SHA-1 IDs to travel to any commit whenever you want. Each commit is identified by a SHA-1 hash (before the commit message). Git reflog shows a list of all commits that have been made in this repo, as well as some other key events, like when switching between branches ( checkout). When you run the git reflog command, you should have something like this: To read more about their differences, check out this StackOverflow answer. Either of the two will serve us just fine, however, for the scope of this tutorial. ![]() While these two commands serve a similar purpose (viewing history), there are subtle differences in how they work and the result they show. There are two major ways to view the git history of a repo: git reflog and git log. And that's where the Git history comes in handy. We also need a list of possible places and times we can travel to. To be able to travel back and forth in time, we need a way to know where we are. Touch page3.txt & echo "Content for page3" > page3.txt git add page3.txt git commit -m "create page3" Checking Git History Touch page2.txt & echo "Content for page2" > page2.txt git add page2.txt git commit -m "create page2" Let's add a few more files with separate commits so we can have more commits to work with. Now, we have a repo set up with one file added and one commit in the git history. Mkdir git-undo & cd git-undo git init touch page1.txt & echo "Let us put something here" > page1.txt git add page.txt git commit -m "create page1" Setting Things Upįrom my terminal, I'll run the following commands to create the repo folder, add some files and create some commits to work with: We'll be doing everything from the terminal, so get ready to do some typing. I advise you do the same so you can easily follow along. To make this post practical and relatable, I'll be creating a new git folder to use as a demo. We'll be taking advantage of this timeline to move back and forth as we please. As you know, Git stores snapshots of a repo at different points and then creates a timeline history. So, next time you think you've made a commit in error, you'll have no reason at all to panic. In this post, we'll explore ways to undo changes in Git. For example, maybe you're working on a new feature that isn't ready yet, and then you need to rollback to a previous commit. There are many other valid reasons you'd want to travel in time between different versions of a project. Lucky us! Undoing things in Git isn't just for mistakes. While life might not always present us with a chance to undo our mistakes, Git provides us ample opportunities to do just that. In life, undoing our mistakes is something we've always wished was possible. You can verify by opening the file in VIM or any other editor.Time Travel in Your Project: Undo Changes with Git Git status, again, shows nothing changes. Nothing to commit (working directory myrepository]# vim firstFile.c myrepository]# git checkout myrepository]# git status If the file has unwanted changes, the following will discard the changes. No changes added to commit (use "git add" and/or "git commit myrepository]# ![]() " to discard changes in working directory) Now we will add some unwanted code in firstFile.c. And it should be, as nothing changed yet. Git status shows there are no changes in any file. Nothing to commit (working directory myrepository]# rw-r-r- 1 root root 0 Dec 23 03:04 myrepository]# rw-r-r- 1 root root 0 Dec 23 03:04 secondFile.c Receiving objects: 100% (5/5), 454 bytes, gitundochanges]# ls -lrtĭrwxr-xr-x 3 root root 4096 Dec 23 03:04 gitundochanges]# cd myrepository]# ls -lrt Remote: Total 5 (delta 0), reused 0 (delta 0) ![]() Remote: Compressing objects: 100% (3/3), done. gitundochanges]# git clone empty Git repository in password: When we do not want to stage unwanted changes, we need to discard changes so that in the next commit, the unwanted changes will not go into the local repository.Ĭlone the repository to bring the files to the local machine from the remote. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |