Let's say we have a git repo named myrepo and we have 4 files in it.
% ls -l -rw-r--r-- 1 c2ctechtv staff 0 Aug 15 13:07 script1.sh -rw-r--r-- 1 c2ctechtv staff 0 Aug 15 13:08 script2.sh -rw-r--r-- 1 c2ctechtv staff 0 Aug 15 13:08 script3.sh -rw-r--r-- 1 c2ctechtv staff 0 Aug 15 13:11 script4.sh
Now let us take a look at the commits.
% git log --oneline a7b6be2 (HEAD -> main) added script4 e338436 added two files 6c9e0b7 initial commit
So we have 3 commits.
Now I want to revert just one file which is script2.sh
Steps to revert a single file from Git Repo
Step 1: Identify the commit hash for the file
In our case it is e338436.
Step 2: Revert the File:
git checkout e338436 -- script2.sh
You can review the changes using git status command.
Step 3: Commit the Reverted Changes::
git add script2.sh git commit -m "Revert script2.sh to previous state"
Step 4: Pust changes to Origin:
Finally we push the changes to Origin Server (GitHub/BitBucket/GitLab)
git push origin main
Facing issues? Have Questions? Post them here! I am happy to answer!
- Fix [oh-my-zsh] Cant update: not a git repository
- [fix] fatal: pathspec index.html did not match any files error
- How to ignore files in git using .gitignore file
- Git Fix: fatal: refusing to merge unrelated histories Error
- Install GitHub Command Line Tool on Mac
- How to change directory in Git bash
- Get List of all local branches git command
- fix fatal: --local can only be used inside a git repository error
- [git] fatal: your current branch 'main' does not have any commits yet
- Git Revision Questions Before the Interview
- Change the default git branch name from master to main
- git command to remove/unstage files from staging area
- Remove git config at Local, Global or System Levels?
- [fix] fatal: this operation must be run in a work tree in git
- Clone a particular remote brach using git clone command
- How do I get a list of all branches in Git?
- How to check your installed version of Git
- Step-by-Step: How to delete a git branch from local as well as remote origin
- Git Commit - Author identity unknown, Please tell me who you are email
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to Rename a Git Local Branch Using the --move Option
- Perform an Empty Commit in Git without anything in Staging Area
- [Fixed] Git Clone firstname.lastname@example.org: Permission denied (publickey) Could not read from remote repository Fatal error
- GitHub: How to Search Code
- Rename git branch on Local and GitHub Remove using Command
- How to install SpaCy (NLP Library) on Mac - Python
- fix fatal: --local can only be used inside a git repository error - Git
- Check Bluetooth is turned on or off on Android device programmatically [Java Code] - Android
- Center align text in TextView Android Programming - Android
- [Android] RuntimeException: Unable to start activity Need BLUETOOTH permission - Android
- Fix - Error:Invalid Gradle JDK configuration found (Android Studio) - Gradle
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- The Date Command and its usage [Linux - Unix - macOS] - Linux