How do I undo the most recent local commits in Git?
2022-11-09
Question I accidentally committed the wrong files to Git, but didn't push the commit to the server yet. How do I undo those commits from the local repository? Answer Undo a commit & redo $ git commit -m "Something terribly misguided" # (0: Your Accident) $ git reset HEAD~ # (1) [ edit files as necessary ] # (2) $ git add . # (3) $ git commit -c ORIG_HEAD # (4) git reset is the command responsible for the undo.…
How to search a Git repository by commit message?
2022-11-09
Question I checked some source code into GIT with the commit message "Build 0051". However, I can't seem to find that source code any more - how do I extract this source from the GIT repository, using the command line? Update Checked in versions 0043, 0044, 0045 and 0046 using SmartGIT. Checked out 0043, and checked in versions up to 0051 on a different branch. Checked out 0043 again. Now, 0051 has disappeared.…
Error with renamed repo in GitHub: "remote: This repository moved. Please use the new location"
2022-11-08
Question I am receiving this notice when I push updates from my local instance to remote master on GitHub: remote: This repository moved. Please use the new location [new location] Is there a way to fix this? Answer The simple way is: git remote set-url origin [updated link url https://........git] Alternatively, if you like the long way it is: git remote rm origin git remote add origin [updated link] Changing a remote's URL GitHub documentation goes into further detail.…
How to find the commit in which a given file was added?
2022-11-08
Question Say I have a file foo.js that was committed some time ago. I would like to simply find the commit where this file was first added. After reading the answers and my own tinkering, this works for me git log --follow --diff-filter=A --find-renames=40% foo.js Answer Here's simpler, "pure Git" way to do it, with no pipeline needed: git log --diff-filter=A -- foo.js Check the documentation. You can do the same thing for Deleted, Modified, etc.…
Remove specific commit
2022-11-07
Question I was working with a friend on a project, and he edited a bunch of files that shouldn't have been edited. Somehow I merged his work into mine, either when I pulled it, or when I tried to just pick the specific files out that I wanted. I've been looking and playing for a long time, trying to figure out how to remove the commits that contain the edits to those files, it seems to be a toss up between revert and rebase, and there are no straightforward examples, and the docs assume I know more than I do.…
Git - fatal: Unable to create '/path/my_project/.git/index.lock': File exists
2022-11-06
Question I am still getting this error message, when I try to move my project tree on to git repo. I checked the permissions of my directory with this project and these are set on 777. In terminal in the directory with my_project I set: git init and then if I try git add . or git commit -m "first upload" so I'll get the error fatal: Unable to create '/path/my_proj/.…
What are the differences between git branch, fork, fetch, merge, rebase and clone?
2022-11-05
Question I want to understand the difference between a branch, a fork and a clone in Git? Similarly, what does it mean when I do a git fetch as opposed to a git pull? Also, what does rebase mean in comparison to merge? How can I squash individual commits themselves together? How are they used, why are they used and what do they represent? How does GitHub figure in? Answer Git This answer includes GitHub as many folks have asked about that too.…