Difference between revisions of "Git"

From Wiki
Jump to: navigation, search
Line 109: Line 109:
  cd /other/repo
  cd /other/repo
  git am -3 < ~/file.patch
  git am -3 < ~/file.patch
== Search all branches ==
git grep "regexp" $(git rev-list --all)
git branch -a --contains <commit>
= Gitweb Installation =
= Gitweb Installation =

Latest revision as of 16:19, 12 October 2020

Git Notes

config settings

Useful git config settings:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.pof push origin force
git config --global alias.unstage 'reset HEAD --'
git config --global alias.rbc rebase --continue
git config --global status.showUntrackedFiles all
git config --global color.ui true
git config --global push.default simple

Squelch push errors

git config receive.denyCurrentBranch ignore

Generate log report. What did I do this week?

git log --pretty=format:"* %B%n" --since=1.weeks --author User\ Name

== Amend last commit. Avoid amending commits that have been pushed.

git commit --amend

See changes from a commit:

git diff COMMIT^!

Checkout remote branch:

git fetch origin

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

git checkout -b test origin/test

Delete remote branch:

As of Git v1.7.0, you can delete a remote branch using

git push origin --delete <branchName>

which is easier to remember than

git push origin :<branchName>

Clone remote branch:

git clone -b my-branch <origin>

Set origin

git remote set-url origin root@server.example.com:/etc/puppet 

Roll back to previous commit:

git reset <commit>

To discard the current working directory use the --hard option.

git reset --hard <commit>

Set up initial repo for push.

You will need to set up an empty directory and then initialize it to store git data.

 mkdir newproject
 cd newproject
 git init

Now push from your *source* repo.

 git push user@example.com:/home/projects/newproject master

Stop tracking a file. This removes the file from version control while keeping it in the working repository.

git rm --cached <file>

Cherry pick commit from another branch:

git cherry-pick <id>

Switch upstream branch:

git branch -m master production
git push --set-upstream origin production

To change the default branch for new clones you will need to create a symbolic ref.

git --git-dir /path/to/bare/repo symbolic-ref HEAD refs/heads/production

Remove untracked files/dirs

git clean -f -d or git clean -fd.
git clean -f -X or git clean -fX.

To remove ignored and non-ignored files, run git clean -f -x or git clean -fx.

Checkout remote branch:

git fetch
git checkout -b newbranch origin/newbranch

Create and apply patch

cd /repo
git format-patch -1 <commit_id>
mv <patch_file> ~

cd /other/repo
git am -3 < ~/file.patch

Search all branches

git grep "regexp" $(git rev-list --all)
git branch -a --contains <commit>

Gitweb Installation

git includes a cgi script which can be used to browse git repositories. To set this up first install the packages:

yum -y install gitweb httpd

Edit the apache config file to enable name based virtual hosts. Uncomment the line that says "NameVirtualHost *:80".

Set up a default virtual host. For example, add these lines to httpd.conf.

<VirtualHost *:80>
    ServerAdmin ops-team@example.com
    DocumentRoot /var/www/html
    ServerName server1.example.com

Next edit the /etc/httpd/conf.d/git.conf config file for gitweb.

<VirtualHost *:80>
ServerName git.example.com
SuexecUserGroup git git
DocumentRoot /var/www/gitweb

SetEnv GITWEB_CONFIG  /etc/gitweb.conf
SetEnv GIT_PROJECT_ROOT /home/git/repositories
ScriptAlias /git/ /var/www/gitweb/gitweb.cgi/

# Logfiles
ErrorLog  /var/log/httpd/gitweb.error.log
CustomLog /var/log/httpd/gitweb.access.log combined

<Directory /var/www/gitweb>
    Options +ExecCGI +FollowSymLinks
    AddHandler cgi-script .cgi
    DirectoryIndex gitweb.cgi

    # Pretty gitweb URLs
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.* /gitweb.cgi/$0 [L,PT]

    Order Deny,Allow
    Deny from all
    Allow from

# Enable git clone over HTTP - does not work
ScriptAliasMatch \
          "(?x)^/(.*/(HEAD | \
          info/refs | \
          objects/(info/[^/]+ | \
          [0-9a-f]{2}/[0-9a-f]{38} | \
          pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
          git-(upload|receive)-pack))$" \

Since we are using suexec the gitweb files must be copied into the default document root.

rsync -avHl /usr/share/gitweb/ /var/www/gitweb/
chown -R git:git /var/www/gitweb/

Restart apache.

Edit /etc/gitweb.conf to customize gitweb for your environment.

# path to git projects (.git)
$projectroot = "/home/git/repositories";

#uncomment this to display URLs for cloning
#@git_base_url_list = ("git://git.example.com", "http://git.example.com");

# directory to use for temp files
$git_temp = "/tmp";

$site_name = "git.example.com";

# require export flag
$export_ok = "git-daemon-export-ok";
$strict_export = 1;

# target of the home link on top of all pages
#$home_link = $my_uri || "/";

# html text to include at home page
#$home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
#$projects_list = $projectroot;

# stylesheet to use
#$stylesheet = "gitweb.css";

# javascript code for gitweb
#$javascript = "gitweb.js";

# logo to use
#$logo = "git-logo.png";

# the 'favicon'
#$favicon = "git-favicon.png";

# enable git blame
$feature{'blame'}{'default'} = [1];

# enable pickaxe search
$feature{'pickaxe'}{'default'} = [1];

# enable snapshot downloads
$feature{'snapshot'}{'default'} = ['zip', 'tgz'];

# enable syntax highlighting
$feature{'highlight'}{'default'} = [1];

# enable pretty URLs
$feature{'pathinfo'}{'default'} = [1];