主頁 > 移動端開發 > .gitignore中指定的檔案是否默認推送(上傳)到遠程倉庫?

.gitignore中指定的檔案是否默認推送(上傳)到遠程倉庫?

2022-01-08 08:15:41 移動端開發

在我的本地倉庫中,我有不希望提交或上傳到遠程倉庫的大檔案。如果我將這些檔案添加到 .gitignore,它們是否仍會通過git push命令上傳到遠程倉庫

uj5u.com熱心網友回復:

git push命令發送提交,而不是檔案。然后每個提交包含每個檔案(它包含;見下文)。因此,要回答您提出的問題——是否git push上傳列出的任何檔案.gitignore——您必須檢查提交以查看它們是否包含這些檔案。

這個名字.gitignore本質上是錯誤的:它不會讓 Git忽略檔案。問題是,一個正確的名稱會非常笨拙以至于無法使用:它可能類似于.git-do-not-auto-add-these-files-if-they-are-untracked-and-if-they-are-untracked-do-not-complain-about-them-being-untracked-when-I-run-git-status. 它還有一個效果,雖然很少見,但也很重要:它允許 Git在特定情況下破壞這些檔案。所以一個完全正確的名字會更糟。但這兩個都是關于未跟蹤檔案的,是兩個常見的。

那么:首先,關于未跟蹤檔案的所有這些東西是什么是什么讓檔案“未被跟蹤”?要回答這個問題,我們必須從 Git 存在的原因開始,那就是提交。

Git 是關于提交的

那些剛接觸 Git 的人通常認為它與檔案有關。它不是:它是關于提交的。提交確實包含檔案,但 Git 通常一次處理整個提交。或者,如果他們不認為 Git 是關于檔案的,那么初學者通常會認為 Git 是關于分支的,但事實并非如此:它仍然是關于提交的。分支——或者更準確地說,分支名稱——很重要,因為它們幫助我們(和 Git)找到提交。真正重要的是提交如果您打算使用 Git,您必須了解提交是什么以及為您做什么。

每個提交:

  • 有編號。提交的“數量”很大、丑陋且看起來很隨機。它實際上是一個以十六進制表示的通用唯一 ID ,例如dcc0cd074f0c639a0df20461a301af6d45bd582e. 該數字一旦分配給提交,就意味著提交,而不是任何其他提交。這就是兩個不同的 Git 存盤庫如何決定它們是否具有相同的提交:它們要么有編號,在這種情況下他們有那個提交,要么一個有,一個沒有,然后一個沒有它的需要從那個做的人那里得到提交。

  • 完全只讀。所有 Git 內部物件都是這樣的——使編號方案起作用是必要的。

  • 存盤 Git 在您或任何人進行提交時“知道”的每個檔案的完整存檔。提交中的檔案特殊的、只讀的、僅 Git 的、壓縮的和去重的形式間接存盤,以便兩個不同的檔案具有相同的內容,或者兩個提交具有相同的檔案具有相同的內容,只存盤一次檔案因此,雖然每個提交都有每個檔案的完整存檔,但所有提交也共享所有檔案,因此存盤庫不會變得非常臃腫。

  • 還存盤一些元資料,或有關提交本身的資訊。這包括提交人的姓名和電子郵件地址。它包括一些日期和時間戳。它包括您想要放入的任何提交日志訊息。而且,對于 Git 自己的內部操作而言至關重要的是,每個提交都存盤了一個先前提交哈希 ID串列這個串列通常只有一個條目:這意味著這個提交緊跟在它列出的提交之后。

正是這個父級串列,將向后提交的字串組合在一起,形成了存盤庫中的真正分支資訊。舉例來說,假設我們有提交該串兩端用哈希在一個我們稱之為H(用于H灰)。CommitH存盤一些較早提交的哈希 ID。我們說這H 指向這個較早的提交:

            <-H

如果我們呼叫較早的 commitG并將其繪制進去,我們有:

        <-G <-H

當然G也指向一個更早的提交,它再次指向后,依此類推:

... <-F <-G <-H

那是一個“分支”。找到這個分支,Git 需要知道 commit 的實際哈希 ID H,我們將其粘貼在分支名稱中,并說該名稱指向 H

...--F--G--H   <-- main

如果我們有多個分支名稱,每個名稱只指向一個提交:

          I--J   <-- feature-1
         /
...--G--H   <-- main
         \
          K--L   <-- feature-2

現在有三個“最后”提交:H最后提交上main和以前一樣,但是J是最后一次提交的feature-1,并且L是最后一次提交的feature-2請注意,提交H都在所有三個分支上(這是 Git 特有的;大多數版本控制系統都不是這樣作業的)。

如果提交中的檔案是只讀的且僅限 Git,我們如何使用它們?

The fact is that each commit is read-only, and the files inside the commit can only be read by Git itself. So before we can use a commit, we have to have Git extract the commit. We do this with git checkout or git switch: we pick some commit, by hash ID like H or by name like main which provides Git with hash ID H, and say extract that commit:

git switch main

Git will read all the files from commit H and expand them into ordinary everyday files. Git puts these files in a work area, which Git calls our working tree or work-tree.

That, then, answers the question about how we get work done: we use the working tree copies of the files. These files are not in Git! They came out of Git—at least, the initial ones did—but they are not in Git at this point.

Git's index or staging area

After we work on some file in the working tree, we might want Git to use that file—plus all the other files that we didn't change—to make a new commit. In other version control systems, we would generally run their "make commit" verb:

hg commit

for instance. They would figure out which files we changed and make the new commit. Git, alas, does not make it this easy. Instead, Git demands that we run:

git add updated-file
git commit

The first command—the git add step—tells Git: Read the working tree file, compress it into your internal Gitty format with de-duplication, and insert that into your index / staging-area, ready to be committed.

The secret here—it's not really a secret, but it's not always advertised or taught correctly—is that Git already has every existing checked-out file in its index / staging-area. This thing—the thing Git calls either the index or the staging area, depending on which bit of Git documentation is doing this calling—holds in it your proposed next commit.

When you first switch to some particular commit, Git removes from your working tree all the checked-out files that came out of whatever commit you were using, as recorded in its index. It also removes all the checked-out files from its index. Then it installs, into its index and into your working tree, all the files that go with the new commit you want to use. That is, if we did:

git switch main

we had all the files from H, but if we then decided to look at feature-1 instead and ran:

git switch feature-1

Git removed all the files from H and replaced them with the files from J, which is the commit to which the name feature-1 points.

There are a couple of important things to know about this remove-and-replace step:

  • First, Git only removes-and-replaces any files it has to. Git is already de-duplicating files, so it knows which files in H are the same in J. For any file that's the same in both commits, it can skip the R(emove)-and-R(eplace) job. For files that are in H but not in J, it has to do the first R—the remove—without a replace, and for files that are in J but not H, it has to just replace.

  • Second, Git only does an R-and-R for the files it knows about. But what are those files? That's where the index comes in.

Because your working tree is a regular directory (or folder, if you prefer that term) with regular everyday files in it, you can create files that Git doesn't know about. These are your untracked files. That gives us the definition of an "untracked" file: an untracked file is one that is not in Git's index right now. This is extremely important, so let's repeat it.

An untracked file is a file that is not in Git's index right now

This is the key to making new commits, and also the key to .gitignore. Files that are in Git's index right now are tracked. Files that are not in Git's index right now are untracked. The "right now" part is important, because:

  • git switch and git checkout fill in the index; but
  • git add reads a working tree file and copies it into the index; and
  • git rm removes a file from both the working tree and the index.

This gives you several ways to change what's in the index / staging-area. (There are more ways, such as git restore, but we'll just cover these three for now.) Using git rm --cached, you can remove a file from Git's index without removing it from the working tree, too, so it's easy to take a file from tracked to untracked with git rm --cached, or from untracked to tracked with git add.

Git builds new commits from whatever is in the index right now

When you run:

git commit

Git makes a snapshot of all the files that are in the index right then, as of the form they have in the index at that time. So if you:

git switch main
<modify one file - say, README.txt>
git add file
<modify the file again>

you currently have three versions of README.txt: there's one unchangeable one in the current commit, a second one in Git's index, and a third one in your working tree. If you run git commit, it's that second one—the one in Git's index—that goes in the new commit.

(Note that git commit -a is roughly equivalent to git add -u && git commit. That is, the -a option merely does an update-add of all the files that are already in Git's index. So truly-new files require a separate git add step.)

This is where .gitignore comes in

You can explicitly run:

git add some-file

or, if you like, you can run an en-masse "add everything here" with:

git add .

Both operations tell Git to add a file; the second one tells Git to add all the files from the current directory. But some files should not be put in new commits, and it would be painful to force you to git add individual files instead of letting you use git add . to just make Git figure things out.

Besides this, the git status command will run two diffs:

  • The first diff compares the HEAD (current) commit to Git's index. For each file that is the same, Git says nothing at all. For each file that is different—including newly added or removed—Git tells you about it, saying staged for commit. That's because whatever is in the index will be in the next commit, and if it's different, that's interesting. If you have a big project, with thousands of files, you probably don't care that dull/1 through dull/999 are all unchanged; what you care about is that important.code is changed. This git status diff will tell you that.

  • The second diff compares the index to your working tree. For each file that is the same, Git says nothing. For files that are different, Git tells you about them, saying not staged for commit. But new files—files that aren't in Git's index, but are in your working tree—are separated out. Git tells you about them not as "new" but rather as untracked.

To prevent git status from being uselessly noisy about 10,000 untracked files that should be untracked, and to make git add . useful by not adding the 10,000 files, Git will read the poorly-named file .gitignore. These are the files that Git will (a) shut up about, and (b) not add even though you said git add ..

This has no effect on files that are already in Git's index. If the file is in Git's index, it is tracked and it will be in the next commit. Listing a file in .gitignore doesn't make it untracked and does not make Git ignore it: it just means that if it is untracked, Git won't complain about it and won't auto-add it.1


1In fact, if you explicitly try to add it with git add listed-ignored-file, Git will tell you that it's both currently untracked and listed in .gitignore, but—if you haven't disabled the hint—that you can override this with git add -f. Some people like to use this trick to ignore "everything" (with * in a .gitignore, for instance), and then force-add the few files that they don't want to ignore. I personally don't like to do this, but it does work. Just note that if you do use this trick, git rm --cached -r . && git add . will do bad things.


Diagnosing whether a file is "ignored"

Suppose you have some repository in which someone, at some point, said to ignore *.zorg files. Maybe that was the right thing to do at that time, even. But now you do need to commit some or all of the files in which you store your bad guys, so you don't want *.zorg ignored.

You run ls and you see:

jean-baptiste.emanuel.zorg

You run git status and it doesn't list jean-baptiste.emanuel.zorg.

Is the file unchanged, or is it changed-but-ignored, and not present in Git's index right now?

You can run:

git add jean-baptiste.emanuel.zorg

If it says nothing, the file was added; git status will now tell you whether it's changed. If it says that the file is explicitly ignored but you can use -f to override, you can git add -f jean-baptiste.emanuel.zorg to get it added.

您還可以運行:

git ls-files jean-baptiste.emanuel.zorg

如果這什么都沒,則忽略該檔案。或者你可以運行:

git check-ignore jean-baptiste.emanuel.zorg

(也考慮添加-v)。如果這沒有說明,則不會忽略該檔案,可能是因為它已被強制添加。

uj5u.com熱心網友回復:

如果在 中添加正確的檔案路徑,.gitignore則使用時不會添加檔案git add .,鍵入時不會推送git push

這么簡短的回答,不

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

標籤:

上一篇:當我將我的存盤庫從我的mac推送到Github時,什么也沒有發生

下一篇:為什么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)

熱門瀏覽
  • 【從零開始擼一個App】Dagger2

    Dagger2是一個IOC框架,一般用于Android平臺,第一次接觸的朋友,一定會被搞得暈頭轉向。它延續了Java平臺Spring框架代碼碎片化,注解滿天飛的傳統。嘗試將各處代碼片段串聯起來,理清思緒,真不是件容易的事。更不用說還有各版本細微的差別。 與Spring不同的是,Spring是通過反射 ......

    uj5u.com 2020-09-10 06:57:59 more
  • Flutter Weekly Issue 66

    新聞 Flutter 季度調研結果分享 教程 Flutter+FaaS一體化任務編排的思考與設計 詳解Dart中如何通過注解生成代碼 GitHub 用對了嗎?Flutter 團隊分享如何管理大型開源專案 插件 flutter-bubble-tab-indicator A Flutter librar ......

    uj5u.com 2020-09-10 06:58:52 more
  • Proguard 常用規則

    介紹 Proguard 入口,如何查看輸出,如何使用 keep 設定入口以及使用實體,如何配置壓縮,混淆,校驗等規則。

    ......

    uj5u.com 2020-09-10 06:59:00 more
  • Android 開發技術周報 Issue#292

    新聞 Android即將獲得類AirDrop功能:可向附近設備快速分享檔案 谷歌為安卓檔案管理應用引入可安全隱藏資料的Safe Folder功能 Android TV新主界面將顯示電影、電視節目和應用推薦內容 泄露的Android檔案暗示了傳說中的谷歌Pixel 5a與折疊屏新機 谷歌發布Andro ......

    uj5u.com 2020-09-10 07:00:37 more
  • AutoFitTextureView Error inflating class

    報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......

    uj5u.com 2020-09-10 07:00:41 more
  • 根據Uri,Cursor沒有獲取到對應的屬性

    Android: 背景:呼叫攝像頭,拍攝視頻,指定保存的地址,但是回傳的Cursor檔案,只有名稱和大小的屬性,沒有其他諸如時長,連ID屬性都沒有 使用 cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATIO ......

    uj5u.com 2020-09-10 07:00:44 more
  • Android連載29-持久化技術

    一、持久化技術 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲。 二、檔案存盤 最基本存盤方式, ......

    uj5u.com 2020-09-10 07:00:47 more
  • Android Camera2Video整合到自己專案里

    背景: Android專案里呼叫攝像頭拍攝視頻,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后來因專案需要,改成了camera2 1.Camera2Video 官方demo有點問題,下載后,不能直接整合到專案 問題1.多次拍攝視頻崩潰 問題2.雙擊record按鈕, ......

    uj5u.com 2020-09-10 07:00:50 more
  • Android 開發技術周報 Issue#293

    新聞 谷歌為Android TV開發者提供多種新功能 Android 11將自動填表功能整合到鍵盤輸入建議中 谷歌宣布Android Auto即將支持更多的導航和數字停車應用 谷歌Pixel 5只有XL版本 搭載驍龍765G且將比Pixel 4更便宜 [圖]Wear OS將迎來重磅更新:應用啟動時間 ......

    uj5u.com 2020-09-10 07:01:38 more
  • 海豚星空掃碼投屏 Android 接收端 SDK 集成 六步驟

    掃碼投屏,開放網路,獨占設備,不需要額外下載軟體,微信掃碼,發現設備。支持標準DLNA協議,支持倍速播放。視頻,音頻,圖片投屏。好點意思。還支持自定義基于 DLNA 擴展的操作動作。好像要收費,沒體驗。 這里簡單記錄一下集成程序。 一 跟目錄的build.gradle添加私有mevan倉庫 mave ......

    uj5u.com 2020-09-10 07:01:43 more
最新发布
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:40:31 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:40:11 more
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:39:36 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:39:13 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:16:23 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:16:15 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:15:46 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:14:53 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:14:08 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:08:34 more