Git quick reference

From FedoraProject

(Difference between revisions)
Jump to: navigation, search
m (Replace all git-<command> instances with the supported "git <command>" form)
(Update instructions for displaying the current branch in bash prompt)
Line 21: Line 21:
 
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, add these lines as a function to <code>~/.bashrc</code>:
+
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>:
  
<pre>
+
<pre>export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '</pre>
if [ -z "`declare -p | grep __git_ps1`" ]; then
+
 
    export PS1="[\u@\h \W\$(git branch 2> /dev/null | grep -e '\* ' | sed 's/^..\(.*\)/ {\1}/')]\$ "
+
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>.
fi
+
</pre>
+
  
 
To activate bash configuration changes, run <code>source ~/.bashrc</code>.
 
To activate bash configuration changes, run <code>source ~/.bashrc</code>.
 +
 +
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.
  
 
=== Initialize a new repo ===
 
=== Initialize a new repo ===

Revision as of 02:13, 22 September 2009


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

Contents

Basic Operations

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.

git config --global user.name "John Q. Public"

git config --global user.email "john.public@example.com"

git config --global color.diff auto

git config --global color.status auto

git config --global color.branch auto

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 /etc/bash_completion.d/git in the git package. Add this line to ~/.bashrc:

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

If you do not have the bash-completion package installed, you must manually source the git completion script prior to using __git_ps1(). To do this, add "source /etc/bash_completion.d/git" to ~/.bashrc.

To activate bash configuration changes, run source ~/.bashrc.

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 /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 -- 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 $EDITOR and allow you to write a commit log message explaining the changes you've made. A commit log 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

Use:

git branch -a

to determine the name of the remote branch you want to work on. Remote branches will be preceded by origin/. Then use this to switch to it:

git checkout --track -b <branch> origin/<branch>

Maintainer Operations

Remove a remote branch

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>