2

I am new to git and try to understand how I ll undo /stash changes + logs lets say I have a very fresh branch with no commits yet e.g

git clone https://url   testrepo
cd testrepo
git log 

   fatal: bad default revision 'HEAD'

git checkout

   fatal: You are on a branch yet to be born

echo "test" > README.txt
git status

On branch master
Initial commit

Untracked files:   (use "git add <file>..." to include in what will be
committed)

    README.txt

nothing added to commit but untracked files present (use "git add" to track)

git add .
git status 

On branch master

Initial commit

Changes to be committed:   (use "git rm --cached <file>..." to unstage)
    new file:   README.txt
git commit -m "My read me file "

[master (root-commit) 6357fd2] My read me file
1 file changed, 1 insertion(+)
create mode 100644 README.txt

git status

On branch master Your branch is based on 'origin/master', but the        
upstream is gone.   (use "git branch --unset-upstream" to fixup)
nothing to commit, working directory clean

(I don't understand what does "upstream is gone" mean :( )

However, now lets say I want to stash all changes. One simple solution is to delete the clone and re-clone from remote repo . What are other options to undo the above commit so I ll again have a clean checkout (without commits + commit history (logs)

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
sakhunzai
  • 13,900
  • 23
  • 98
  • 159

1 Answers1

1

You have created a project and added some content to it.
Since you have clone an empty repository it does not have any content yet.

fatal: bad default revision 'HEAD'

To understand what HEAD is - read about in here:
How to move HEAD back to a previous location? (Detached head)

I don't understand what does "upstream is gone" mean :(

Since you have cloned an empty repository there is no such branch named master on the remote so you have to create it (end of the answer).

If you would have cloned an existing repo you would have gotten the master branch (or any other default branch) checkout out to your project.

In your case as explained above you have cloned an empty project so you have to first push master

git push origin master

Why do i need to push the master branch?

The first commit to your local repository created master branch and it still not found on the remote.


However, now lets say I want to stash all changes....
What are other options to undo the above commit so I ll again have a clean checkout

Read the above linked post about head to learn how to do it.


Orphan branch

Another option is to checkout an orphan branch (branch without any history)

git checkout --orphan <new_branch>

And you will have a clean branch with the content of the folder but without any history.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • thanks, how I ll undo commits , lets say I change my mind ? ,Obviously one option is to delete the directory and clone again – sakhunzai Mar 14 '16 at 10:15
  • Reat the related answer: http://stackoverflow.com/questions/34519665/how-to-move-head-checkout-revert-reflog-reset-old-commit/34519716#34519716 and the last part of the answer here about orphan branches – CodeWizard Mar 14 '16 at 10:18
  • :) it seems "chicken or the egg" problem since I dont have any branch to checkout ,event I dont have master , therefore command `git checkout --orphan` fails . Further .git directory still hold some entries under .git/objects. – sakhunzai Mar 14 '16 at 10:38
  • If you need you can recover any object inside your `.git` folder – CodeWizard Mar 14 '16 at 11:35