COMMITID=$(git rev-list HEAD | head -3 | tail -1)Īt this point you’re prompted to decide what to do with the previous commits before continuing. Then you use the git rebase command with the -i (interactive) flag to ‘drop’ the relevant commits from your branch. As before, you first get the relevant commit ID. This time we’re going to use git rebase to go back to ‘Change 6’. Now your HEAD pointer and master branch are pointed at the change you wanted. Now check that that change is the one you want: git show $" Let’s say we want to reverse the last-but- two commit on our branch (ie the one that added ‘ Change 9‘ to the file).įirst we use the git rev-list command to list the previous changes in reverse order, and capture the commit ID we want to the LASTBUTONE variable using pipes to head and tail: COMMITID=$(git rev-list HEAD | head -3 | tail -1) Sometimes the manual approach is not easy to achieve, or you want to revert a specific commit (ie not the previous one on your branch). Next, you apply the changes by hand, or you can run this command (which effectively removes the last line of the file) to achieve the same result in this particular context only: head -9 file > file2Īnd then commit the change: git commit -am 'Revert C10 manually' If you haven’t done any funny business on your repo then it points to the last commit on the branch you are working on. The HEAD tag always points to a specific commit. The output should show you a diff of what changed in the last entry. The ‘ git show‘ command can give you this: git show HEAD The simplest way (conceptually, at least) to undo a change is to add a new commit that just reverses the change you last made.įirst, you need to see exactly what the change was. See here if you want to know more about this git log command. If you run: git log -oneline -graph -allĪt any point in this walkthrough you should be able to see what’s going on. Now you are in a fresh folder with a git repository that has 10 simple changes to a single file (called ‘ file') in it. If you want to follow along, run these commands to set up a simple series of changes to a git repository: cd $(mktemp -d) There’s many ways to achieve the result you might want, so this can be a pretty instructive and fertile question to answer. This got me thinking, and I came up with a walkthrough similar to the ones I use in my book to help embed key Git concepts and principles. git/objects directory is not reduced in size.Recently, while showing someone at work a useful Git ‘trick’, I was asked “how many ways are there to undo a bad change in Git?”. I have run this without obvious errors and then cloned the repo to find that the. Git commit -m 'Removing $1 from git history' Git for-each-ref -format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d Git filter-branch -tree-filter 'rm -rf $1' -prune-empty HEAD The link above has a long discussion of a procedure that I've put to this script : #!/bin/bashĮcho "missing argument: subdirectory to remove" I want to remove the massive subdirectory. The checkouts/clones take a long time and I believe this is because of the large. The massive subdirectory is no longer in the download but the objects directory is enormous. This repository had a large subdirectory of large files that months ago I removed from the commit and that I now want to remove from the repo forever.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |