From Fedora Project Wiki

(Use color.ui instead of multiple color.<foo> calls. Link directly to git config documention for more details.)
m (Use code blocks for file names.)
(13 intermediate revisions by 9 users not shown)
Line 2: Line 2:


{{admon/warning|Quick Reference Only|This is not intended to be an exhaustive list of git operations. See the [http://git-scm.com/documentation documentation] for that.}}
{{admon/warning|Quick Reference Only|This is not intended to be an exhaustive list of git operations. See the [http://git-scm.com/documentation documentation] for that.}}
For instructions on using the git-based Fedora package maintenance system, see [[Package maintenance guide]].
== Basic Operations ==
== Basic Operations ==


=== Configure your global git settings ===
=== Configure your global git settings ===
Running these commands will setup your global git settings. You should obviously use your own contact details.  Should you wish to change your details, you can edit the '~/.gitconfig' file for global settings, or edit '.git/config' to change settings on a particular repo.
Running these commands will setup your global git settings. You should obviously use your own contact details.


<code>git config --global user.name "John Q. Public"</code>
<pre>
git config --global user.name "John Q. Public"
git config --global user.email "john.public@example.com"
git config --global color.ui auto
</pre>


<code>git config --global user.email "john.public@example.com"</code>
Should you wish to change your details later on, you can manually edit the <code>~/.gitconfig</code> file for global settings, or edit <code>.git/config</code> to change settings on a particular repo. Alternatively, you can simply run the above commands again with new details.


<code>git config --global color.ui auto</code>
See the [https://www.kernel.org/pub/software/scm/git/docs/git-config.html git-config] documentation for many more configuration options.
 
See the [http://www.kernel.org/pub/software/scm/git/docs/git-config.html <code>git config</code>] documentation for many more configuration options.


==== Display current branch in bash ====
==== Display current branch in bash ====
Line 19: Line 23:
If you work with branches, and you should, this setting helps you keep track of which branch you are in at a given time.  If you are in a git working directory, it shows the current branch as part of the prompt:
If you work with branches, and you should, this setting helps you keep track of which branch you are in at a given time.  If you are in a git working directory, it shows the current branch as part of the prompt:


<pre>[user@host directory-name (master)]$ </pre>
<pre>[user@host directory-name (master)]$</pre>
 
To enable this, you can take advantage of the <code>__git_ps1</code> function, provided by <code>/usr/share/git-core/contrib/completion/git-prompt.sh</code> file in the git package. Add this line to your <code>~/.bashrc</code>:
 
<pre>export PS1='[\u@\h \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")]\$ '</pre>
 
If you do not have the {{package|bash-completion}} package installed, you must manually source the git completion script prior to using <code>__git_ps1()</code>. To do this, add this line to your <code>~/.bashrc</code>:
 
<pre>. /usr/share/git-core/contrib/completion/git-prompt.sh</pre>
 
To activate bash configuration changes, run:
 
<pre>. ~/.bashrc</pre>
 
In addition to displaying the current branch, this will show when you are in the middle of a merge or rebase.
 
You might also want to display when there are changes in your work tree or the git index:
 
<pre>
[user@host directory-name (master*)]$
[user@host directory-name (master+)]$
[user@host directory-name (master%)]$
</pre>


To enable this, you can take advantage of the <code>__git_ps1</code> function, provided by <code>/etc/bash_completion.d/git</code> in the git package.  Add this line to <code>~/.bashrc</code>:
* On the first line, a tracked file was modified
* On the second line, a tracked file was modified and staged (with <code>git add</code>)
* On the third line, you have untracked files in your tree


<pre>export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '</pre>
Of course, those can combine themselves...


If you do not have the [https://admin.fedoraproject.org/pkgdb/packages/name/bash-completion bash-completion] package installed, you must manually source the git completion script prior to using <code>__git_ps1()</code>.  To do this, add "<code>source /etc/bash_completion.d/git</code>" to <code>~/.bashrc</code>.
To do so, simply add these lines in your <code>~/.bashrc</code>, right before the line modifying your prompt:


To activate bash configuration changes, run <code>source ~/.bashrc</code>.
<pre>
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
</pre>


In addition to displaying the current branch, this will show when you are in the middle of a merge or rebase.  It can also display when there are changes in your work tree or the git index.  See the comments at the beginning of <code>/etc/bash_completion.d/git</code> for more details.
See the comments at the beginning of <code>/etc/bash_completion.d/git</code> for more details.


=== Initialize a new repo ===
=== Initialize a new repo ===


<code>mkdir repo && cd repo && git init</code>
<pre>mkdir repo && cd repo && git init</pre>


Once you've created a repo, you'll find a '.git' folder inside it.  What you essentially have at this point is a bare repo -- a repository with the git configs, but no actual files contained in the repository.  Now let's create a file and tell git that we want it to be part of our repo.  From the repo directory (not the .git directory), type:
Once you've created a repo, you'll find a <code>.git</code> folder inside it.  What you essentially have at this point is a bare repo, which is a repository with the git configs, but no actual files contained in the repository.  Now, let's create a file and tell git that we want it to be part of our repo.  From the repo directory (not the <code>.git</code> directory), type:


<code>echo "Blah" > test.txt</code>
<pre>
 
echo "Blah" > test.txt
<code>git add test.txt</code>
git add test.txt
</pre>


We can then commit the changes by typing:
We can then commit the changes by typing:


<code>git commit test.txt</code>
<pre>git commit test.txt</pre>


Another way of committing all the changes (without having to specify all the files that have changed) is to type:
Another way of committing all the changes (without having to specify all the files that have changed) is to type:


<code>git commit -a</code>
<pre>git commit -a</pre>


Either way, it will bring up whichever editor you have defined in $EDITOR and allow you to write a commit log message explaining the changes you've made.  A commit log message usually consists of:
Either way, it will bring up whichever editor you have defined in your <code>$EDITOR</code> environment variable and allow you to write a commit message explaining the changes you've made.  A commit message usually consists of:


* a one-line summary of your changes
* A one-line summary of your changes
* a blank line
* A blank line
* one or more additional lines with more detail. These lines are optional.
* One or more additional lines with more detail. These lines are optional.


You can always check the status of your current repo by typing
You can always check the status of your current repo by typing:


<code>git status</code>
<pre>git status</pre>


== Developer Operations ==
== Developer Operations ==


=== Create a new local branch ===
=== Create a new local branch ===
<code>git checkout -b <branch></code>
 
<pre>git checkout -b <branch></pre>


=== Push and create a new remote branch from an existing local branch of the same name ===
=== Push and create a new remote branch from an existing local branch of the same name ===
<code>git push origin <branch></code>
 
<pre>git push origin <branch></pre>


=== Switch to a branch that was pushed remotely ===
=== Switch to a branch that was pushed remotely ===
Use:


<code>git branch -a</code>
Run the following command to determine the name of the upstream branch you want to work on.:
 
<pre>git branch -r</pre>
 
Then run this to switch to it:


to determine the name of the remote branch you want to work on.  Remote branches will be preceded by <code>origin/</code>.  Then use this to switch to it:
<pre>git checkout --track origin/<branch></pre>


<code>git checkout --track -b <branch> origin/<branch></code>
This creates a local branch named <code><branch></code> and tells git that it came from <code>origin/<branch></code>.  Using <code>git status</code> will show you whether your local branch is ahead, behind, or otherwise different than the upstream branch.
 
If you want to use a different name for your local branch, you can use:
 
<pre>git checkout --track --branch <some-other-name> origin/<branch></pre>


== Maintainer Operations ==
== Maintainer Operations ==


=== Remove a remote branch ===
=== Remove a remote branch ===
{{admon/warning | This is a dangerous operation. | If there's any chance that other people are using the remote branch, you should at least warn them before doing this.}}
{{admon/warning | This doesn't entirely work right now for Fedora pkgs git | This may give a (hook denied) error. See [https://fedorahosted.org/fedora-infrastructure/ticket/3135] for details.}}
Assuming you had a branch named <code>blah</code> on the remote server, you could remove it by typing:


Assuming you had a branch named ''blah'' on the remote server, you could remove it by typing:
<pre>git push origin :blah</pre>


<code>git push origin :''blah''</code>
=== Apply mailed git patch ===


=== Apply mailed git patch ===
<pre>git am <file></pre>
<code>git am <file></code>

Revision as of 12:05, 5 April 2020


Warning.png
Quick Reference Only
This is not intended to be an exhaustive list of git operations. See the documentation for that.

For instructions on using the git-based Fedora package maintenance system, see Package maintenance guide.

Basic Operations

Configure your global git settings

Running these commands will setup your global git settings. You should obviously use your own contact details.

git config --global user.name "John Q. Public"
git config --global user.email "john.public@example.com"
git config --global color.ui auto

Should you wish to change your details later on, you can manually edit the ~/.gitconfig file for global settings, or edit .git/config to change settings on a particular repo. Alternatively, you can simply run the above commands again with new details.

See the git-config documentation for many more configuration options.

Display current branch in bash

If you work with branches, and you should, this setting helps you keep track of which branch you are in at a given time. If you are in a git working directory, it shows the current branch as part of the prompt:

[user@host directory-name (master)]$

To enable this, you can take advantage of the __git_ps1 function, provided by /usr/share/git-core/contrib/completion/git-prompt.sh file in the git package. Add this line to your ~/.bashrc:

export PS1='[\u@\h \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")]\$ '

If you do not have the Package-x-generic-16.pngbash-completion package installed, you must manually source the git completion script prior to using __git_ps1(). To do this, add this line to your ~/.bashrc:

. /usr/share/git-core/contrib/completion/git-prompt.sh

To activate bash configuration changes, run:

. ~/.bashrc

In addition to displaying the current branch, this will show when you are in the middle of a merge or rebase.

You might also want to display when there are changes in your work tree or the git index:

[user@host directory-name (master*)]$ 
[user@host directory-name (master+)]$ 
[user@host directory-name (master%)]$
  • On the first line, a tracked file was modified
  • On the second line, a tracked file was modified and staged (with git add)
  • On the third line, you have untracked files in your tree

Of course, those can combine themselves...

To do so, simply add these lines in your ~/.bashrc, right before the line modifying your prompt:

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true

See the comments at the beginning of /etc/bash_completion.d/git for more details.

Initialize a new repo

mkdir repo && cd repo && git init

Once you've created a repo, you'll find a .git folder inside it. What you essentially have at this point is a bare repo, which is a repository with the git configs, but no actual files contained in the repository. Now, let's create a file and tell git that we want it to be part of our repo. From the repo directory (not the .git directory), type:

echo "Blah" > test.txt
git add test.txt

We can then commit the changes by typing:

git commit test.txt

Another way of committing all the changes (without having to specify all the files that have changed) is to type:

git commit -a

Either way, it will bring up whichever editor you have defined in your $EDITOR environment variable and allow you to write a commit message explaining the changes you've made. A commit message usually consists of:

  • A one-line summary of your changes
  • A blank line
  • One or more additional lines with more detail. These lines are optional.

You can always check the status of your current repo by typing:

git status

Developer Operations

Create a new local branch

git checkout -b <branch>

Push and create a new remote branch from an existing local branch of the same name

git push origin <branch>

Switch to a branch that was pushed remotely

Run the following command to determine the name of the upstream branch you want to work on.:

git branch -r

Then run this to switch to it:

git checkout --track origin/<branch>

This creates a local branch named <branch> and tells git that it came from origin/<branch>. Using git status will show you whether your local branch is ahead, behind, or otherwise different than the upstream branch.

If you want to use a different name for your local branch, you can use:

git checkout --track --branch <some-other-name> origin/<branch>

Maintainer Operations

Remove a remote branch

Warning.png
This is a dangerous operation.
If there's any chance that other people are using the remote branch, you should at least warn them before doing this.
Warning.png
This doesn't entirely work right now for Fedora pkgs git
This may give a (hook denied) error. See [1] for details.

Assuming you had a branch named blah on the remote server, you could remove it by typing:

git push origin :blah

Apply mailed git patch

git am <file>