difference between git --git-dir checkout and git checkout - git-checkout

git checkout acts differently for using option --git-dir.
Commands :
inside_gitdir$ git checkout remote/branch
outside_gitdir$ git --git-dir=/path/.git checkout remote/branch
Here command 1 works well and checking out. but the command 2 shows to move files like :
error: The following untracked working tree files would be overwritten by checkout:
someFiles
Please move or remove them before you can switch branches.
Aborting
I thought both the commands are running in same way. But,
Why am i getting this error in second command not in first ?
What is the difference of using --git-dir option ?

I ran into the same issue.
It seems to work if you do: git --git-dir=/path/.git --work-tree=/path/ checkout remote/branch

Related

Git for Windows "No tags file" Response from "git diff" Command

Git version: 2.14.2.2
Whenever I run git diff on a repository I am greeted with the response No tags file. I have tried running the command on multiple repositories, multiple consoles (Cmd, PowerShell, MINGW64, Visual Studio Command Prompt) and all have the same response.
Strangely, the git log command also fails. Many other commands work, however, such as git status, git pull, etc. It seems to be only log and diff.
I have uninstalled Git entirely and reinstalled. Restarted my system. Tried referencing the git.exe directly (which yields the exact same response). Nothing is working and I have not seen this error anywhere else. I compared my user configs with those of a colleague and they are identical.
Some portion of the command executes properly, because if I supply two commit hashes, and I intentionally break one, the response I receive is:
It seems like another program may be hijacking the git diff command. I believe this because I'm not sure "No tags file" is even a possible Git response. Not sure what else it would be.
To make things even more confusing- my ultimate goal is to run the git diff within the context of an msbuild and it DOES EXECUTE CORRECTLY. Now, I could be satisfied with this, but I need to modify the diff command slightly, and running a full build each time is not productive, nor easy to troubleshoot. There is a task within the build script that runs an Exec command and it has no issues performing the diff. I'm also able to execute a Diff Against Current within SourceTree, which to the best of my knowledge, runs a git diff behind the scenes.
Any help would be very much appreciated.
:: Edits ::
Various commands:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
Output:
Every one of the commands above returns the same No tags file response, in all of my repos.
Cat Head:
cd .git
cat HEAD
ls -R refs
Output:
New Repo:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
Output:
git config -e:
git config --global -e:
::Edits 2::
I uninstalled all of my diffing/source control tools (SourceTree, Git, SVN, WinMerge, KDiff). Installed the portable version of Git. Opened CMD to a repo, put in full path to the git.exe portable and it still returned the No tags file response.
I also reviewed all of my path variables for: git, vim, ming, mintty and anything else that seemed suspect, but didn't find any.
I have restarted after performing all steps, and yet the problem persists.
::Edits 3::
I have a different user on my laptop, switched to that user and the git diff works properly, so clearly there is something with my main user that is conflicting. Will continue to look into my User directory for issues.
Here are the steps I'd take in this situation:
Try the following and check the response:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
Try the same with log:
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
I'm actually guessing that your refs are messed up, which means that the direct hashes might work, but the HEAD and/or master one may not.
Look into the .git/refs folder
From the main repo folder:
cd .git
cat HEAD
ls -R refs
Hopefully, HEAD is pointing to a branch, and if master is checked out, cat HEAD output should look like:
ref: refs/heads/master
Then, the ls -R refs, should show a heads folder, with files for each of your local branches (i.e. master and possibly others). You also likely have refs/remotes and refs/tags directories.
If any of these things are radically different or missing, that could be your issue...
Since you have reinstalled git, create a brand new repo and try the same commands:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
If this last one works, then git is likely working okay, but some tool you have is messing things up.
Post your config from git config -e and git config --global -e - maybe we can see something?
When googling for the "No tags file" message, the first results I get all talk about vi.
I do not understand why git would try to execute vi when running git diff or git log, could it be that your system is configured to use vi as a pager ?
# some possible places which could influence that :
echo $PAGER
echo $GIT_PAGER
git config --get core.pager
When digging in the documentation for less, I found that less can use a ctags file, to spot "the file that contains this tag".
So you can also look at the list of variables that influence the behavior of less :
# from bash :
# env will list all the defined environment variables
env
# the ones that impact 'less' should contain "LESS" in their names :
env | grep LESS

Windows custom git commands

Say I want a new git command, git new, that makes a new branch that is up to date with origin/master.
Is there a way I can make this script and have it available in all repositories on Windows from powershell?
edit: To clarify I want a git script not a powershell function. The only reason I mentioned powershell is because I don't use git bash.
Create a batch file that contains the following commands:
git branch %1 origin/master
git checkout %1
Save it, let's say, as C:\Scripts\new-branch.cmd. (I never worked with PowerShell, I don't know its rules. However, it should work as well using the old Windows Command Prompt).
Test the batch file works as expected by running:
C:\Scripts\new-branch.cmd test1
It should output something along these lines:
Branch test1 set up to track remote branch master from origin by rebasing.
Switched to branch 'test1'
Your branch is up-to-date with 'origin/master'.
If you don't need the new branch to track the remote branch then you just add --no-track to the git branch command.
If everything goes well then run:
git config --global alias.new "!C:/Scripts/new-branch.cmd"
This makes the Git alias new available to your Windows profile in all repositories. If you need it only in one repository then remove --global and run the command when the current directory is in the repository where you need it.
Use it as:
git new test2
You can use a git alias which uses git checkout:
git config --global alias.new 'checkout origin/master -b'
This would then be used as git new new_branch.
(Which is equivolent to git checkout origin/master -b new_branch
See the git docs for checkout. I tried the command and it worked, but when I looked at the docs, I didn't find a syntax that exactly matched my form. (Closest is git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>])
Note: #axiac, I may have used the !git because it doesn't hurt, and I may have needed to do multiple commands to solve the problem, and didn't remove it when I was done.

How to call Git Bash from Windows and preserve autocrlf

I have automated python scripts for git versioning system. It works on linux, but I have some problems on windows. I will go directly to the core of the problem...
There are sources ended by LF (not CRLF) and Windows Git Bash can handle it perfectly (checkout as Windows style and commit as unix style).
When I open Git Bash and type "git status" I get
your branch is up-to-date.....nothing to commit, working directory clean
When I open cmd.exe and I write "git status"
....
modified: example.h
modified: example.cpp
....
the modification is just because cmd.exe does not handle autocrlf. the monification is just in line endings.
I tried a workaround:
c:\myrepo>echo git status|"C:\Program Files (x86)\Git\bin\sh.exe" --login -i
output:
Welcome to Git (version 1.9.2-preview20140411)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
xxx#yy /c/myrepo (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
xxx#yy /c/myrepo (master)
$
xxx#yy4 /c/myrepo (master)
$ logout
This almost solves my issue, but there is an annoying welcome message which makes my script not working.
Another workaround could be without --login -i:
c:\myrepo>echo git status|"C:\Program Files (x86)\Git\bin\sh.exe"
output:
....
modified: example.h
modified: example.cpp
....
The command without --login -i solves the welcome issue, but it does not autoclrf.
I could delete welcome mesage file , but I want my project to be usable after checkout without deleting anything in system.
Is there some painless solution?

I can't checkout other branch in git

Error
I got error
error: The following untracked working tree files would be overwritten by checkout:
MyProject/xcuserdata/shingo.nakanishi.xcuserdatad/UserInterfaceState.xcuserstate
Please move or remove them before you can switch branches.
Aborting
I try this
(The following untracked working tree files would be overwritten by checkout)
git rm --cached MyProject/xcuserdata/shingo.nakanishi.xcuserdatad/UserInterfaceState.xcuserstate
and
git clean -d -fx ""
git commit -a
git push
When 「UserInterfaceState.xcuserstate」file is not exist, this is work
But Xcode soon make UserInterfaceState.xcuserstate.
When I use Xcode, Xcode make UserInterfaceState.xcuserstate file.
So, Each checkout branch , I must do git clean -d -fx "" each Time.
My ~/.gitignore
this is my ~/.gitignore
.DS_Store
*UserInterfaceState.xcuserstate
*Breakpoints.xcbkptlist
How to ignore the file?
the first error means it is already in your repository (committed). you need to first remove it from the repository, then ignore it.
please read Git ignore file for Xcode projects
https://gist.github.com/3786883
Please search the SO before post questions.
For the change you have made. use git checkout -- <fileName> to discard changes.
Git doesn't ignore the files you already tracked. So just remove that file from git first
git rm -rf file
git commit -m "remove"
git push origin #{branchname}
and after, you're .gitignore will ignore it :)

Xcode and Git Source Control : "The working copy XXXXX failed to commit files"

I know this may be an ultra-newbie question, but - although I've been coding for like 18 years - it's been only a few days that I've been using Source Control for my project and I feel quite lost.
I have set up Git properly and, from time to time, I'm commiting the changes.
(File -> Source Control -> Commit)
However :
Whenever I try to delete a file (that initially existed) and then commit the changes, I'm getting an error like this :
The working copy "MY_COPY_NAME" failed to commit files
fatal: could not switch to "/the/path/of/the/file/i/just/deleted": no
such file or directory
What's going on?
What should I do in order to commit the changes even after a file has been deleted?
you should run
git add -u your_file
the -u means you want to update the file you already added. Another possibility is
git rm your_file
And it's likely you get an error if Git does not find an inexisting
To fix above error you should use below lines of code:
Open the command line [Terminal] and enter the following two commands:
xcrun git config --global user.email your#Email.com
xcrun git config --global user.name "Enter Your Name"

Resources