0

Hi guys I am new to git .

  1. I created a branch and commited my files on it .
  2. then created another branch (this one is the one i want to push) .
  3. pulled the first branch to the new branch .
  4. mistakenly did

    git checkout
    

instead of

git checkout "file"

Now when I type git status i get a message

nothing to commit

What can I do?

AD7six
  • 63,116
  • 12
  • 91
  • 123
sisimh
  • 1,287
  • 3
  • 20
  • 37
  • 3
    `git checkout` without any further arguments is a "glorified no-op", as the docs put it (http://git-scm.com/docs/git-checkout). So this operation did not do anything. Whatever is wrong with your tree, it is caused by something else. – thule Jul 26 '15 at 13:15
  • what about git checkout . ? @letitbee – sisimh Jul 26 '15 at 13:17
  • Yep, that's bad. This will kill all your changes. – thule Jul 26 '15 at 13:19
  • 2
    And no way to undo it. Git cannot help you if you have not commited your code in any way, or at least stashed it. But the good news is if you have commited, there is almost always a way to get it back. – thule Jul 26 '15 at 13:22
  • well I commited the files on another branch , then switched to a new branch and pulled that branch ,, so the changes are there one that branch but i checked them on this one i am on now ...so what i want is to have them on my current branch – sisimh Jul 26 '15 at 13:28
  • What do you mean by "pushed the first branch to the new branch" – Anthony Raymond Jul 26 '15 at 13:33
  • sorry I pulled it .. i did git pull origin firstBranch – sisimh Jul 26 '15 at 13:33
  • 1
    you describe the process you have done, can you add the command? I can't understand how you can pull a local branch. – Anthony Raymond Jul 26 '15 at 13:36
  • @AnthonyRaymond i just did in my previous comment ... "git pull branch" – sisimh Jul 26 '15 at 13:40
  • I mean the whole process. I can see two options, you are missunderstanding how commit and push works, or i am missing somethink you have done. As far as i can see you did not pushed anithing, but you are trying to pull a branch that have never been pushed, and this is not possible. – Anthony Raymond Jul 26 '15 at 13:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84302/discussion-between-anthony-raymond-and-sisimh). – Anthony Raymond Jul 26 '15 at 13:44

2 Answers2

1

You can revert to previous commit, ignoring any changes: git reset --hard HEAD.

Where HEAD is the last commit in your current branch.

If you want to get remote gh-pages branch: git reset --hard origin/gh-pages.

Suriyaa
  • 2,222
  • 2
  • 25
  • 44
  • i dont have a commit on my current branch , I pulled another branch then did checkout . by mistake , what can I do now ? – sisimh Jul 26 '15 at 13:18
  • if "there is nothing to commit" it is already at the previous commit state – Anthony Raymond Jul 26 '15 at 13:20
  • but i pulled another branch and I want that branch's files to commit them on this branch , so now after the checkout i dont see the files anymore @AnthonyRaymond ,, so what can I do now ? – sisimh Jul 26 '15 at 13:25
  • Can you please add to you post the complete list of operation you have done? – Anthony Raymond Jul 26 '15 at 13:27
0

I think that you are missunderstanding commit and push.

A commit is like a pictures you are shooting with a camera. (you specifie what elements you want to add to this pictures with add).

When you have taken this pictures, it will stay in you album (where nobody execpt you can see it).

How to :
git add <path_to_file_1>
git add <path_to_file_2>
git commit -m "my commit message to describe what i had done"




A push is use to send you album to a repository.

How to :
git push <repo_name> <branchName>




When code is into a distant repository, it can be pulled to be merged with your code.

How to :
git pull <repo_name> <repoBranch>




What have you done.

As far as i understand, you have done the following sequence.

git branch my_first_branch
git checkout my_first_branch
..... working on your branch, modifying files .....
git add < all my files>
git commit -m "Hey this is my first commit"

git branch my_second_branch
# At this point, since you created your second_branch from first_branch, second branch contains all modifications made to first_branch, there are both the same.

git pull origin first_branch
# This should have failed unless you have push ????

git checkout
# this should have done nothing
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59