创建 Git 存储库

创建 Git 存储库

我已经安装了 git 2.2 版,但无法在我的目录上执行“git stash”和“git pull”。运行 git stash 和 git pull 时,我收到以下错误消息

fatal: Not a git repository (or any of the parent directories):

有人能帮助我将工作目录设为 git 存储库吗?

答案1

根据您的评论,问题很简单,您尚未在目录中初始化 git 存储库。

Git 将数据和元数据存储在.git目录中,存储在 git 存储库的顶层。如果存储库尚未初始化,则没有git stash可以恢复文件的“干净状态”。您需要创建存储库,然后进行提交,然后才能存储后续更改。

由于您似乎对 git 还不太熟悉,如果您需要将更改藏在某处,我强烈建议您远离“存储”,并保持对单独分支的提交。存储很快就会变得混乱。

mkdir mygit || exit -1
cd mygit
git init 
date > file.txt
git add file.txt
git commit -m 'My initial commit'
date >> file.txt
git status
git diff
git stash
git status 
git diff
cd ..
rm -rf mygit

这是一个直接来自 shell 的示例:

$ mkdir mygit || exit -1
$ cd mygit
$ git init
Initialized empty Git repository in /Users/dfarrell/mygit/.git/
$ date > file.txt
$ git add file.txt
$ git commit -m 'My initial commit'
[master (root-commit) 21de065] My initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ date >> file.txt
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/file.txt b/file.txt
index 99e9a80..8dcda34 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
 Mon Dec 15 16:00:32 CST 2014
+Mon Dec 15 16:00:32 CST 2014
$ git stash
Saved working directory and index state WIP on master: 21de065 My initial commit
HEAD is now at 21de065 My initial commit
$ git status
On branch master
nothing to commit, working directory clean
$ git diff
$ cd ..
$ rm -rf mygit
$

相关内容