Git

From Things and Stuff Wiki
Revision as of 18:08, 8 June 2013 by Milk (talk | contribs) (→‎Stash)
Jump to navigation Jump to search


Reference

  • github-cli - command-line interface to GitHub's Issues API (v2)

See also; Vim#Git

Guides

Articles

Consider how a common Git workflow falls apart.

Create a branch off Master, do work, and merge it back into Master when you’re done

Most of the time this behaves as you expect because Master changed since you branched. Then one day you merge a feature branch into Master, but Master hasn’t diverged. Instead of creating a merge commit, Git points Master to the latest commit on the feature branch, or “fast forwards.” (Diagram)

Unfortunately, your feature branch contained checkpoint commits, frequent commits that back up your work but captures the code in an unstable state. Now these commits are indistinguishable from Master’s stable commits. You could easily roll back into a disaster.

Config

git config --global --edit
  open config file in editor
git config --global user.name "Your Name Here"
git config --global user.email "your_email@youremail.com"
git config color.ui true
  turns on colour for everything
git config format.pretty oneline
git config --global push.default matching
  (will be default soon)
git config --global help.autocorrect 10
git config --global credential.helper 'cache --timeout=3600'

The credential helper only works when you clone an HTTPS repo URL. If you use the SSH repo URL instead, SSH keys are used for authentication.

Aliases

Bare

To convert repo to bare repo; [5]

mv repo/.git repo.git
git --git-dir=repo.git config core.bare true
rm -rf repo

Commands

See Vim#Git

Repo config

  • git config receive.denyCurrentBranch warn - allow pushing to a non-base repo
git repo-config core.sharedRepository true - allow access by multiple users

Adding files

git add .
  add file to Index (staging)
git add *
  add all (except those in .gitignore) files to Index
git add -p
  add hunks seperatly [6]

Committing

Committing moves files from Index into HEAD.

git commit -m Commit message goes here.
git commit -a
  git adds at the same time
git commit --amend
  without file change, alter last commit msg

Remotes

git remote add origin <server>
git remote show origin

Getting from remote

git fetch [origin] [master]
  get files from origin master remote
git pull [origin] [master]
  fetch from and merge with another repository or a local branch
git push otherrepo <branch>
  stick branch on otherrepo remote

Reset

git reset --hard HEAD
  reset any uncommitted changes in Index
git reset --hard origin/master
  reset from remote master

Rebase

Branching and merging

git branch
  list branches with current starred
git branch -a
  list branches (including remote)

git branch <branch>
  create branch
git branch -d <branch>
  delete branch
git checkout <branch>
  switch to working on <branch>
git checkout -b <branch>
  same as "git branch <branch> | git checkout <branch>"

git checkout -- <file>
  checkout file from HEAD

git checkout -
   check out previous branch
git merge <branch>
  merge branch, keep branch in commit log
git rebase <branch> - fast-forward merge current to <branch> (?)
git diff master <branch> - show difference between master and another branch
git branch --set-upstream master origin/master
  Branch master set up to track remote branch master from origin.

Stash

Log

git log
  list commits with message and sha
git log .
  log for current directory

git log --since="today"
git log --since="6am"

git log -p
  show diffs
git log -p -2
   show second two last diffs
lol = log --graph --decorate --pretty=oneline --abbrev-commit
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Diffs

See also Vim#Git

git diff
  diff changes
git diff --cached
  diff changes in staging
git diff shaOfCheckIn
  show diff between sha and current
git show shaOfCheckIn
  show diff of sha from previous
  or
git diff shaOfCheckIn^ shaOfCheckIn
git diff shaOfCheckIn{^,}
git diff <revision_1>:<file_1> <revision_2>:<file_2>
  diff between two files

Merging

git mergetool

git mergetool -t opendiff

Meld GUI is worth checking.

Fugitive

:ls
  list window numbers
:diffget [window]
   get change from active window to working

Tags

git tag - list repo tags
git tag 1.0 <sha> - tag <sha> (branch+commit hash) with 1.0

Submodules

git submodule add git@mygithost:project goes/here --branch x.y
git submodule add --branch 7.x-1.x git://git.drupal.org/project/examples.git in/folder
git submodule update --init - bootstrap submodules listed in .gitmodules after cloning somewhere
git submodule foreach git pull origin master - update all submodules
git submodule add git@mygithost:billboard [path/optional]
git submodule init
git submodule update

remove the entry in .gitmodules and .git/config and

git rm --cached path/to/submodule

Patches

git diff > change.patch - create a patch
git diff > [description]-[issue-number]-[comment-number].patch - create a Drupal patch
git apply -v <patch> - apply patch. also;
curl https://github.com/github/jobs/pull/25.patch | git am

.gitignore

  • man gitignore - Specifies intentionally untracked files to ignore

Archive to tar

git archive [8]
  default output is tar
git archive master | tar -x -C /somewhere/else
  pipe to extraction
git archive --format=tar --remote=git@server:repo.git master | tar -xf -

also, Legit.

CLI tools

Zsh prompt symbols

Status:
✔:	repository clean
●n:	there are n staged files
✖n:	there are n unmerged files
✚n:	there are n changed but unstaged files
…:	there are some untracked files

Branch:
↑n:	ahead of remote by n commits
↓n:	behind remote by n commits
↓m↑n:	branches diverged, other by m commits, yours by n commits
::	hash, not a branch

Ncurses

other

GUI

Handy

Services

GitHub

  • willitmerge - A command line tool to check if pull requests are mergeable.
  • Ost.io ("open-source talks") is a forum for open-source projects and the best place for discussing project stuff with other users. It is tightly integrated with GitHub.
  • hub is a command-line wrapper for git that makes you better at GitHub.

Gist

Bitbucket

curl -k -X POST --user user:pass "https://api.bitbucket.org/1.0/repositories" -d "name=project_name"

Other

Repository management

Articles

Git used for private repo, with Gitweb for easy overview.

Public read only;

Git Daemon

Gitolite

  • create git user
  • ~/.ssh/authorized_keys is empty or non-existent
  • put pub key in $HOME/YourName.pub
git clone git://github.com/sitaramc/gitolite
mkdir -p $HOME/bin
gitolite/install -to $HOME/bin
gitolite setup -pk YourName.pub

GitLab

recommends 1Gb ram!

gerrit

RhodeCode

to sort