AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Git cleanup1/13/2024 ![]() □ HEAD~ refers to the nth parent of your HEAD. Now you can selectively add files to the staging area and create meaningful commits to clean up your commit history. Running git status will now show Changes not staged for commit message. In other words, all the changes you have committed since then will be unstaged. Doing so, Git will move your branch to that commit but also modifies your index area to match HEAD. Git runs this command with -mixed option by default. But if you wish to create multiple commits starting from old-commit, you can also run git reset. I normally use -soft reset variant when I feel like squashing old commits into a single commit. Now, you can go ahead and commit those changes with a new message □. Running git status will now show 'Changes to be committed' message. It essentially undid all the git commit commands. And everything you have committed so far since this commit (old-commit) will be shown in your staging area. It won't modify the index or working directory. And as we learned, Git will start by moving the branch pointer to it and stops right there. ![]() If you have been lazily writing multiple vague commits, you can use git reset -soft to make your branch point to that old commit. With the foundation set aside, let's see how can you use the -soft and -mixed(also a default) options with git reset to clean up your commit history. Make the working directory looks like the index ( -hard).Modify the index(staging area) to look like HEAD ( stop here if -mixed).Move the branch HEAD point to ( stop here if -soft).It runs the following operations in order: Irrespective of which one you chose, running git reset will always start by moving the branch HEAD points to, and stops where you ask it to. Git reset has 3 flavours at your disposal: - soft, - mixed, and - hard. □ git reset moves the branch HEAD is pointing to git checkout moves the HEAD to that branch. For example, if you are on master branch, running git reset 8fgh5 will move the branch to this commit as shown in the picture below: Now when you run git reset, it moves the branch HEAD is pointing to. When Git moves the HEAD, it also modifies the index and working directory to reflect the changes of that commit. The branch is just a lightweight movable pointer to one of the commits you are on, whereas, HEAD by default points to the branch you are currently on. HEAD and branch are two different concepts in Git. If you wish to skip the basics, you can jump straight to the cleanup section too. In this article, I will show you how you can smartly clean up your commit history using git reset But before I throw in that tip, I would also like to unveil the reasoning behind it so that it can be more fruitful in a long run. Therefore, It's a good habit to keep your commit history clean and Git has a few tools to achieve this. Now, you can selectively stage those files and can create multiple new commits however you want.Ī commit message should justify your intent for the change, and not just add unnecessary noise to the Git history. With -mixed(also a default) flag, Git will match your staging area with the content snapshot at that commit, which means all those changes since old commit have been unstaged too. You can then create a single commit to update your commit history. And now when you run git status, you will see all the changes you have made since commit-hash in your staging area. Running git reset -soft will move the branch to this old commit.
0 Comments
Read More
Leave a Reply. |