How to find the nearest parent of a Git branch
2023-02-04
Question Let's say I have the following local repository with a commit tree like this: master --> a \ \ develop c --> d \ \ feature f --> g --> h master is my this is the latest stable release code, develop is my this is the 'next' release code, and feature is a new feature being prepared for develop. Using hooks, I want to be able to refuse pushes to feature to my remote repository, unless commit f is a direct descendant of develop HEAD.…
How to find the Git commit that introduced a string in any branch?
2023-02-03
Question I want to be able to find a certain string which was introduced in any commit in any branch, how can I do that? I found something (that I modified for Win32), but git whatchanged doesn't seem to be looking into the different branches (ignore the py3k chunk, it's just a msys/win line feed fix) git whatchanged -- <file> | \ grep "^commit " | \ python -c "exec(\"import sys,msvcrt,os\nmsvcrt.…
git ignore exception
2023-02-02
Question I have a gitignore file that makes git ignore *.dll files, and that is actually the behavior I want. However, if I want an exception ( i.e. to be able to commit foo.dll), how can I achieve this? Answer Use: *.dll #Exclude all dlls !foo.dll #Except for foo.dll From gitignore: An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again.…
How to abort a stash pop?
2023-02-02
Question I popped a stash and there was a merge conflict. Unlike the question that is listed as a duplicate, I already had some uncommitted changes in the directory which I wanted to keep. I don't just want to make the merge conflict disappear, but also to get my directory back to the state it was before the pop. I tried git merge --abort, but git claimed no merge was in progress.…
How to undo "git commit --amend" done instead of "git commit"
2023-02-02
Question I accidentally amended my previous commit. The commit should have been separate to keep history of the changes I made to a particular file. Is there a way to undo that last commit? If I do something like git reset --hard HEAD^, the first commit also is undone. (I have not yet pushed to any remote directories) Answer What you need to do is to create a new commit with the same details as the current HEAD commit, but with the parent as the previous version of HEAD.…
What's the purpose of git-mv?
2023-02-02
Question From what I understand, Git doesn't really need to track file rename/move/copy operations, so what's the real purpose of git mv? The man page isn't particularly descriptive... Is it obsolete? Is it an internal command, not meant to be used by regular users? Answer git mv oldname newname is just shorthand for: mv oldname newname git add newname git rm oldname i.e. it updates the index for both old and new paths automatically.…
git ahead/behind info between master and branch?
2023-02-01
Question I have created a branch for testing in my local repo (test-branch) which I pushed to Github. If I go to my Github account and select this test-branch it shows the info: This branch is 1 commit ahead and 2 commits behind master My questions are: How can I display this info locally (ie: a command that shows this on the terminal, rather than having to open Github to see it)?…