![]() We can run git reset HEAD README.md to unstage the README.md file. The command above will fetch the version of the README.md file in the parent commit of the current commit and add it to the index for the next commit. We can update the staged snapshot of a file to match the version of the specified commit, as shown below. The effects of the commands are limited to a single file. The git checkout and the git reset commands accept a file path as an optional parameter. This way, we avoid re-writing the commit history of shared repositories.įor example, if we want to revert the most recent commit, we will run: $ git revert HEAD When we revert commits, we create a new commit that reverses the effects of the specified commit. Always create a new branch when you want to commit changes while in the detached HEAD state. ![]() The command above will switch us into the detached HEAD mode because our current HEAD does not have a branch reference.Īll changes and commits done in detached HEAD mode will not be reachable once you switch to another branch. Passing HEAD~2 will check out the grandparent. The command above will check out the parent of our current commit. We can also inspect arbitrary commits by passing a commit hash or reference instead of a branch name. The command above will switch to the feature branch. We can also pass branch names to switch between branches. ![]() We can use the git checkout command to inspect a repository’s state at a specified commit. -hard - Alters both the working directory and staged snapshot to match the specified commit.-mixed - Does not affect the working directory but alters the staged snapshot to match the specified commit.-soft - Does not change the staged changes or the working directory.We can also use the git reset command to change the staged snapshot and working directory by passing one of the following flags. It is best to use the command to do away with commits that we have not published to a remote shared repository. We can refer to them as dangling or orphaned commits. The command above will move the tip of our branch backward by three commits. We can use this command to delete commits from a branch. Resetting on the commit level will move the HEAD ref to a specified commit. If we fail to include a file parameter, the commands act on the commit as a whole. We pass parameters to the git reset and git checkout commands to invoke different levels of operation. git revert File level (N/A) Commit Level Operations git revert Commit level Reverse the effects of a commit in public branches. git checkout File level Throw away changes in the working directory. git checkout Commit level Inspect old commits and switch between branches. git reset File level Unstage a file from the index. Command Scope Common Use Cases git reset Commit level Delete commits in a branch locally or discard uncommitted changes. The table below gives some common use cases for the three commands. They both modify the history of a repository and can lead to conflicts when pushing to the remote public or shared repositories. We normally use the git reset and the git checkout to undo local or private changes. We can reset at three different modes corresponding to the three trees. When resetting, we take a specific commit, reset the Three Trees, and update a repository to match the state of the repo at the specified commit. The git revert commit only takes effect at the commit level and does not have file-level functionality. When reverting, we take a specific commit and create a new one that reverses the specified commit’s effect. Checking out at the file level will update the file’s contents with the contents of the specified commit. We can use the git checkout command at the commit and file level. We can move our HEAD ref to any commit using the git checkout command.įor example, to move our HEAD ref to the commit b, we will run: $ git checkout -b The HEAD and main branch pointers are currently at the commit d. The image above shows a sequence of commits in a Git repository. This operation relocates the HEAD pointer to a specified commit. ![]() We can also call the above components the Three Trees. It will be easier to understand these commands if we are clear on the effect of each command on the three main components of a Git repository. Difference Between the Git Reset, Git Revert, and Git Checkout Commands It is easy to mix up the commands, but by the end of this article, you will have gained the confidence to use and navigate your repository with the above commands. These are some of the most useful Git utilities that allow us to undo some changes in our repositories. This article discusses the difference between the git reset, git revert, and git checkout commands. Difference Between the Git Reset, Git Revert, and Git Checkout Commands. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |