Note: git reset hard discards both changes and files from the working directory. The hard reset command on the HEAD^ removed the latest commit. git commit -m "Reset the HEAD"īefore resetting it. We can use it as follows.Īnd commit them. The hard reset is the most straightforward command to git discard changes already committed. Scenario-2: Git discard changes already committed Example-5: Discarding un-pushed files Untracks the files from the index and dumps the changes in the working directory. Running the mixed reset command git reset HEAD
We can see the stashed changes using git stash showĪnd restore them using git stash apply as follows git stash applyĪnother way to discard the changes in the index is to do a mixed reset on the changes. echo line1 > file1.txt echo line2 > f2.txtĭiscard the changes using the stash command. Let's modify the two files: file1.txt and f2.txt to practice git stash. git/refs/stash file in your working tree, only retaining changes as per the commit HEAD. Before clearing your changes from the index, it records them in the. We can also modify file1.txt and f2.txt then git discard changes on them as follows echo line1 > file1.txtĮxample-3: Use git stash command to stash the changesĪssume you want to git discard changes hoping to restore them later. We can do that by running the command: git restore file1.txt echo line1 > file1.txtĪssume we want to delete the most recent changes on file1.txt. Which has been git's preferred way to discard changes since the introduction of git version 2.23 in 2019. If it fails to remove your recent changes, use the restore command git restore All you do is be at the root of the working directory and run either git checkout -p Apart from switching between branches and commits, you can apply it to git discard changes. The checkout command has several uses in the git workflow.
Let's pick 1: clean to git discard changesĮxample-2: Use either git checkout or git restore command Lastly, we can use the -i flag by picking the clean option. The untracked dir directory got discarded! Similarly, introducing the -d flag dumps untracked files and directories. git add file1.txt f2.txtĬlears the third.txt file from the working directory. Although we can use it with several flags, its most familiar forms are -f for untracked files only, -fd for both untracked files and directories, and -i for interactive file discard.įor instance, let's stage file1.txt and f2.txt, leaving the third.txt file and dir directory untracked. The git clean command is crucial in discarding changes in untracked files. Scenario-1: Git discard uncommitted changes Example-1: Using git clean command Let us git discard changes by applying the setup in the following examples. Both local and remote repos' HEAD point the only commit we have.Ĭreate three text files and a directory with two files as follows touch file1.txt f2.txt third.txt Shows we have one file, whereas logging the history git log I am creating a remote called git_discard_changes with a README.md on GitHub.Ĭopy the repo URL and clone it on the command line or terminal.Ĭd into the new repo git_discard_changes and inspect the repo status as follows. Lab setup to practice git discard changes Let's see their role in discarding git changes. That is the basic explanation of the commit HEAD, uncommitted, and committed changes. Passed the local workflow, you can send the changes to a remote server on a website like GitHub, GitLab, or Bitbucket. Changes that have not passed the index (a.k.a staging area) are called uncommitted, while those with a history are called committed changes. The last commit hash is often referred to as the HEAD. The history tells more about the commit, such as the author, date created, and most importantly, a unique hash called the commit hash. After creating or modifying files in the working directory, git temporarily takes a snapshot of the index changes before storing them in the git database when each commit becomes part of the history. Git workflow entails three stages: working directory, the index, and the history. Git commit HEAD, uncommitted and committed changes explained Besides, it would be best to see practical ways to use the above commands. You probably wonder what we mean by uncommitted, committed, or HEAD.
How to use git revert properly git revert Where N is the number of commits from the HEAD or git reset HEADĪ practical guide to git reset hard vs soft vs mixed git reset -hard Doing a mixed reset to the HEAD at the index discards the changes.