如何克隆一个旧的 git 提交(以及一些关于 git 的问题)

我有一个关于我的项目的 git 仓库,大约有20次提交。我知道如何用 git clone克隆实际的提交,

  • 但是我怎样才能“克隆”一个旧的承诺呢?
  • 有没有一个真正好的 git-GUI (imho qgit不是一个好的 GUI) ?
  • “分支”到底是什么?
  • 当我想要发布0.1、0.2等版本时,在 git 中标记这些提交的最好方法是什么?
  • 与 svn 有什么大的区别?
108229 次浏览

A git repository contains the all history at all time.
So when you are cloning a repository, you are cloning it with its full history, and then, you can make a branch from whatever commit you want:

 $ git checkout -b aNewBranch SHA1

with SHA1 representing the commit id from which you want to proceed.


Branches in Git are just a way to keep track of one path of a DAG (Directed Acyclic Graph) which is the set of commits representing the history of a Git repository.
It is a mere pointer you assign to one of those commits, and it will keep moving along with each new commits.

branches

See Pro Git book for more.


You can mark a specific commit with a tag, which, like a branch, is a mere pointer, but an immutable one (it wont move when you make new commit).
You will use preferably annotated tags, which are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).


The "Graphical Interfaces" section of InterfacesFrontendsAndTools page on Git Wiki lists the various GUI for Git at the moment.


You will see many questions about the difference between Git and SVN: see my answer (or this one) for example.
My most complete answer about the fundamental differences between Git and SVN is here:
"which of the two is better:git or SVN".

There are a few questions in this post, here is my take on some answers:

First, to "clone" a previous commit, you can do something like this:

git clone REPO_URL
git checkout HEAD~1 // checks out the last commit's first parent

Use ~1 to access the last commit's first parent, and increment the number to get the parent's parent and so on. More on tilde and caret notation.

The two commands above will put you in a detached HEAD state, which may or may not be important based on context. For example, it isn't important if you are cloning as part of your deployment scripts and all you care about is accessing a previous commit (say, as part of a rollback strategy).

If you need to begin work from this point in history, you can run

git checkout -b NEW_BRANCH_NAME

A good git GUI? For me SourceTree is the best.

What are branches? In my own words, a branch is just a very easy way to pivot. Say you are working on one branch, master and you want to try an experiment. Easy, just git checkout -b experiment and you are quickly in a safe place to break stuff.

What's different between git and svn?

git is a distribute version control system. svn is not. Also, branching (mentioned above) is easier in git.

For tagging, I don't know if there's "One True Way" (is there ever?) but just explore the git tag command. One great thing about git is how easy it is to clone a duplicate of your repo on your local computer (or wherever) and do whatever you want to it and see what happens. If you mess something up, just delete the directory. So, you can experiment with git tag in some testing directory and see what you like.