<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect2 SYSTEM "../../../dtd/dblite.dtd">

<sect2 lang="en" id="git-update-ref(1)">
    <title>git-update-ref(1)</title>
    <date>2024-07-02</date>
<revhistory><revision><date>2024-07-02</date></revision></revhistory>
<indexterm>
<primary>git-update-ref(1)</primary>
</indexterm>
<simplesect id="git-update-ref(1)__name">
<title>NAME</title>
<simpara>git-update-ref - Update the object name stored in a ref safely</simpara>
</simplesect>
<simplesect id="git-update-ref(1)__synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git update-ref</emphasis> [-m &lt;reason&gt;] [--no-deref] (-d &lt;ref&gt; [&lt;old-oid&gt;] | [--create-reflog] &lt;ref&gt; &lt;new-oid&gt; [&lt;old-oid&gt;] | --stdin [-z])</literallayout>
</blockquote>
</simplesect>
<simplesect id="git-update-ref(1)__description">
<title>DESCRIPTION</title>
<simpara>Given two arguments, stores the &lt;new-oid&gt; in the &lt;ref&gt;, possibly
dereferencing the symbolic refs.  E.g. <emphasis>git update-ref HEAD
&lt;new-oid&gt;</emphasis> updates the current branch head to the new object.</simpara>
<simpara>Given three arguments, stores the &lt;new-oid&gt; in the &lt;ref&gt;,
possibly dereferencing the symbolic refs, after verifying that
the current value of the &lt;ref&gt; matches &lt;old-oid&gt;.
E.g. <emphasis>git update-ref refs/heads/master &lt;new-oid&gt; &lt;old-oid&gt;</emphasis>
updates the master branch head to &lt;new-oid&gt; only if its current
value is &lt;old-oid&gt;.  You can specify 40 "0" or an empty string
as &lt;old-oid&gt; to make sure that the ref you are creating does
not exist.</simpara>
<simpara>It also allows a "ref" file to be a symbolic pointer to another
ref file by starting with the four-byte header sequence of
"ref:".</simpara>
<simpara>More importantly, it allows the update of a ref file to follow
these symbolic pointers, whether they are symlinks or these
"regular file symbolic refs".  It follows <emphasis role="strong">real</emphasis> symlinks only
if they start with "refs/": otherwise it will just try to read
them and update them as a regular file (i.e. it will allow the
filesystem to follow them, but will overwrite such a symlink to
somewhere else with a regular filename).</simpara>
<simpara>If --no-deref is given, &lt;ref&gt; itself is overwritten, rather than
the result of following the symbolic pointers.</simpara>
<simpara>In general, using</simpara>
<literallayout class="monospaced">git update-ref HEAD "$head"</literallayout>
<simpara>should be a <emphasis>lot</emphasis> safer than doing</simpara>
<literallayout class="monospaced">echo "$head" &gt; "$GIT_DIR/HEAD"</literallayout>
<simpara>both from a symlink following standpoint <emphasis role="strong">and</emphasis> an error checking
standpoint.  The "refs/" rule for symlinks means that symlinks
that point to "outside" the tree are safe: they'll be followed
for reading but not for writing (so we'll never write through a
ref symlink to some other tree, if you have copied a whole
archive by creating a symlink tree).</simpara>
<simpara>With <emphasis>-d</emphasis> flag, it deletes the named &lt;ref&gt; after verifying it
still contains &lt;old-oid&gt;.</simpara>
<simpara>With <emphasis>--stdin</emphasis>, update-ref reads instructions from standard input and
performs all modifications together.  Specify commands of the form:</simpara>
<literallayout class="monospaced">update SP &lt;ref&gt; SP &lt;new-oid&gt; [SP &lt;old-oid&gt;] LF
create SP &lt;ref&gt; SP &lt;new-oid&gt; LF
delete SP &lt;ref&gt; [SP &lt;old-oid&gt;] LF
verify SP &lt;ref&gt; [SP &lt;old-oid&gt;] LF
option SP &lt;opt&gt; LF
start LF
prepare LF
commit LF
abort LF</literallayout>
<simpara>With <emphasis>--create-reflog</emphasis>, update-ref will create a reflog for each ref
even if one would not ordinarily be created.</simpara>
<simpara>Quote fields containing whitespace as if they were strings in C source
code; i.e., surrounded by double-quotes and with backslash escapes.
Use 40 "0" characters or the empty string to specify a zero value.  To
specify a missing value, omit the value and its preceding SP entirely.</simpara>
<simpara>Alternatively, use <emphasis>-z</emphasis> to specify in NUL-terminated format, without
quoting:</simpara>
<literallayout class="monospaced">update SP &lt;ref&gt; NUL &lt;new-oid&gt; NUL [&lt;old-oid&gt;] NUL
create SP &lt;ref&gt; NUL &lt;new-oid&gt; NUL
delete SP &lt;ref&gt; NUL [&lt;old-oid&gt;] NUL
verify SP &lt;ref&gt; NUL [&lt;old-oid&gt;] NUL
option SP &lt;opt&gt; NUL
start NUL
prepare NUL
commit NUL
abort NUL</literallayout>
<simpara>In this format, use 40 "0" to specify a zero value, and use the empty
string to specify a missing value.</simpara>
<simpara>In either format, values can be specified in any form that Git
recognizes as an object name.  Commands in any other format or a
repeated &lt;ref&gt; produce an error.  Command meanings are:</simpara>
<variablelist>
<varlistentry>
<term>
update
</term>
<listitem>
<simpara>
        Set &lt;ref&gt; to &lt;new-oid&gt; after verifying &lt;old-oid&gt;, if given.
        Specify a zero &lt;new-oid&gt; to ensure the ref does not exist
        after the update and/or a zero &lt;old-oid&gt; to make sure the
        ref does not exist before the update.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
create
</term>
<listitem>
<simpara>
        Create &lt;ref&gt; with &lt;new-oid&gt; after verifying it does not
        exist.  The given &lt;new-oid&gt; may not be zero.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
delete
</term>
<listitem>
<simpara>
        Delete &lt;ref&gt; after verifying it exists with &lt;old-oid&gt;, if
        given.  If given, &lt;old-oid&gt; may not be zero.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
verify
</term>
<listitem>
<simpara>
        Verify &lt;ref&gt; against &lt;old-oid&gt; but do not change it.  If
        &lt;old-oid&gt; is zero or missing, the ref must not exist.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
option
</term>
<listitem>
<simpara>
        Modify the behavior of the next command naming a &lt;ref&gt;.
        The only valid option is <emphasis>no-deref</emphasis> to avoid dereferencing
        a symbolic ref.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
start
</term>
<listitem>
<simpara>
        Start a transaction. In contrast to a non-transactional session, a
        transaction will automatically abort if the session ends without an
        explicit commit. This command may create a new empty transaction when
        the current one has been committed or aborted already.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
prepare
</term>
<listitem>
<simpara>
        Prepare to commit the transaction. This will create lock files for all
        queued reference updates. If one reference could not be locked, the
        transaction will be aborted.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
commit
</term>
<listitem>
<simpara>
        Commit all reference updates queued for the transaction, ending the
        transaction.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
abort
</term>
<listitem>
<simpara>
        Abort the transaction, releasing all locks if the transaction is in
        prepared state.
</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>If all &lt;ref&gt;s can be locked with matching &lt;old-oid&gt;s
simultaneously, all modifications are performed.  Otherwise, no
modifications are performed.  Note that while each individual
&lt;ref&gt; is updated or deleted atomically, a concurrent reader may
still see a subset of the modifications.</simpara>
</simplesect>
<simplesect id="git-update-ref(1)__logging_updates">
<title>LOGGING UPDATES</title>
<simpara>If config parameter "core.logAllRefUpdates" is true and the ref is one
under "refs/heads/", "refs/remotes/", "refs/notes/", or a pseudoref
like HEAD or ORIG_HEAD; or the file "$GIT_DIR/logs/&lt;ref&gt;" exists then
<emphasis>git update-ref</emphasis> will append a line to the log file
"$GIT_DIR/logs/&lt;ref&gt;" (dereferencing all symbolic refs before creating
the log name) describing the change in ref value.  Log lines are
formatted as:</simpara>
<literallayout class="monospaced">oldsha1 SP newsha1 SP committer LF</literallayout>
<simpara>Where "oldsha1" is the 40 character hexadecimal value previously
stored in &lt;ref&gt;, "newsha1" is the 40 character hexadecimal value of
&lt;new-oid&gt; and "committer" is the committer's name, email address
and date in the standard Git committer ident format.</simpara>
<simpara>Optionally with -m:</simpara>
<literallayout class="monospaced">oldsha1 SP newsha1 SP committer TAB message LF</literallayout>
<simpara>Where all fields are as described above and "message" is the
value supplied to the -m option.</simpara>
<simpara>An update will fail (without changing &lt;ref&gt;) if the current user is
unable to create a new log file, append to the existing log file
or does not have committer information available.</simpara>
</simplesect>
<simplesect id="git-update-ref(1)__git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
</simplesect>
</sect2>
