GitSnips

From Custard
Jump to: navigation, search

Contents

Git Snips

Configure your Git

"Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Every Git clone is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on network access or a central server. Branching and merging are fast and easy to do." http://git-scm.com/

  1. bash-3.2$ git config --global user.name "CustardCat"
  2. bash-3.2$ git config --global user.email "custard@cpan.org"
  3. bash-3.2$ git config --global -l
    user.name=CustardCat
    user.email=custard@cpan.org


Create a new project and commit your code...

  • Create a project and cd into it..
    cd my_project
  • Make the project a git project
    git init
  • Add all the files in the project to git
    git add .
  • Commit your local files
    git commit


Get status, logs & diffs

  • Status of changes
    git status
  • Diffs
    git diffs
  • Add more files
    git add file1 file2 etc..
  • See what's been happening
    git log
    git log --stat --summary
    git log --pretty=oneline
  • See diffs between versions. nb. only need first few chars of version
    git diff 16f8..8bd2
    git diff HEAD^..HEAD

List files in a commit

  • git diff-tree --no-commit-id --name-only -r HEAD

Reverting & adding files

  • Throw all uncommitted changes away (revert)
    git reset --hard
  • Edit the commit message
    git commit --amend
  • Add forgotten files to the last commit
    git reset --soft HEAD^
    git add forgotten.file.txt
    git commit


Ignoring files

  • Ignore files - also can use wildcards
    vi .gitignore
    git add .gitignore
    git commit


Branches & Tags

  • Making a branch
    git branch splendid_branch
  • View Branches - asterisk shows the current branch
    git branch
  • Switch to the new branch
    git checkout splendid_branch
  • Switch back to master
    git checkout master
  • Merge the branch into master
    git merge splendid_branch
  • Tagging - use tag names in place of the revision ids
    git tag v1.2.3
    git tag -a v1.2.3 # Annotated tag
  • Pushing tags
    git push --tags origin master
  • Push branch to origin
    git push origin splendid_branch
  • Delete branch
    git branch -d splendid_branch

Cloning & updating clone

  • Clone a repository
    git clone git://git.kernel.org/.../blah my_blah
    cd my_blah
  • Pull changes from origin (update from upstream)
    git pull origin
  • Check logs since pull
    git log -p ORIG_HEAD../some/dir
  • Extract patches
    git format-patch HEAD^
    git format-patch origin
  • Pull from branch and merge
    git pull git://git.kernel.org/.../some_branch.git ALL
  • Revert a pull
    git reset --hard ORIG_HEAD
  • Garbage collect
    git gc
  • Update tags from original
    git fetch --tags
  • Clone with submodules
    git clone --recursive git://....
    git submodule init # To initialise the local config file
  • Pull from origin and update submodules
    git pull --recurse-submodules
    git submodule foreach git pull origin master

Remotes

  • Add a remote called origin
    • git remote add origin git@my_host:repo.git
  • Rename a remote
    • git remote rename old_name new_name
  • List remotes
    • git remote
    • git remote -v
  • Show a remotes config
    • git remote show origin
  • Remove a remote
    • git remote rm origin

Delete files removed from git

  • git ls-files --deleted | xargs git rm
  • git add -u
  • git rm "$(git ls-files -d)"

Remove a file from all history

  • git filter-branch --index-filter 'git rm --cached --ignore-unmatch MyFileToDelete'


Command Aliases

  • git config --global alias.co checkout

Or in ~/.gitconfig file:

[alias]
    co = checkout


WebDAV repository

Apache2 httpd.conf

<Location /git>
	DAV on
	AuthType Basic
	AuthName "Git"
	AuthUserFile /etc/apache2/passwd.git
	Require valid-user
</Location>

On the server:

cd /var/www/my_host
mkdir git
htpasswd -c /etc/apache2/passwd.git
git init --bare
git update-server-info
chown -R apache:apache .

On the client:

vi ~/.netrc
~ machine your_host
~ login ...
~ password ***

chmod 600 ~/.netrc

Configure git

git config remote.repo.url http://your_host/git
git push repo master

Gotchas

  • PUT error: curl result=22, HTTP code=404

Check the permissions on /var/www/my_host/git.

chown -R apache:apache /var/www/my_host/git

Gitosis repository

Installation

  • (sparc-gentoo): emerge git
  • (sparc-gentoo): ACCEPT_KEYWORDS="~sparc" emerge gitosis
  • mkdir /home/git
  • chown git:git /home/git
  • chmod 711 /home/git
  • vi /etc/passwd (Edit the git user home directory to point at /home/git)
  • su - git
  • ssh-keygen -t rsa
  • gitosis-init < ~/.ssh/id_rsa.pub


Gitosis works by adding a command to the authorised key for a user in ~/.ssh/authorized_keys. gitosis-init creates a default authorised key. It looks a bit like:

### autogenerated by gitosis, DO NOT EDIT
command="gitosis-serve git@gitmachine",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC..

If you need to add more git users, copy their id_rsa.pub keys to this file and copy the first part of this line (from command= to no-pty) before the key. This means when an authorised user connects to git@gitmachine it runs the gitosis-serve command.

When git is used to push to the repo by a user who possesses this public key it redirects the shell to gitosis-serve which handles the repo access control.

Editing the gitosis config

The config itself is contained in a git repo, so you need to clone it, edit it, commit it and push it.

cd ~/projects
git clone git@192.168.1.1:gitosis_admin.git
cd gitosis_admin
vi gitosis.conf
git commit gitosis.conf
git push

Adding a repo

  • Edit the gitosis.conf file (See editing config above)
  • You can specify groups of users, the groups are then referred to with an @ preceding the name.
[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = git@eschaton

[group developer]
members = custardcat@eschaton

[group my-repo]
writable = my-repo
members = @developer custard@nemeton
  • Initialise the repo
    • git init --bare /home/git/repositories/my-repo.git
  • Adding a remote
    • git remote add origin git@192.168.1.1:my_repo.git
  • Pushing your local repo to the remote
    • git push origin master

Adding a user

  • Edit the gitosis.conf file (See editing config above)
  • Add the users ssh public key to the gitosis_admin repo.
    • cp ~/.ssh/id_rsa.pub keydir/jamesb02@mymachine.pub
  • Edit gitosis.conf to reference the key in the relevant project.
[group my-repo]
writable = my-repo
members = @developer jamesb@mymachine

Gotchas

  • ERROR:gitosis.serve.main:Repository read access denied
  • Codebase not updated since 2009 (so it seems)
  • Effectively obsoleted by gitolite

External docs

Gitolite repository

Installation

  • Create a git user and log in as that user.
  • cd ~;mkdir bin;export PATH=$PATH:~/bin
  • ssh-keygen -t rsa
  • cp .ssh/id_rsa.pub git.pub
  • git clone git://github.com/sitaramc/gitolite
  • cd gitolite
  • src/gl-system-install
  • gl-setup ~/git.pub

Add a user

  • git clone git@gitserver:gitolite-admin
  • Copy users id_rsa.pub to gitolite-admin/keydir/UserName.pub
  • Edit gitolite-admin/conf/gitolite.conf and add the user to a repo.

eg.

repo    gitolite-admin
        RW+     =   git

repo    testing
        RW+     =   @all

repo    my-repo
        RW+     = jamesb@octopus.lan

NB. If a username contains an @ then it must also have a domain containing a . (See http://sitaramc.github.com/gitolite/doc/gitolite.conf.html)

  • Push to your repo
    • git push git@gitserver:my-repo

External docs

GitLab

Gitorious

Personal tools