Git 初学者:权威实用指南
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/315911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git for beginners: The definitive practical guide
提问by Adam Davis
Ok, after seeing this post by PJ Hyett, I have decided to skip to the end and go with Git.
好的,在看到PJ Hyett 的这篇文章后,我决定跳到最后并使用Git。
So what I need is a beginner's practicalguide to Git. "Beginner" being defined as someone who knows how to handle their compiler, understands to some level what a Makefileis, and has touched source control without understanding it very well.
所以我需要的是一个初学者的Git实用指南。“初学者”被定义为知道如何处理他们的编译器的人,在某种程度上理解Makefile是什么,并且在没有很好理解的情况下接触了源代码控制。
"Practical" being defined as this person doesn't want to get into great detail regarding what Git is doing in the background, and doesn't even care (or know) that it's distributed. Your answers might hint at the possibilities, but try to aim for the beginner that wants to keep a 'main' repository on a 'server' which is backed up and secure, and treat their local repository as merely a 'client' resource.
“实用”被定义为这个人不想详细了解 Git 在后台做什么,甚至不关心(或知道)它是分布式的。您的回答可能暗示了这些可能性,但请尝试针对想要在备份和安全的“服务器”上保留“主”存储库并将其本地存储库仅视为“客户端”资源的初学者。
So:
所以:
Installation/Setup
安装/设置
- How to install Git
- How do you set up Git? Try to cover Linux, Windows, Mac, think 'client/server' mindset.
- How do you create a new project/repository?
- How do you configure it to ignore files (.obj, .user, etc) that are not really part of the codebase?
- 如何安装 Git
- 你如何设置 Git?尝试涵盖 Linux、Windows、Mac,想想“客户端/服务器”的心态。
- 你如何创建一个新的项目/存储库?
- 您如何配置它以忽略不属于代码库的文件(.obj、.user 等)?
Working with the code
使用代码
- How do you get the latest code?
- How do you check out code?
- How do you commit changes?
- How do you see what's uncommitted, or the status of your current codebase?
- How do you destroy unwanted commits?
- How do you compare two revisions of a file, or your current file and a previous revision?
- How do you see the history of revisions to a file?
- How do you handle binary files (visio docs, for instance, or compiler environments)?
- How do you merge files changed at the "same time"?
- How do you undo (revert or reset) a commit?
- 你如何获得最新的代码?
- 你如何检查代码?
- 你如何提交更改?
- 您如何查看未提交的内容或当前代码库的状态?
- 你如何销毁不需要的提交?
- 您如何比较文件的两个修订版,或者您当前的文件和以前的修订版?
- 您如何查看文件的修订历史?
- 您如何处理二进制文件(例如,visio 文档或编译器环境)?
- 如何合并“同时”更改的文件?
- 如何撤消(还原或重置)提交?
Tagging, branching, releases, baselines
标记、分支、发布、基线
- How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?
- How do you pull a particular 'release'?
- How do you branch?
- How do you merge branches?
- How do you resolve conflicts and complete the merge?
- How do you merge parts of one branch into another branch?
- What is rebasing?
- How do I track remote branches?
- How can I create a branch on a remote repository?
- How do I delete a branch on a remote repository?
- Git workflow examples
- 您如何“标记”、“标记”或“发布”一组特定文件的特定修订版本,以便您以后可以随时提取该修订版本?
- 你如何拉一个特定的“发布”?
- 你如何分支?
- 你如何合并分支?
- 您如何解决冲突并完成合并?
- 如何将一个分支的部分合并到另一个分支?
- 什么是变基?
- 如何跟踪远程分支?
- 如何在远程存储库上创建分支?
- 如何删除远程存储库上的分支?
- Git 工作流示例
Other
其他
- Describe and link to a good GUI, IDE plugin, etc. that makes Git a non-command line resource, but please list its limitations as well as its good.
- Any other common tasks a beginner should know?
- How do I work effectively with a subversion repository set as my source control source?
- 描述并链接到使 Git 成为非命令行资源的良好 GUI、IDE 插件等,但请列出其局限性和优点。
- 初学者应该知道的其他常见任务吗?
- 我如何有效地使用设置为我的源代码控制源的 subversion 存储库?
Other Git beginner's references
其他 Git 初学者参考
- Git guide
- Git book
- Git magic
- gitcasts
- GitHub guides
- Git tutorial
- Progit - book by Scott Chacon
- Git - SVN Crash Course
- Git from the bottom up
- Git ready
- gitref.org
- Git visual cheatsheet
- Git 指南
- git书
- Git魔术
- gitcasts
- GitHub 指南
- Git教程
- Progit - 斯科特·查康 (Scott Chacon) 着的书
- Git - SVN 速成课程
- Git自下而上
- Git 准备好了
- gitref.org
- Git 可视化备忘单
Delving into Git
深入了解 Git
I will go through the entries from time to time and 'tidy' them up so they have a consistent look/feel and it's easy to scan the list - feel free to follow a simple "header - brief explanation - list of instructions - gotchas and extra info" template. I'll also link to the entries from the bullet list above so it's easy to find them later.
我会不时浏览条目并“整理”它们,使它们具有一致的外观/感觉,并且很容易浏览列表 - 随意遵循简单的“标题 - 简要说明 - 说明列表 - 陷阱和额外信息”模板。我还将链接到上面项目符号列表中的条目,以便以后轻松找到它们。
回答by dbr
How do you create a new project/repository?
你如何创建一个新的项目/存储库?
A git repository is simply a directory containing a special .git
directory.
git 存储库只是一个包含特殊.git
目录的目录。
This is different from "centralised" version-control systems (like subversion), where a "repository" is hosted on a remote server, which you checkout
into a "working copy" directory. With git, your working copy isthe repository.
这与“集中式”版本控制系统(如 subversion)不同,其中“存储库”托管在远程服务器上,您可以checkout
将其放入“工作副本”目录中。使用 git,您的工作副本就是存储库。
Simply run git init
in the directory which contains the files you wish to track.
只需git init
在包含要跟踪的文件的目录中运行即可。
For example,
例如,
cd ~/code/project001/
git init
This creates a .git
(hidden) folder in the current directory.
这将.git
在当前目录中创建一个(隐藏)文件夹。
To make a new project, run git init
with an additional argument (the name of the directory to be created):
要创建一个新项目,请git init
使用附加参数(要创建的目录的名称)运行:
git init project002
(This is equivalent to: mkdir project002 && cd project002 && git init)
To check if the current current path is within a git repository, simply run git status
- if it's not a repository, it will report "fatal: Not a git repository"
要检查当前当前路径是否在 git 存储库中,只需运行git status
- 如果它不是存储库,它将报告“fatal: Not a git repository”
You could also list the .git
directory, and check it contains files/directories similar to the following:
您还可以列出.git
目录,并检查它是否包含类似于以下内容的文件/目录:
$ ls .git
HEAD config hooks/ objects/
branches/ description info/ refs/
If for whatever reason you wish to "de-git" a repository (you wish to stop using git to track that project). Simply remove the .git
directory at the base level of the repository.
如果出于某种原因您希望“de-git”一个存储库(您希望停止使用 git 来跟踪该项目)。只需删除.git
存储库基础级别的目录。
cd ~/code/project001/
rm -rf .git/
Caution:This will destroy allrevision history, allyour tags, everythinggit has done. It will not touch the "current" files (the files you can currently see), but previous changes, deleted files and so on will be unrecoverable!
注意:这将破坏所有修订历史、所有标签以及git 所做的一切。它不会触及“当前”文件(您当前可以看到的文件),但之前的更改、删除的文件等将无法恢复!
回答by dylanfm
GUIs for git
git 图形用户界面
Git GUI
图形用户界面
Included with git — Run git gui
from the command line, and the Windows msysgitinstaller adds it to the Start menu.
包含在 git 中 —git gui
从命令行运行,Windows msysgit安装程序将其添加到“开始”菜单。
Git GUI can do a majority of what you'd need to do with git. Including stage changes, configure git and repositories, push changes, create/checkout/delete branches, merge, and many other things.
Git GUI 可以完成您需要使用 git 完成的大部分工作。包括阶段更改、配置 git 和存储库、推送更改、创建/签出/删除分支、合并等等。
One of my favourite features is the "stage line" and "stage hunk" shortcuts in the right-click menu, which lets you commit specific parts of a file. You can achieve the same via git add -i
, but I find it easier to use.
我最喜欢的功能之一是右键单击菜单中的“stage line”和“stage hunk”快捷方式,它允许您提交文件的特定部分。您可以通过 实现相同的效果git add -i
,但我发现它更易于使用。
It isn't the prettiest application, but it works on almost all platforms (being based upon Tcl/Tk)
它不是最漂亮的应用程序,但它几乎适用于所有平台(基于 Tcl/Tk)
GitK
吉特克
Also included with git. It is a git history viewer, and lets you visualise a repository's history (including branches, when they are created, and merged). You can view and search commits.
也包含在 git 中。它是一个 git 历史查看器,可让您可视化存储库的历史记录(包括分支、创建和合并的时间)。您可以查看和搜索提交。
Goes together nicely with git-gui.
与 git-gui 搭配得很好。
Gitnub
gitnub
Mac OS X application. Mainly an equivalent of git log
, but has some integration with github(like the "Network view").
Mac OS X 应用程序。主要相当于git log
,但与github有一些集成(如“网络视图”)。
Looks pretty, and fits with Mac OS X. You can search repositories. The biggest critisism of Gitnub is that it shows history in a linear fashion (a single branch at a time) - it doesn't visualise branching and merging, which can be important with git, although this is a planned improvement.
看起来很漂亮,并且适合 Mac OS X。您可以搜索存储库。对 Gitnub 最大的批评是它以线性方式(一次一个分支)显示历史——它没有可视化分支和合并,这对 git 来说可能很重要,尽管这是一个有计划的改进。
Download links, change log and screenshots| git repository
GitX
GitX
Intends to be a "gitk clone for OS X".
打算成为“OS X 的 gitk 克隆”。
It can visualise non-linear branching history, perform commits, view and search commits, and it has some other nice features like being able to "Quicklook" any file in any revision (press space in the file-list view), export any file (via drag and drop).
它可以可视化非线性分支历史、执行提交、查看和搜索提交,它还有一些其他不错的功能,例如能够“快速查看”任何修订中的任何文件(在文件列表视图中按空格)、导出任何文件(通过拖放)。
It is far better integrated into OS X than git-gui
/gitk
, and is fast and stable even with exceptionally large repositories.
它比git-gui
/更好地集成到 OS X 中gitk
,并且即使在非常大的存储库中也快速而稳定。
The original git repository pieterhas not updated recently (over a year at time of writing). A more actively maintained branch is available at brotherbard/gitx- it adds "sidebar, fetch, pull, push, add remote, merge, cherry-pick, rebase, clone, clone to"
原始 git 存储库pieter最近没有更新(在撰写本文时已超过一年)。在Brotherbard/gitx 上提供了一个更积极维护的分支——它添加了“侧边栏、获取、拉取、推送、添加远程、合并、挑选、rebase、克隆、克隆到”
Download| Screenshots| git repository| brotherbard fork| laullon fork
SmartGit
智能Git
From the homepage:
从主页:
SmartGit is a front-end for the distributed version control system Git and runs on Windows, Mac OS X and Linux. SmartGit is intended for developers who prefer a graphical user interface over a command line client, to be even more productive with Git — the most powerful DVCS today.
SmartGit 是分布式版本控制系统 Git 的前端,可在 Windows、Mac OS X 和 Linux 上运行。SmartGit 适用于更喜欢图形用户界面而不是命令行客户端的开发人员,以便使用 Git(当今最强大的 DVCS)提高工作效率。
You can download it from their website.
你可以从他们的网站下载。
TortoiseGit
TortoiseGit
TortoiseSVN Git version for Windows users.
适用于 Windows 用户的 TortoiseSVN Git 版本。
It is porting TortoiseSVN to TortoiseGit The latest release 1.2.1.0 This release can complete regular task, such commit, show log, diff two version, create branch and tag, Create patch and so on. See ReleaseNotesfor detail. Welcome to contribute this project.
正在将 TortoiseSVN 移植到 TortoiseGit 最新版本 1.2.1.0 此版本可以完成常规任务,如提交、显示日志、差异两个版本、创建分支和标签、创建补丁等。请参见发布记录的细节。欢迎贡献这个项目。
QGit
QGit
QGit is a git GUI viewer built on Qt/C++.
With qgit you will be able to browse revisions history, view patch content and changed files, graphically following different development branches.
QGit 是一个基于 Qt/C++ 构建的 git GUI 查看器。
使用 qgit,您将能够浏览修订历史、查看补丁内容和更改的文件,以图形方式遵循不同的开发分支。
gitg
gitg
gitg is a git repository viewer targeting gtk+/GNOME. One of its main objectives is to provide a more unified user experience for git frontends across multiple desktops. It does this not be writing a cross-platform application, but by close collaboration with similar clients for other operating systems (like GitX for OS X).
gitg 是一个针对 gtk+/GNOME 的 git 存储库查看器。其主要目标之一是为跨多个桌面的 git 前端提供更统一的用户体验。这并不是编写跨平台应用程序,而是通过与其他操作系统的类似客户端(如 OS X 的 GitX)密切合作。
Features
特征
- Browse revision history.
- Handle large repositories (loads linux repository, 17000+ revisions, under 1 second).
- Commit changes.
- Stage/unstage individual hunks.
- Revert changes.
- Show colorized diff of changes in revisions.
- Browse tree for a given revision.
- Export parts of the tree of a given revision.
- Supply any refspec which a command such as 'git log' can understand to built the history.
- Show and switch between branches in the history view.
- 浏览修订历史。
- 处理大型存储库(加载 linux 存储库,17000 多个修订版,不到 1 秒)。
- 提交更改。
- 舞台/非舞台个人帅哥。
- 还原更改。
- 显示修订更改的彩色差异。
- 浏览给定修订的树。
- 导出给定修订的树的部分。
- 提供诸如“git log”之类的命令可以理解的任何 refspec 来构建历史记录。
- 在历史视图中显示和切换分支。
Gitbox
gitbox
Gitbox is a Mac OS X graphical interface for Git version control system. In a single window you see branches, history and working directory status.
Everyday operations are easy: stage and unstage changes with a checkbox. Commit, pull, merge and push with a single click. Double-click a change to show a diff with FileMerge.app.
Gitbox 是用于 Git 版本控制系统的 Mac OS X 图形界面。在单个窗口中,您可以看到分支、历史记录和工作目录状态。
日常操作很简单:使用复选框暂存和取消暂存更改。只需单击一下即可提交、拉取、合并和推送。双击更改以显示与 FileMerge.app 的差异。
Gity
吉蒂
The Gity website doesn't have much information, but from the screenshots on there it appears to be a feature rich open source OS X git gui.
Gity 网站没有太多信息,但从那里的屏幕截图来看,它似乎是一个功能丰富的开源 OS X git gui。
Meld
融合
Meld is a visual diff and merge tool. You can compare two or three files and edit them in place (diffs update dynamically). You can compare two or three folders and launch file comparisons. You can browse and view a working copy from popular version control systems such such as CVS, Subversion, Bazaar-ng and Mercurial [and Git].
Meld 是一个视觉差异和合并工具。您可以比较两个或三个文件并就地编辑它们(差异动态更新)。您可以比较两个或三个文件夹并启动文件比较。您可以从流行的版本控制系统(例如 CVS、Subversion、Bazaar-ng 和 Mercurial [和 Git])浏览和查看工作副本。
Katana
武士刀
A Git GUIfor OSX by Steve Dekorte.
Steve Dekorte 为 OSX 编写的 Git GUI。
At a glance, see which remote branches have changes to pull and local repos have changes to push. The git ops of add, commit, push, pull, tag and reset are supported as well as visual diffs and visual browsing of project hieracy that highlights local changes and additions.
一目了然,查看哪些远程分支有更改要拉,本地存储库有更改要推送。支持添加、提交、推送、拉取、标记和重置的 git 操作,以及突出显示本地更改和添加的项目层次结构的视觉差异和视觉浏览。
Free for 1 repository, $25 for more.
1 个存储库免费,更多则 25 美元。
Sprout (formerly GitMac)
Sprout(以前的 GitMac)
Focuses on making Git easy to use. Features a native Cocoa (mac-like) UI, fast repository browsing, cloning, push/pull, branching/merging, visual diff, remote branches, easy access to the Terminal, and more.
专注于使 Git 易于使用。具有原生 Cocoa(类 Mac)用户界面、快速存储库浏览、克隆、推/拉、分支/合并、视觉差异、远程分支、轻松访问终端等。
By making the most commonly used Git actions intuitive and easy to perform, Sprout (formerly GitMac) makes Git user-friendly. Compatible with most Git workflows, Sprout is great for designers and developers, team collaboration and advanced and novice users alike.
通过使最常用的 Git 操作直观且易于执行,Sprout(以前称为 GitMac)使 Git 用户友好。Sprout 与大多数 Git 工作流程兼容,非常适合设计师和开发人员、团队协作以及高级和新手用户。
Tower
塔
A feature-rich Git GUI for Mac OSX. 30-day free trial, $59USD for a single-user license.
适用于 Mac OSX 的功能丰富的 Git GUI。30 天免费试用,单用户许可证 59 美元。
EGit
EGit
EGit is an Eclipse Team provider for the Git version control system. Git is a distributed SCM, which means every developer has a full copy of all history of every revision of the code, making queries against the history very fast and versatile.
The EGit project is implementing Eclipse tooling on top of the JGit Java implementation of Git.
EGit 是 Git 版本控制系统的 Eclipse 团队提供者。Git 是分布式 SCM,这意味着每个开发人员都拥有代码每次修订的所有历史记录的完整副本,从而可以非常快速且灵活地查询历史记录。
EGit 项目在 Git 的 JGit Java 实现之上实现了 Eclipse 工具。
Git Extensions
Git 扩展
Open Source for Windows - installs everything you need to work with Git in a single package, easy to use.
适用于 Windows 的开源 - 在单个包中安装使用 Git 所需的一切,易于使用。
Git Extensions is a toolkit to make working with Git on Windows more intuitive. The shell extension will intergrate in Windows Explorer and presents a context menu on files and directories. There is also a Visual Studio plugin to use git from Visual Studio.
Git Extensions 是一个工具包,可让您在 Windows 上更直观地使用 Git。shell 扩展将集成到 Windows 资源管理器中,并在文件和目录上显示上下文菜单。还有一个 Visual Studio 插件可以使用 Visual Studio 中的 git。
Big thanks to dbrfor elaborating on the git gui stuff.
非常感谢dbr对 git gui 内容的详细说明。
SourceTree
源树
SourceTree is a freeMac client for Git, Mercurial and SVN. Built by Atlassian, the folks behind BitBucket, it seems to work equally well with any VC system, which allows you to master a single tool for use with all of your projects, however they're version-controlled. Feature-packed, and FREE.
SourceTree 是适用于 Git、Mercurial 和 SVN的免费Mac 客户端。由 BitBucket 背后的人员 Atlassian 构建,它似乎与任何 VC 系统都一样好用,它允许您掌握一个用于所有项目的工具,但是它们是受版本控制的。功能丰富,免费。
Expert-Ready & Feature-packed for both novice and advanced users:
Review outgoing and incoming changesets. Cherry-pick between branches. Patch handling, rebase, stash / shelve and much more.
专家就绪且功能丰富,适合新手和高级用户:
查看传出和传入的变更集。在树枝间摘樱桃。补丁处理、rebase、藏匿/搁置等等。
回答by Pat Notz
Well, despite the fact that you asked that we not "simply" link to other resources, it's pretty foolish when there already exists a community grown (and growing) resource that's really quite good: the Git Community Book. Seriously, this 20+ questions in a question is going to be anything but concise and consistent. The Git Community Book is available as both HTML and PDF and answers many of your questions with clear, well formatted and peer reviewed answers and in a format that allows you to jump straight to your problem at hand.
好吧,尽管您要求我们不要“简单地”链接到其他资源,但当已经存在一个社区发展(和不断增长)的资源非常好时,这是非常愚蠢的:Git 社区手册。说真的,一个问题中的这 20 多个问题绝不会是简洁和一致的。Git 社区手册以 HTML 和 PDF 格式提供,并以清晰、格式良好和同行评审的答案回答您的许多问题,并且格式允许您直接跳到手头的问题。
Alas, if my post really upsets you then I'll delete it. Just say so.
唉,如果我的帖子真的让你不高兴,那么我会删除它。就这么说吧。
回答by Brian Gianforcaro
How to configure it to ignore files:
如何将其配置为忽略文件:
The ability to have git ignore files you don't wish it to track is very useful.
让 git 忽略您不希望它跟踪的文件的能力非常有用。
To ignore a file or set of files you supply a pattern. The pattern syntax for git is fairly simple, but powerful. It is applicable to all three of the different files I will mention bellow.
要忽略一个文件或一组文件,请提供一个模式。git 的模式语法相当简单,但功能强大。它适用于我将在下面提到的所有三个不同的文件。
- A blank line ignores no files, it is generally used as a separator.
- Lines staring with #serve as comments.
- The !prefix is optional and will negate the pattern. Any negated pattern that matches will override lower precedence patterns.
- Supports advanced expressions and wild cards
- Ex: The pattern: *.[oa]will ignore all files in the repository ending in .o or .a (object and archive files)
- If a pattern has a directory ending with a slash git will only match this directory and paths underneath it. This excludes regular files and symbolic links from the match.
- A leading slash will match all files in that path name.
- Ex: The pattern /*.cwill match the file foo.cbut not bar/awesome.c
- 空行不忽略任何文件,它通常用作分隔符。
- 以#开头的行作为注释。
- 该!前缀是可选的,将否定模式。任何匹配的否定模式都将覆盖较低优先级的模式。
- 支持高级表达式和通配符
- 例如:模式:*.[oa]将忽略存储库中以 .o 或 .a 结尾的所有文件(对象和存档文件)
- 如果一个模式有一个以斜杠结尾的目录,git 只会匹配这个目录和它下面的路径。这从匹配中排除了常规文件和符号链接。
- 前导斜杠将匹配该路径名中的所有文件。
- 例如:模式/*.c将匹配文件foo.c但不匹配bar/awesome.c
Great Example from the gitignore(5)man page:
来自gitignore(5)手册页的好例子:
$ git status
[...]
# Untracked files:
[...]
# Documentation/foo.html
# Documentation/gitignore.html
# file.o
# lib.a
# src/internal.o
[...]
$ cat .git/info/exclude
# ignore objects and archives, anywhere in the tree.
*.[oa]
$ cat Documentation/.gitignore
# ignore generated html files,
*.html
# except foo.html which is maintained by hand
!foo.html
$ git status
[...]
# Untracked files:
[...]
# Documentation/foo.html
[...]
Generally there are three different ways to ignore untracked files.
通常有三种不同的方法可以忽略未跟踪的文件。
1) Ignore for all users of the repository:
1)忽略存储库的所有用户:
Add a file named .gitignoreto the root of your working copy.
将名为.gitignore的文件添加到工作副本的根目录。
Edit .gitignoreto match your preferences for which files should/shouldn't be ignored.
编辑.gitignore以匹配您对哪些文件应该/不应该被忽略的偏好。
git add .gitignore
and commit when you're done.
完成后提交。
2) Ignore for only your copy of the repository:
2)仅忽略您的存储库副本:
Add/Edit the file $GIT_DIR/info/excludein your working copy, with your preferred patterns.
添加/编辑文件$GIT_DIR/info/exclude在您的工作副本中,使用您喜欢的模式。
Ex: My working copy is ~/src/project1 so I would edit ~/src/project1/.git/info/exclude
例如:我的工作副本是 ~/src/project1 所以我会编辑~/src/project1/.git/info/exclude
You're done!
你完成了!
3) Ignore in all situations, on your system:
3)在所有情况下,在您的系统上忽略:
Global ignore patterns for your system can go in a file named what ever you wish.
你的系统的全局忽略模式可以放在一个你想要的名字的文件中。
Mine personally is called ~/.gitglobalignore
我个人叫做~/.gitglobalignore
I can then let git know of this file by editing my ~/.gitconfigfile with the following line:
然后我可以通过使用以下行编辑我的~/.gitconfig文件来让 git 知道这个文件:
core.excludesfile = ~/.gitglobalignore
You're done!
你完成了!
I find the gitignoreman page to be the best resource for more information.
我发现gitignore手册页是获取更多信息的最佳资源。
回答by dbr
How do you 'tag' a particular set of revisions
你如何“标记”一组特定的修订
How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?
您如何“标记”、“标记”或“发布”一组特定文件的特定修订版本,以便您以后可以随时提取该修订版本?
Using the git tag
command.
使用git tag
命令。
To simply "tag" the current revision, you would just run..
要简单地“标记”当前修订版,您只需运行..
git tag -a thetagname
git tag -a 0.1
git tag -a 2.6.1-rc1 -m 'Released on 01/02/03'
To list the current tags, simply run git tag
with no arguments, or -l
(lower case L):
要列出当前标签,只需git tag
不带参数运行,或者-l
(小写 L):
$ git tag -a thetagname # and enter a message, or use -m 'My tag annotation'
$ git tag -l
thetagname
To delete a tag, you use the -d
flag:
要删除标签,请使用以下-d
标志:
$ git tag -d thetagname
Deleted tag 'thetagname'
$ git tag
[no output]
To tag a specific (previous) commit, you simply do..
要标记特定的(先前的)提交,您只需执行..
git tag [tag name] [revision SHA1 hash]
For example:
例如:
git tag 1.1.1 81b15a68c6c3e71f72e766931df4e6499990385b
Note: by default, git creates a "lightweight" tag (basically a reference to a specific revision). The "right" way is to use the -a
flag. This will launch your editor asking for a tag message (identical to asking for a commit message, you can also use the -m
flag to supply the tag message on the command line). Using an annotated tag creates an object with its own ID, date, tagger (author), and optionally a GPG signature (using the -s
tag).
For further information on this, see this post
注意:默认情况下,git 创建一个“轻量级”标签(基本上是对特定修订版的引用)。“正确”的方法是使用-a
标志。这将启动您的编辑器,要求提供标签消息(与要求提交消息相同,您也可以使用该-m
标志在命令行上提供标签消息)。使用带注释的标签创建一个具有自己的 ID、日期、标记者(作者)和可选的 GPG 签名(使用-s
标签)的对象。
有关这方面的更多信息,请参阅此帖子
git tag mytagwithmsg -a -m 'This is a tag, with message'
And to list the tags with annotations, use the -n1
flag to show 1 line of each tag message (-n245
to show the first 245 lines of each annotation, and so on):
并列出带有注释的标签,使用-n1
标志显示每个标签消息的 1 行(-n245
显示每个注释的前 245 行,依此类推):
$ git tag -l -n1
mytagwithmsg This is a tag, with message
For more information, see the git-tag(1) Manual Page
有关更多信息,请参阅git-tag(1) 手册页
回答by ashwoods
Workflow example with GIT.
使用 GIT 的工作流示例。
Git is extremely flexible and adapts good to any workflow, but not enforcing a particular workflow might have the negative effect of making it hard to understand what you can do with git beyond the linear "backup" workflow, and how useful branching can be for example.
Git 非常灵活,可以很好地适应任何工作流程,但不强制执行特定的工作流程可能会产生负面影响,即难以理解除了线性“备份”工作流程之外您可以使用 git 做什么,以及例如分支的有用性.
This blog postexplains nicely a very simple but effective workflow that is really easy to setup using git.
这篇博文很好地解释了一个非常简单但有效的工作流程,它非常容易使用 git 进行设置。
quoting from the blog post: We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state:
引自博文:我们认为 origin/master 是 HEAD 源代码始终反映生产就绪状态的主要分支:
The workflow has become popular enough to have made a project that implements this workflow: git-flow
工作流已经变得足够流行,以至于创建了一个实现此工作流的项目:git-flow
Nice illustration of a simple workflow, where you make all your changes in develop, and only push to master when the code is in a production state:
一个简单的工作流程的很好的例证,您可以在其中进行所有更改,并且仅在代码处于生产状态时推送到 master:
Now let's say you want to work on a new feature, or on refactoring a module. You could create a new branch, what we could call a "feature" branch, something that will take some time and might break some code. Once your feature is "stable enough" and want to move it "closer" to production, you merge your feature branch into develop. When all the bugs are sorted out after the merge and your code passes all tests rock solid, you push your changes into master.
现在让我们假设您想要开发一个新功能,或者重构一个模块。你可以创建一个新的分支,我们可以称之为“功能”分支,这需要一些时间并且可能会破坏一些代码。一旦您的功能“足够稳定”并希望将其“更接近”生产,您将您的功能分支合并到开发中。当合并后所有错误都被解决并且您的代码通过所有测试时,您将更改推送到 master。
During all this process, you find a terrible security bug, that has to be fixed right away. You could have a branch called hotfixes, that make changes that are pushed quicker back into production than the normal "develop" branch.
在整个过程中,您会发现一个可怕的安全漏洞,必须立即修复。您可以有一个名为 hotfixes 的分支,它可以比正常的“开发”分支更快地将更改推回生产。
Here you have an illustration of how this feature/hotfix/develop/production workflow might look like (well explained in the blog post, and I repeat, the blog post explains the whole process in a lot more detail and a lot better than I do.
在这里,您可以说明此功能/修补程序/开发/生产工作流程的外观(在博客文章中进行了很好的解释,我再说一遍,该博客文章比我做的更详细并且更好地解释了整个过程.
回答by Adam Davis
Here's a copy of PJ Hyett's post, as it is not available anymore:
这是 PJ Hyett 帖子的副本,因为它不再可用:
Git Isn't Hard
Nov 23, 2008
When we tell people why they should use Git over Subversion, the go-to line is, “Git does Subversion better than Subversion, but it does a lot more than that.”
The “lot more” is comprised of a bunch of stuff that makes Git really shine, but it can be pretty overwhelming for those coming from other SCM's like Subversion.
That said, there's nothing stopping you from using Git just like you use Subversion while you're making the transition.
Assuming you've installed the necessary software and have a remote repository somewhere, this is how you would grab the code and push your changes back with Subversion:
$ svn checkout svn://foo.googlecode.com/svn/trunk foo
# make your changes
$ svn commit -m "my first commit"
And how would you do it in Git:
$ git clone [email protected]:pjhyett/foo.git
# make your changes
$ git commit -a -m "my first commit"
$ git push
One more command to make it happen in Git. That extra command has large implications, but for the purposes of this post, that's all we're talking about, one extra command.
See, it really isn't that hard.
Update:I'd be remiss to not also mention that the equivalent of updating your local copy in Subversion compared to Git is
svn update
andgit pull
, respectively. Only one command in both cases.
Git并不难
2008 年 11 月 23 日
当我们告诉人们为什么他们应该使用 Git 而不是 Subversion 时,重点是,“Git 在 Subversion 上做得比 Subversion 好,但它做的远不止这些。”
“更多”由一堆让 Git 真正闪耀的东西组成,但对于那些来自其他 SCM(如 Subversion)的人来说,它可能会让人不知所措。
也就是说,没有什么能阻止您使用 Git,就像您在进行转换时使用 Subversion 一样。
假设您已经安装了必要的软件并且在某处有一个远程存储库,这就是您获取代码并使用 Subversion 推送您的更改的方式:
$ svn checkout svn://foo.googlecode.com/svn/trunk foo
# make your changes
$ svn commit -m "my first commit"
你会如何在 Git 中做到这一点:
$ git clone [email protected]:pjhyett/foo.git
# make your changes
$ git commit -a -m "my first commit"
$ git push
另一个命令可以在 Git 中实现。这个额外的命令有很大的影响,但就本文而言,这就是我们所谈论的,一个额外的命令。
看,真的没那么难。
更新:我没有提到与 Git 相比,在 Subversion 中更新本地副本的等价物分别是
svn update
和git pull
。在这两种情况下都只有一个命令。
回答by dbr
How to install Git
如何安装 Git
On Windows:
在 Windows 上:
Install msysgit
安装msysgit
There are several downloads:
有几个下载:
- Git:Use this unless you specifically need one of the other options below.
- PortableGit:Use this if you want to run Git on a PC without installing on that PC (e.g. running Git from a USB drive)
- msysGit:Use this if you want to develop Git itself. If you just want to use Git for yoursource code, but don't want to edit Git'ssource code, you don't need this.
- Git:除非您特别需要以下其他选项之一,否则请使用此选项。
- PortableGit:如果您想在 PC 上运行 Git 而无需在该 PC 上安装(例如从 USB 驱动器运行 Git),请使用此选项
- msysGit:如果你想自己开发 Git,请使用它。如果您只想将 Git 用于您的源代码,但不想编辑Git 的源代码,那么您不需要这个。
This also installs a Cygwin bash shell, so you can use the git
in a nicer shell (than cmd.exe), and also includes git-gui (accessible via git gui
command, or the Start > All Programs > Git
menu)
这还会安装 Cygwin bash shell,因此您可以git
在更好的 shell(比 cmd.exe)中使用它,并且还包括 git-gui(可通过git gui
命令或Start > All Programs > Git
菜单访问)
Mac OS X
Mac OS X
Use the git-osx-installer, or you can also install from source
使用git-osx-installer,或者您也可以从源代码安装
Via a package manager
通过包管理器
Install git
using your native package manager. For example, on Debian (or Ubuntu):
git
使用您的本机包管理器安装。例如,在 Debian(或 Ubuntu)上:
apt-get install git-core
Or on Mac OS X, via MacPorts:
或者在 Mac OS X 上,通过MacPorts:
sudo port install git-core+bash_completion+doc
…or fink:
……或者芬克:
fink install git
…or Homebrew:
...或自制:
brew install git
On Red Hat based distributions, such as Fedora:
在基于 Red Hat 的发行版上,例如 Fedora:
yum install git
In Cygwin the Git package can be found under the "devel" section
在 Cygwin 中,可以在“开发”部分下找到 Git 包
From source (Mac OS X/Linux/BSD/etc.)
来自源代码(Mac OS X/Linux/BSD/等)
In Mac OS X, if you have the Developer Tools installed, you can compile Git from source very easily. Download the latest version of Git as a .tar.bz
or .tar.gz
from http://git-scm.com/, and extract it (double click in Finder)
在 Mac OS X 中,如果您安装了 Developer Tools,则可以非常轻松地从源代码编译 Git。下载最新版本的Git的作为.tar.bz
或.tar.gz
从http://git-scm.com/,并提取它(在Finder中双击)
On Linux/BSD/etc. it should be much the same. For example, in Debian (and Ubuntu), you need to install the build-essential
package via apt
.
在 Linux/BSD/等上。应该大同小异。例如,在 Debian(和 Ubuntu)中,您需要build-essential
通过apt
.
Then in a Terminal, cd
to where you extracted the files (Running cd ~/Downloads/git*/
should work), and then run..
然后在终端中,cd
将文件解压缩到的位置(运行cd ~/Downloads/git*/
应该可以工作),然后运行..
./configure && make && sudo make install
This will install Git into the default place (/usr/local
- so git
will be in /usr/local/bin/git
)
这会将 Git 安装到默认位置(/usr/local
- 所以git
将在/usr/local/bin/git
)
It will prompt you to enter your password (for sudo
), this is so it can write to the /usr/local/
directory, which can only be accessed by the "root" user so sudo is required!
它会提示您输入密码(for sudo
),这样它就可以写入/usr/local/
目录,该目录只能由“root”用户访问,因此需要 sudo!
If you with to install it somewhere separate (so Git's files aren't mixed in with other tools), use --prefix
with the configure command:
如果要将其安装在单独的位置(因此 Git 的文件不会与其他工具混合),请--prefix
与 configure 命令一起使用:
./configure --prefix=/usr/local/gitpath
make
sudo make install
This will install the git
binary into /usr/local/bin/gitpath/bin/git
- so you don't have to type that every time you, you should add into your $PATH
by adding the following line into your ~/.profile
:
这会将git
二进制文件安装到/usr/local/bin/gitpath/bin/git
- 所以你不必每次都输入,你应该$PATH
通过将以下行添加到你的~/.profile
:
export PATH="${PATH}:/usr/local/bin/gitpath/bin/"
If you do not have sudo access, you can use --prefix=/Users/myusername/bin
and install into your home directory. Remember to add ~/bin/
to $PATH
如果您没有 sudo 访问权限,则可以使用--prefix=/Users/myusername/bin
并安装到您的主目录中。记得添加~/bin/
到$PATH
The script x-git-update-to-latest-versionautomates a lot of this:
脚本x-git-update-to-latest-version自动化了很多:
This script updates my local clone of the git repo (localy at
~/work/track/git
), and then configures, installs (at/usr/local/git
-git describe
) and updates the/usr/local/git
symlink.This way, I can have
/usr/local/git/bin
in myPATH
and I'm always using the latest version.The latest version of this script also installs the man pages. You need to tweak your
MANPATH
to include the/usr/local/git/share/man
directory.
此脚本更新我的 git 存储库的本地克隆(localy at
~/work/track/git
),然后配置、安装(at/usr/local/git
-git describe
)并更新/usr/local/git
符号链接。这样,我就可以使用
/usr/local/git/bin
我的PATH
并且始终使用最新版本。此脚本的最新版本还会安装手册页。您需要调整您的
MANPATH
以包含/usr/local/git/share/man
目录。
回答by Dean Rather
Git Reset
Git重置
Say you make a pull, merge it into your code, and decide you don't like it. Use git-log, or tig, and find the hash of wherever you want to go back to (probably your last commit before the pull/merge) copy the hash, and do:
假设你做了一个 pull,把它合并到你的代码中,然后决定你不喜欢它。使用 git-log 或 tig,找到你想要返回的任何地方的哈希值(可能是你在拉/合并之前的最后一次提交)复制哈希值,然后执行:
# Revert to a previous commit by hash:
git-reset --hard <hash>
Instead of the hash, you can use HEAD^as a shortcut for the previous commit.
您可以使用HEAD^作为上一次提交的快捷方式,而不是散列。
# Revert to previous commit:
git-reset --hard HEAD^
回答by Asgeir S. Nilsen
How do you set up a shared team repository?
您如何设置共享团队存储库?
How to set up a normalrepository is described here-- but how do you set up a team repository that everybody can pull and push from and to?
这里描述了如何设置一个普通的存储库——但是你如何设置一个每个人都可以从中拉取和推入的团队存储库?
Using a shared NFS file system
使用共享 NFS 文件系统
Assuming your team already has for instance a shared group membership that can be used.
例如,假设您的团队已经拥有可以使用的共享组成员身份。
mkdir /your/share/folder/project.git
cd /your/share/folder/project.git
newgrp yourteamgroup # if necessary
git init --bare --shared
To start using this repository the easiest thing to do is start from a local repository you already have been using:
要开始使用此存储库,最简单的方法是从您已经使用的本地存储库开始:
cd your/local/workspace/project
git remote add origin /your/share/folder/project.git
git push origin master
Others can now clone this and start working:
其他人现在可以克隆它并开始工作:
cd your/local/workspace
git clone /your/share/folder/project.git
Using SSH
使用 SSH
Set up a user account on the target server. Whether you use an account with no password, an account with a password, or use authorized_keys
really depend on your required level of security. Take a look at Configuring Git over SSHfor some more information.
在目标服务器上设置用户帐户。无论您使用没有密码的帐户、有密码的帐户还是使用,authorized_keys
实际上取决于您所需的安全级别。有关更多信息,请查看通过 SSH 配置 Git。
If all developers use the same account for accessing this shared repository, you do not need to use the --shared
option as above.
如果所有开发者使用同一个账号访问这个共享仓库,则不需要使用上述--shared
选项。
After initing the repository in the same way as above, you do the initial push like this:
以与上述相同的方式初始化存储库后,您可以像这样进行初始推送:
cd your/local/workspace/project
git remote add origin user@server:/path/to/project.git
git push origin master
See the similarity with the above? The only thing that might happen in addition is SSH asking for a password if the account has a password. If you get this prompt on an account without a password the SSH server probably has disabled PermitEmptyPasswords
.
看到与上面的相似之处了吗?此外,唯一可能发生的事情是,如果帐户有密码,SSH 会要求输入密码。如果您在没有密码的帐户上收到此提示,则 SSH 服务器可能已禁用PermitEmptyPasswords
。
Cloning now looks like this:
克隆现在看起来像这样:
cd your/local/workspace
git clone user@server:/path/to/project.git