# git-update-ref
> 原文: [https://git-scm.com/docs/git-update-ref](https://git-scm.com/docs/git-update-ref)
## 名稱
git-update-ref - 安全地更新存儲在ref中的對象名
## 概要
```
git update-ref [-m <reason>] [--no-deref] (-d <ref> [<oldvalue>] | [--create-reflog] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
```
## 描述
給出兩個參數,存儲< newvalue>在< ref>中,可能取消引用符號引用。例如。 `git update-ref HEAD <newvalue>`將當前分支頭更新為新對象。
給出三個參數,存儲< newvalue>在< ref>中,在驗證< ref>的當前值之后,可能解除引用符號引用。匹配< oldvalue>。例如。 `git update-ref refs/heads/master <newvalue> <oldvalue>`將主分支頭更新為< newvalue>僅當其當前值為< oldvalue>時。您可以將40“0”或空字符串指定為< oldvalue>確保您創建的引用不存在。
它還允許“ref”文件作為指向另一個ref文件的符號指針,方法是從“ref:”的四字節頭文件序列開始。
更重要的是,它允許更新ref文件以遵循這些符號指針,無論它們是符號鏈接還是這些“常規文件符號引用”。它只跟隨**真實**符號鏈接,如果它們以“refs /”開頭:否則它只會嘗試讀取它們并將它們更新為常規文件(即它將允許文件系統跟隨它們,但會覆蓋它們符號鏈接到其他具有常規文件名的地方)。
如果給出了--no-deref,則< ref>本身被覆蓋,而不是遵循符號指針的結果。
一般來說,使用
```
git update-ref HEAD "$head"
```
應該是_很多_比做更安全
```
echo "$head" > "$GIT_DIR/HEAD"
```
從符合條件的符號鏈接**和**兩者都是錯誤檢查的立場。符號鏈接的“refs /”規則意味著指向樹“外部”的符號鏈接是安全的:它們將被用于讀取但不用于寫入(因此我們永遠不會通過ref符號鏈接寫入其他樹,如果您已通過創建符號鏈接樹復制了整個存檔。
使用`-d`標志,它將刪除命名的< ref>驗證后仍然包含< oldvalue>。
使用`--stdin`,update-ref從標準輸入讀取指令并一起執行所有修改。指定表單的命令:
```
update SP <ref> SP <newvalue> [SP <oldvalue>] LF
create SP <ref> SP <newvalue> LF
delete SP <ref> [SP <oldvalue>] LF
verify SP <ref> [SP <oldvalue>] LF
option SP <opt> LF
```
使用`--create-reflog`,update-ref將為每個ref創建一個reflog,即使通常不會創建一個。
引用包含空格的字段,就好像它們是C源代碼中的字符串一樣;即,被雙引號包圍并帶有反斜杠逃逸。使用40“0”字符或空字符串指定零值。要指定缺失值,請完全省略該值及其前面的SP。
或者,使用`-z`以NUL終止格式指定,而不引用:
```
update SP <ref> NUL <newvalue> NUL [<oldvalue>] NUL
create SP <ref> NUL <newvalue> NUL
delete SP <ref> NUL [<oldvalue>] NUL
verify SP <ref> NUL [<oldvalue>] NUL
option SP <opt> NUL
```
在此格式中,使用40“0”指定零值,并使用空字符串指定缺失值。
無論使用哪種格式,都可以以Git識別為對象名稱的任何形式指定值。任何其他格式的命令或重復的< ref>產生錯誤。命令含義是:
```
update
```
設置< ref>到< newvalue>在驗證< oldvalue>之后,如果給出。指定零< newvalue>確保更新后ref不存在和/或零< oldvalue>確保在更新之前ref不存在。
```
create
```
創建< ref>與< newvalue>在驗證它不存在之后。給定的< newvalue>可能不是零。
```
delete
```
刪除< ref>在驗證它與< oldvalue>存在之后,如果給出。如果給出,< oldvalue>可能不是零。
```
verify
```
驗證< ref>反對< oldvalue>但不要改變它。如果< oldvalue>零或缺少,ref必須不存在。
```
option
```
修改命名< ref>的下一個命令的行為。唯一有效的選項是`no-deref`,以避免取消引用符號引用。
如果可以同時使用匹配的< oldvalue>來鎖定所有< ref>,則執行所有修改。否則,不執行任何修改。注意,雖然每個人< ref>以原子方式更新或刪除,并發讀者仍可以看到修改的子集。
## 記錄更新
如果config參數“core.logAllRefUpdates”為true且ref為1,則為“refs / heads /”,“refs / remotes /”,“refs / notes /”或符號ref HEAD;或文件“$ GIT_DIR / logs /< ref>”然后存在`git update-ref`將一行添加到日志文件“$ GIT_DIR / logs /< ref>” (在創建日志名稱之前取消引用所有符號引用)描述ref值的更改。日志行的格式為:
```
oldsha1 SP newsha1 SP committer LF
```
其中“oldsha1”是先前存儲在< ref>中的40字符十六進制值,“newsha1”是< newvalue>的40字符十六進制值。 “committer”是標準Git committer ident格式的提交者姓名,電子郵件地址和日期。
可選地使用-m:
```
oldsha1 SP newsha1 SP committer TAB message LF
```
其中所有字段如上所述,“message”是提供給-m選項的值。
如果當前用戶無法創建新日志文件,附加到現有日志文件或沒有可用的提交者信息,則更新將失敗(不更改< ref>)。
## GIT
部分 [git [1]](https://git-scm.com/docs/git) 套件
- git
- git-config
- git-help
- git-init
- git-clone
- git-add
- git-status
- git-diff
- git-commit
- git-reset
- git-rm
- git-mv
- git-branch
- git-checkout
- git-merge
- git-mergetool
- git-log
- git-stash
- git-tag
- git-worktree
- git-fetch
- git-pull
- git-push
- git-remote
- git-submodule
- git-show
- git-log
- git-shortlog
- git-describe
- git-apply
- git-cherry-pick
- git-rebase
- git-revert
- git-bisect
- git-blame
- git-grep
- gitattributes
- giteveryday
- gitglossary
- githooks
- gitignore
- gitmodules
- gitrevisions
- gittutorial
- gitworkflows
- git-am
- git-format-patch
- git-send-email
- git-request-pull
- git-svn
- git-fast-import
- git-clean
- git-gc
- git-fsck
- git-reflog
- git-filter-branch
- git-instaweb
- git-archive
- git-bundle
- git-daemon
- git-update-server-info
- git-cat-file
- git-check-ignore
- git-checkout-index
- git-commit-tree
- git-count-objects
- git-diff-index
- git-for-each-ref
- git-hash-object
- git-ls-files
- git-merge-base
- git-read-tree
- git-rev-list
- git-rev-parse
- git-show-ref
- git-symbolic-ref
- git-update-index
- git-update-ref
- git-verify-pack
- git-write-tree