Mauricio T.
Mauricio T.

Reputation: 23

How can I know in which git branch I create a file?

I'm working in a git project. I have several branch, I code a bit on a branch, git add then git commit, switch to another branch, code, add, commit. My problem is that at one point or another I forgot to add a file and git status show me the file as untracked, but I forget in which branch create the file. How can I know that? If I add the file and then commit, the file is commited to the actual branch? or to the branch its belong to? Thanks in advance.

Upvotes: 0

Views: 204

Answers (3)

gzm0
gzm0

Reputation: 14842

When you do:

git add myfile.txt
git commit -m 'My message'

The file (and all other changes in the index) is committed to the current branch you are on. If you have not added and committed a file when you were in the right branch, git will be unable to tell you in which branch you where when you actually created it.

UPDATE You can issue:

git reflog show --date=local

This shows you when you switched branches. Together with the creation time of the file this will allow you to determine which branch the file probably belongs to.

Upvotes: 2

Chris Throup
Chris Throup

Reputation: 671

You cannot find out which branch it belongs to because it doesn't belong to any; you didn't commit it.

You may be able to work out which branch you were working on when you created it, assuming you haven't made any changes to the file since, by comparing timestamps.

Run git log --all to see the list of commits from all branches, and look for the timestamp which most closely matches that of your mystery file. That commit is probably from the branch you are looking for.

Upvotes: 2

Nick Tomlin
Nick Tomlin

Reputation: 29221

  1. Because git is not tracking the file, you cannot know which branch it was originally created in.
  2. The file will be added to the current "working" branch

I'm not sure what Environment you are in, but it can be helpful to look at the timestamp on the file to try and try to find corresponding branch/commit by using git log --until <time>

Upvotes: 2

Related Questions