主頁 > 區塊鏈 > gitcheckout有什么作用?

gitcheckout有什么作用?

2021-11-07 10:12:12 區塊鏈

我認為該git checkout命令僅更新了作業樹中的檔案。事實上,手冊頁是這樣寫的:

git-checkout - 切換分支或恢復作業樹檔案

但是,我剛剛運行了這個命令:

git checkout 11cb5b6 -- hello.txt

并且,除了更新我的作業樹副本之外,這個命令還更新了我的索引。在命令之前,git status給了我一個干凈的結果:

nothing to commit, working tree clean

但緊跟在 之后checkout,它寫著:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   hello.txt

即作業樹檔案已更新和暫存。我錯過了什么?

uj5u.com熱心網友回復:

當您執行git checkout特定分支或提交時,臨時區域作業目錄都會 更新因此,該版本位于 head 的父級中,并將其復制到暫存區和作業目錄中。hello.txt

如果您沒有指定提交或分支,則 hello.txt 的內容將從暫存區復制到作業目錄。暫存區本身沒有改變。

uj5u.com熱心網友回復:

git checkout命令是非常復雜的它是如此復雜以至于有人最終在 Git 2.23 中將其拆分為兩個單獨的命令:

  • git switch, 執行“選擇其他分支或提交簽出”操作;
  • git restore,它執行“更新索引和/或作業樹中的某些檔案”操作。

這還沒有提到幾種額外的操作模式(git checkout -m例如),1但至少將所有“恢復檔案”選項分開,其中有很多。

git checkout正如shrey deshwal 指出的那樣您正在使用“恢復檔案”模式,此操作將:

  • 寫入您的作業樹(始終);
  • 寫入您的索引/暫存區(有時)。

使用git restore代替 時git checkout可以使用-S(staging area) 和-W(working tree) 選項控制更新哪些索引/暫存區和作業樹檔案這是不可能的git checkoutgit checkout總是寫入作業樹,如果您指定提交或樹物件作為要寫入作業樹的檔案的源,也會寫入索引/暫存區

如果您有 Git 2.23 或更高版本,請使用它git restore來執行此操作:它的操作不那么混亂且更直接。--source為檔案指定,或讓它默認使用索引:

git restore --source 11cb5b6 -- hello.txt

這將寫入作業樹(僅)。或者,添加-S和/或-W寫入暫存區(索引)-S,和/或作業樹-W

git restore --source 11cb5b6 -SW -- hello.txt

這將寫入暫存區和作業樹(因為-S-W都已給出)。

相比之下,git checkout -- file使源不那么明顯(它與 中的相同git restore,但不那么明顯)并且讓您無法選擇目標(總是-W,但-S如果源是提交或樹,則會被添加)。git restore命令還正確記錄了--overlayvs--no-overlay模式。此選項僅適用于“還原檔案”模式git checkout(現在已記錄在案,但尚不清楚它是否僅適用于此模式!)。


1-m選項git checkout

  • 重新創建合并沖突,或
  • 在結帳期間執行合并操作,就好像您運行了一系列相當復雜的 Git 命令,這些命令以您在目標分支上結束,然后與作業樹中未提交的代碼合并。

這第二個操作有點危險:正如檔案現在指出的那樣,

使用 切換分支時--merge,分階段的更改可能會丟失。

第一個操作愉快地破壞了您在檔案的作業樹副本中開始的任何合并作業。所以git checkout -m總是“危險”的git restore方式:它會在不詢問的情況下清除未提交的作業。我有點希望這些沒有留在git switch命令中,但它們是。


僅當上述內容仍然沒有意義時才閱讀:

If this stuff still doesn't come together, you're probably missing out on a key concept in Git: how the index / staging-area really works.

A Git repository is, to a large extent, just a big database of commits. What you do with this repository is add more commits. Each commit itself is:

  • Numbered. Each commit has a big, ugly, incomprehensible hash ID number such as e9e5ba39a78c8f5057262d49e261b42a8660d5b9 (often abbreviated, e.g., e9e5ba3). These appear random, though in fact they're entirely non-random.

  • Storage: each commit stores two things:

    • A commit has a full snapshot of every file. Commits don't store changes, so when Git shows you changes, it's really doing a git diff of two snapshots.

    • A commit also stores some metadata, or information about the commit itself. This includes things like the name and email address of the author of the commit (from user.name and user.email). It includes some date-and-time stamps. It includes a log message, which git log or git show will show before any diffs. And, crucially for Git's internal operation—though we won't cover any of the details here—each commit stores a list of previous commit hash IDs. Most commits just store one such hash ID, which is the "parent" of the commit. That's how Git finds the previous commit, so that it can show you what changed.

All of this stuff inside the commit is completely, totally, 100% read-only: no part of any commit can ever be changed once it's made.2 But this leaves us with a dilemma: if no part of any commit can ever change, how can we get any new work done?

Git's answer is the same as the answer is other version control systems: sure, the commits are read-only forever, but you don't do work with the committed files. We copy the files out of the commit, into a work area. You work on / with those files. That area is your working tree or work-tree. The copied-out files are ordinary read/write files, that your computer can do ordinary work on or with.

So far, none of this is particularly bizarre or incomprehensible. A commit is like an archive of files, like a tar or rar file made out of other files, but with special Gitty features like metadata and a weird random-looking number. We use git checkout or git switch to pick one: Git extracts the files, and now we can work on them.

But here's where Git gets weird. If you've used other version control systems, you are probably used to this idea: you work on the files and then you tell the VCS to commit them and it does. That would be simple, so Git doesn't do that.

When Git goes to build a new commit, Git does not use your files at all! Instead, Git uses a secret extra copy. Only it's not actually secret, and it's not usually a copy either. What it is, is hidden. This extra "copy" of each file is in what Git calls by three different names:

  • The index: a meaningless term. Meaningless is sometimes good, because then there's no preconceived notion to push out of the way for some weird technical reason. But it makes it a bit hard to remember.

  • The staging area: this is how you use the index, so it makes sense. But this obscures the technical details, which do matter. You need to be aware of them.

  • The cache: this is the worst name of all, because it's how Git itself sometimes uses the index, but not how you use it, and doesn't cover all the ways that Git uses the index. This term is mostly defunct, except that it appears in flags like git rm --cached or git diff --cached.

Sometimes the --cached flag has --staged as a synonym: git diff --staged does exactly the same thing as git diff --cached. Sometimes they don't: git rm --staged rejects the --staged entirely. Oddly, git restore has only --staged. Getting rid of --cached entirely might be a good direction; maybe Git will eventually do that. But in any case, you need to know all three names, as "the index" appears in various places. In particular, the index has a special role during conflicted merges, and it determines whether files are tracked or untracked. We won't go into this level of detail here; we'll only talk about the index as it pertains to making new commits.

When you run git commit, instead of Git reading from your working tree to find out which files you've changed, Git simply packages up the files that are in its index at this time.3 To make that work, the initial git checkout or git switch step first fills in Git's index.

What this means is that after you've checked out some commit to work on it, you have three "copies" of each file:

  • There is a read-only, Git-ified, compressed-and-de-duplicated special format version of the file in the commit.
  • There is another "copy" (see below for the reason for the quotes here) of that file in Git's index / staging-area.
  • Last, there's a usable copy (no quotes this time) of the file in your working tree. That's the one you can see and edit.

When Git stores a file permanently in a commit, it:

  • compresses the file, so that it takes less space;
  • sometimes, super-compresses the file (this happens late in the game);
  • always Git-ifies the file: it's not stored as a file (with a name and other OS attributes), but rather as an internal Git blob object (nothing but a hash ID name, no modes, etc.; the names and modes are stored in additional Git objects called tree objects).

In other words, these files are Git-ified and put into a database, rather than kept as regular files. Whenever Git does this, it automatically de-duplicates the content. So even though every commit stores every file, the repository doesn't bloat up out of control when you have millions of commits, as many of the commits have the same copy of some file, and those are all shared. Instead of a copy of the file, we get a "copy" of the file, inside the commit: hence the quotes.

The index stores pre-Git-ified, pre-compressed and pre-de-duplicated "copies" in this same way that commits do. The git add command therefore:

  1. reads the working tree version of the file;
  2. compresses it and otherwise Git-ifies it: this produces an internal hash ID for de-duplication purposes;
  3. decides whether the file is a duplicate, or not.

If the file is a duplicate, git add throws out the copy it just made: we don't need that one, we have one in the repository already. Git updates its index with the duplicate, and the file is now ready to be committed, all stored in Git's index.

If the file isn't a duplicate, git add takes the now-prepared compressed file and readies it to go into the database, sort of temporarily added.4 And now Git has a copy that is ready to be committed, stored in its index.

So:

  • we started with some file path/to/file.ext in the index, ready to be committed;
  • then we Git-ified the (real, OS-level) file file.ext in folder to in folder path with git add;
  • then git add updated the index "copy" if needed, and now we have path/to/file in the index, ready to be committed.

Hence, before git add, the index contained the proposed next commit. After git add, the index still contains the proposed next commit. What git add just did was update the proposed next commit.

If you add an all new file, the same sequence happens: git add reads and compresses the ordinary OS-level file, figures out whether the contents are a duplicate or not—maybe you've added world.txt which also contains hello world, which is a duplicate of existing hello.txt for instance—and git add has updated the proposed next commit so that new file world.txt is listed there too.

In all cases, Git always has the proposed next commit set up in its index.5 Running git commit means use the proposed next commit as it is now, which is why you can partially stage stuff: what you're doing is adding, to the index, copies of some files that do match the working tree copies, and copies of other files that don't match the working tree copies. Since the index holds its own copies (or "copies" due to pre-de-duplication), this means there are always three "active copies" of each file:

  • There is the HEAD (or current commit) copy. This one is Git-ified and can't be changed because it is in a commit.

  • There is the index copy. This one is Git-ified, but can be changed, because it's only a proposed commit, not a real one yet.

  • Finally, there's the working tree copy. This is the only one you can see and work on / with.

You use and modify the working tree copies. You use git add or git rm to create or remove the index copies, and then you use git commit to turn the proposed next commit into an actual commit.


2This means that git commit --amend is a lie. It doesn't amend the commit, it makes a new and improved replacement. The old commit still exists! This is true of git rebase as well. Things that, in Git, seem to change commits, don't really change them at all. You can tell by saving and comparing those hash IDs—but humans normally just sort of bleep right over them, which is a good thing: it lets us replace bad commits with better ones, without humans noticing that we did that.

3The git commit command offers a flag, -a, that means in effect: first, run git add -u, then run git commit. There are a bunch of subtleties about this, but the key item to note here is that this runs git add -u. The -u option will only update tracked files, so you can't use this for new files. Git therefore forces you to learn about git add.

4Git in fact just adds the object right away, and throws it away later if appropriate, but you can view this as "temporarily added" if you prefer.

5When you're in the middle of a conflicted merge, Git knows that you're in the middle of a conflicted merge because there are some index entries that have a higher "staging number" than staging-number-zero. In this mode, Git won't make a new commit from the index at all, so one can argue that in this mode, the index doesn't hold the proposed next commit.

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351152.html

標籤:混帐

上一篇:如何從命令列使用BeyondCompare,但在VS中使用VisualStudiodiff?

下一篇:即使我洗掉它,Git分支也總是神奇地出現

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • JAVA使用 web3j 進行token轉賬

    最近新學習了下區塊鏈這方面的知識,所學不多,給大家分享下。 # 1. 關于web3j web3j是一個高度模塊化,反應性,型別安全的Java和Android庫,用于與智能合約配合并與以太坊網路上的客戶端(節點)集成。 # 2. 準備作業 jdk版本1.8 引入maven <dependency> < ......

    uj5u.com 2020-09-10 03:03:06 more
  • 以太坊智能合約開發框架Truffle

    前言 部署智能合約有多種方式,命令列的瀏覽器的渠道都有,但往往跟我們程式員的風格不太相符,因為我們習慣了在IDE里寫了代碼然后打包運行看效果。 雖然現在IDE中已經存在了Solidity插件,可以撰寫智能合約,但是部署智能合約卻要另走他路,沒辦法進行一個快捷的部署與測驗。 如果團隊管理的區塊節點多、 ......

    uj5u.com 2020-09-10 03:03:12 more
  • 谷歌二次驗證碼成為區塊鏈專用安全碼,你怎么看?

    前言 谷歌身份驗證器,前些年大家都比較陌生,但隨著國內互聯網安全的加強,它越來越多地出現在大家的視野中。 比較廣泛接觸的人群是國際3A游戲愛好者,游戲盜號現象嚴重+國外賬號安全應用廣泛,這類游戲一般都會要求用戶系結名為“兩步驗證”、“雙重驗證”等,平臺一般都推薦用谷歌身份驗證器。 后來區塊鏈業務風靡 ......

    uj5u.com 2020-09-10 03:03:17 more
  • 密碼學DAY1

    目錄 ##1.1 密碼學基本概念 密碼在我們的生活中有著重要的作用,那么密碼究竟來自何方,為何會產生呢? 密碼學是網路安全、資訊安全、區塊鏈等產品的基礎,常見的非對稱加密、對稱加密、散列函式等,都屬于密碼學范疇。 密碼學有數千年的歷史,從最開始的替換法到如今的非對稱加密演算法,經歷了古典密碼學,近代密 ......

    uj5u.com 2020-09-10 03:03:50 more
  • 密碼學DAY1_02

    目錄 ##1.1 ASCII編碼 ASCII(American Standard Code for Information Interchange,美國資訊交換標準代碼)是基于拉丁字母的一套電腦編碼系統,主要用于顯示現代英語和其他西歐語言。它是現今最通用的單位元組編碼系統,并等同于國際標準ISO/IE ......

    uj5u.com 2020-09-10 03:04:50 more
  • 密碼學DAY2

    ##1.1 加密模式 加密模式:https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html ECB ECB : Electronic codebook, 電子密碼本. 需要加密的訊息按照塊密碼的塊大小被分為數個塊,并對每個塊進 ......

    uj5u.com 2020-09-10 03:05:42 more
  • NTP時鐘服務器的特點(京準電子)

    NTP時鐘服務器的特點(京準電子) NTP時鐘服務器的特點(京準電子) 京準電子官V——ahjzsz 首先對時間同步進行了背景介紹,然后討論了不同的時間同步網路技術,最后指出了建立全球或區域時間同步網存在的問題。 一、概 述 在通信領域,“同步”概念是指頻率的同步,即網路各個節點的時鐘頻率和相位同步 ......

    uj5u.com 2020-09-10 03:05:47 more
  • 標準化考場時鐘同步系統推進智能化校園建設

    標準化考場時鐘同步系統推進智能化校園建設 標準化考場時鐘同步系統推進智能化校園建設 安徽京準電子科技官微——ahjzsz 一、背景概述隨著教育事業的快速發展,學校建設如雨后春筍,隨之而來的學校教育、管理、安全方面的問題成了學校管理人員面臨的最大的挑戰,這些問題同時也是學生家長所擔心的。為了讓學生有更 ......

    uj5u.com 2020-09-10 03:05:51 more
  • 位元幣入門

    引言 位元幣基本結構 位元幣基礎知識 1)哈希演算法 2)非對稱加密技術 3)數字簽名 4)MerkleTree 5)哪有位元幣,有的是UTXO 6)位元幣挖礦與共識 7)區塊驗證(共識) 總結 引言 上一篇我們已經知道了什么是區塊鏈,此篇說一下區塊鏈的第一個應用——位元幣。其實先有位元幣,后有的區塊 ......

    uj5u.com 2020-09-10 03:06:15 more
  • 北斗對時服務器(北斗對時設備)電力系統應用

    北斗對時服務器(北斗對時設備)電力系統應用 北斗對時服務器(北斗對時設備)電力系統應用 京準電子科技官微(ahjzsz) 中國北斗衛星導航系統(英文名稱:BeiDou Navigation Satellite System,簡稱BDS),因為是目前世界范圍內唯一可以大面積提供免費定位服務的系統,所以 ......

    uj5u.com 2020-09-10 03:06:20 more
最新发布
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:46:47 more
  • Hyperledger Fabric 使用 CouchDB 和復雜智能合約開發

    在上個實驗中,我們已經實作了簡單智能合約實作及客戶端開發,但該實驗中智能合約只有基礎的增刪改查功能,且其中的資料管理功能與傳統 MySQL 比相差甚遠。本文將在前面實驗的基礎上,將 Hyperledger Fabric 的默認資料庫支持 LevelDB 改為 CouchDB 模式,以實作更復雜的資料... ......

    uj5u.com 2023-04-16 07:28:31 more
  • .NET Core 波場鏈離線簽名、廣播交易(發送 TRX和USDT)筆記

    Get Started NuGet You can run the following command to install the Tron.Wallet.Net in your project. PM> Install-Package Tron.Wallet.Net 配置 public reco ......

    uj5u.com 2023-04-14 08:08:00 more
  • DKP 黑客分析——不正確的代幣對比率計算

    概述: 2023 年 2 月 8 日,針對 DKP 協議的閃電貸攻擊導致該協議的用戶損失了 8 萬美元,因為 execute() 函式取決于 USDT-DKP 對中兩種代幣的余額比率。 智能合約黑客概述: 攻擊者的交易:0x0c850f,0x2d31 攻擊者地址:0xF38 利用合同:0xf34ad ......

    uj5u.com 2023-04-07 07:46:09 more
  • Defi開發簡介

    Defi開發簡介 介紹 Defi是去中心化金融的縮寫, 是一項旨在利用區塊鏈技術和智能合約創建更加開放,可訪問和透明的金融體系的運動. 這與傳統金融形成鮮明對比,傳統金融通常由少數大型銀行和金融機構控制 在Defi的世界里,用戶可以直接從他們的電腦或移動設備上訪問廣泛的金融服務,而不需要像銀行或者信 ......

    uj5u.com 2023-04-05 08:01:34 more
  • solidity簡單的ERC20代幣實作

    // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import "hardhat/console.sol"; //ERC20 同質化代幣,每個代幣的本質或性質都是相同 //ETH 是原生代幣,它不是ERC20代幣, ......

    uj5u.com 2023-03-21 07:56:29 more
  • solidity 參考型別修飾符memory、calldata與storage 常量修飾符C

    在solidity語言中 參考型別修飾符(參考型別為存盤空間不固定的數值型別) memory、calldata與storage,它們只能修飾參考型別變數,比如字串、陣列、位元組等... memory 適用于方法傳參、返參或在方法體內使用,使用完就會清除掉,釋放記憶體 calldata 僅適用于方法傳參 ......

    uj5u.com 2023-03-08 07:57:54 more
  • solidity注解標簽

    在solidity語言中 注釋符為// 注解符為/* 內容*/ 或者 是 ///內容 注解中含有這幾個標簽給予我們使用 @title 一個應該描述合約/介面的標題 contract, library, interface @author 作者的名字 contract, library, interf ......

    uj5u.com 2023-03-08 07:57:49 more
  • 評價指標:相似度、GAS消耗

    【代碼注釋自動生成方法綜述】 這些評測指標主要來自機器翻譯和文本總結等研究領域,可以評估候選文本(即基于代碼注釋自動方法而生成)和參考文本(即基于手工方式而生成)的相似度. BLEU指標^[^?88^^?^]^:其全稱是bilingual evaluation understudy.該指標是最早用于 ......

    uj5u.com 2023-02-23 07:27:39 more
  • 基于NOSTR協議的“公有制”版本的Twitter,去中心化社交軟體Damus

    最近,一個幽靈,Web3的幽靈,在網路游蕩,它叫Damus,這玩意詮釋了什么叫做病毒式營銷,滑稽的是,一個Web3產品卻在Web2的產品鏈上瘋狂傳銷,各方大佬紛紛為其背書,到底發生了什么?Damus的葫蘆里,賣的是什么藥? 注冊和簡單實用 很少有什么產品在用戶注冊環節會有什么噱頭,但Damus確實出 ......

    uj5u.com 2023-02-05 06:48:39 more