Git: Difference between revisions

From Han Wiki
Jump to navigation Jump to search
→‎Signing work: add uploading of tags
change syntaxhighlight to source
Line 3: Line 3:
Get your GPG configured, and a personal key installed.  Configure Git to use your personal key.
Get your GPG configured, and a personal key installed.  Configure Git to use your personal key.


<syntaxhighlight lang="bash">
<source lang="bash">
$ git config --global user.signingkey 0A46826A
$ git config --global user.signingkey 0A46826A
</syntaxhighlight>
Signing tags:
<syntaxhighlight lang="bash">
$ git tag -s v2.17 -m 'version 2.17 signed by MH'
$ git show v2.17
</syntaxhighlight>
With the signer's public key in the keyring, you can verify the tag:
<syntaxhighlight lang="bash">
$ git tag -v v2.17
</syntaxhighlight>
Upload a specific tag:
<source lang="bash">
$ git push origin v2.29.1
</source>
Upload all tags:
<source lang="bash">
$ git push origin --tags
</source>
</source>


Line 36: Line 11:
You can sign commits simply by adding -S once your environment is configured.
You can sign commits simply by adding -S once your environment is configured.


<syntaxhighlight lang="bash">
<source lang="bash">
$ git commit -S -m 'push a signed commit'
$ git commit -S -m 'push a signed commit'
</syntaxhighlight>
</source>


You can check and verify via <span class="package">git log</span>:
You can check and verify via <span class="package">git log</span>:


<syntaxhighlight lang="bash">
<source lang="bash">
$ git log --show-signature -1
$ git log --show-signature -1
</syntaxhighlight>
</source>


You can configure <span class="package">git log</span> to check any signatures and list them in output via <span class="package">%G?</span> format.
You can configure <span class="package">git log</span> to check any signatures and list them in output via <span class="package">%G?</span> format.


<syntaxhighlight lang="bash">
<source lang="bash">
$ git log --pretty="format:%h %G? %aN  %s"
$ git log --pretty="format:%h %G? %aN  %s"
</syntaxhighlight>
</source>


You can also reject commits that are unsigned and invalid:
You can also reject commits that are unsigned and invalid:


<syntaxhighlight lang="bash">
<source lang="bash">
$ git merge --verify-signature non-verify
$ git merge --verify-signature non-verify


$ git merge --verify-signatures signed-branch
$ git merge --verify-signatures signed-branch
</syntaxhighlight>
</source>


Sign the merge commit itself:
Sign the merge commit itself:


<syntaxhighlight lang="bash">
<source lang="bash">
$ git merge --verify-signatures -S signed-branch
$ git merge --verify-signatures -S signed-branch
</syntaxhighlight>
</source>


= Commands =
= Commands =
== Tags ==
Signing tags:
<source lang="bash">
$ git tag -s v2.17 -m 'version 2.17 signed by MH'
$ git show v2.17
</source>
With the signer's public key in the keyring, you can verify the tag:
<source lang="bash">
$ git tag -v v2.17
</source>
Upload a specific tag:
<source lang="bash">
$ git push origin v2.29.1
</source>
Upload all tags:
<source lang="bash">
$ git push origin --tags
</source>


== Get a build number ==
== Get a build number ==
Line 72: Line 74:
A commit count can be used as a build number for a revision (e.g. HEAD, master, a commit hash):
A commit count can be used as a build number for a revision (e.g. HEAD, master, a commit hash):


<syntaxhighlight lang="bash">
<source lang="bash">
$ git rev-list --count <revision>
$ git rev-list --count <revision>
</syntaxhighlight>
</source>


To get the commit count across all branches:
To get the commit count across all branches:


<syntaxhighlight lang="bash">
<source lang="bash">
$ git rev-list --all --count
$ git rev-list --all --count
</syntaxhighlight>
</source>


== Add a local branch that tracks a remote branch ==
== Add a local branch that tracks a remote branch ==


<syntaxhighlight lang="bash">
<source lang="bash">
$ git branch --track greet origin/greet
$ git branch --track greet origin/greet
$ git branch -a
$ git branch -a
$ git merge origin/master
$ git merge origin/master
</syntaxhighlight>
</source>


== Pulling changes from a remote ==
== Pulling changes from a remote ==
Line 113: Line 115:
== Rebase a branch ==
== Rebase a branch ==


<syntaxhighlight lang="bash">
<source lang="bash">
$ git checkout cbranch
$ git checkout cbranch
Switched to branch 'cbranch'
Switched to branch 'cbranch'
Line 124: Line 126:


$
$
</syntaxhighlight>
</source>


== Merge a branch ==
== Merge a branch ==


<syntaxhighlight lang="bash">
<source lang="bash">
$ git checkout autonotif
$ git checkout autonotif
Switched to branch 'autonotif'
Switched to branch 'autonotif'
Line 137: Line 139:
  1 file changed, 1 insertion(+)
  1 file changed, 1 insertion(+)
  create mode 100644 foobarrecords_savenew.hns
  create mode 100644 foobarrecords_savenew.hns
</syntaxhighlight>
</source>


Merges <branchname> onto autonotif branch
Merges <branchname> onto autonotif branch
Line 183: Line 185:
== Remove a tag ==
== Remove a tag ==


<syntaxhighlight lang="bash">
<source lang="bash">
$ git tag -d <tagname>
$ git tag -d <tagname>
</syntaxhighlight>
</source>


== Undo last commit ==
== Undo last commit ==
Line 191: Line 193:
This undo is recorded as a part of the git log.
This undo is recorded as a part of the git log.


<syntaxhighlight lang="bash">
<source lang="bash">
$ git revert HEAD
$ git revert HEAD
</syntaxhighlight>
</source>


== Fix or amend last commit ==
== Fix or amend last commit ==
Line 203: Line 205:
== Reset the branch to a specific point in the past ==
== Reset the branch to a specific point in the past ==


<syntaxhighlight lang="bash">
<source lang="bash">
$ git reset --hard <hash>
$ git reset --hard <hash>
</syntaxhighlight>
</source>


--hard parameter also updates the working directory. Reset shouldn't be used in a group project on a remote repository because of its potential for confusion.
--hard parameter also updates the working directory. Reset shouldn't be used in a group project on a remote repository because of its potential for confusion.
Line 213: Line 215:
;Temporary ignore changes to a file
;Temporary ignore changes to a file


<syntaxhighlight lang="bash">
<source lang="bash">
$ git update-index --assume-unchanged <filename>
$ git update-index --assume-unchanged <filename>
</syntaxhighlight>
</source>


;Back to tracking changes to the file
;Back to tracking changes to the file


<syntaxhighlight lang="bash">
<source lang="bash">
$ git update-index --no-assume-unchanged <filename>
$ git update-index --no-assume-unchanged <filename>
</syntaxhighlight>
</source>


;Get a list of folders and files that are <code>assume-unchanged</code>
;Get a list of folders and files that are <code>assume-unchanged</code>


<syntaxhighlight lang="bash">
<source lang="bash">
$ git ls-files -v | grep '^h'
$ git ls-files -v | grep '^h'
</syntaxhighlight>
</source>


== Ignore file modes ==
== Ignore file modes ==
Line 241: Line 243:
Verify the connectivity and validity of the objects
Verify the connectivity and validity of the objects


<syntaxhighlight lang="bash">
<source lang="bash">
$ git fsck --unreachable
$ git fsck --unreachable
</syntaxhighlight>
</source>


Manage reflog information
Manage reflog information


<syntaxhighlight lang="bash">
<source lang="bash">
$ git reflog expire --expire=0 --all
$ git reflog expire --expire=0 --all
</syntaxhighlight>
</source>


Pack unpacked objects in a repository
Pack unpacked objects in a repository


<syntaxhighlight lang="bash">
<source lang="bash">
$ git repack -a -d -l
$ git repack -a -d -l
</syntaxhighlight>
</source>


Prune all unreachable objects from the object database
Prune all unreachable objects from the object database


<syntaxhighlight lang="bash">
<source lang="bash">
$ git prune
$ git prune
</syntaxhighlight>
</source>


Clean up unnecessary files and optimize the local repository
Clean up unnecessary files and optimize the local repository


<syntaxhighlight lang="bash">
<source lang="bash">
$ git gc --aggressive
$ git gc --aggressive
</syntaxhighlight>
</source>


= Useful git aliases =
= Useful git aliases =
Line 273: Line 275:
This is a snapshot of my ~/.gitconfig
This is a snapshot of my ~/.gitconfig


<syntaxhighlight lang="ini">
<source lang="ini">
[user]
[user]
         name = Michael Han
         name = Michael Han
Line 305: Line 307:
[merge]
[merge]
         defaultToUpstream = true
         defaultToUpstream = true
</syntaxhighlight>
</source>


= Links =
= Links =

Revision as of 10:51, 27 March 2018

Signing work

Get your GPG configured, and a personal key installed. Configure Git to use your personal key.

$ git config --global user.signingkey 0A46826A

Signing commits

You can sign commits simply by adding -S once your environment is configured.

$ git commit -S -m 'push a signed commit'

You can check and verify via git log:

$ git log --show-signature -1

You can configure git log to check any signatures and list them in output via %G? format.

$ git log --pretty="format:%h %G? %aN  %s"

You can also reject commits that are unsigned and invalid:

$ git merge --verify-signature non-verify

$ git merge --verify-signatures signed-branch

Sign the merge commit itself:

$ git merge --verify-signatures -S signed-branch

Commands

Tags

Signing tags:

$ git tag -s v2.17 -m 'version 2.17 signed by MH'
$ git show v2.17

With the signer's public key in the keyring, you can verify the tag:

$ git tag -v v2.17

Upload a specific tag:

$ git push origin v2.29.1

Upload all tags:

$ git push origin --tags

Get a build number

A commit count can be used as a build number for a revision (e.g. HEAD, master, a commit hash):

$ git rev-list --count <revision>

To get the commit count across all branches:

$ git rev-list --all --count

Add a local branch that tracks a remote branch

$ git branch --track greet origin/greet
$ git branch -a
$ git merge origin/master

Pulling changes from a remote

<sourcelang="bash"> $ git fetch $ git merge origin/master </source>

is equivalent to:

$ git pull

Cleaning up remote branch list

This cleans up and removes any branches that are no longer on the remote

$ git remote prune origin

Rebase a branch

$ git checkout cbranch
Switched to branch 'cbranch'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added Greeter class
Applying: hello uses Greeter
Applying: updated Rakefile

$

Merge a branch

$ git checkout autonotif
Switched to branch 'autonotif'

$ git merge <branchname>
Merge made by the 'recursive' strategy.
 foobarrecords_savenew.hns | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 foobarrecords_savenew.hns

Merges <branchname> onto autonotif branch

Delete a branch

  • Delete a local branch
$ git branch -d hotpatch1
  • Delete a remote branch
git push origin --delete hotpatch1
  • Delete a remote branch that as no actual reference on the remote
git branch -r -d origin/hotpatch1

List merged branches

List branches merged into master

$ git branch --merged master

List branches merged into HEAD (i.e. tip of current branch)

$ git branch --merged

List branches that have not been merged

$ git branch --no-merged

Remove a tag

$ git tag -d <tagname>

Undo last commit

This undo is recorded as a part of the git log.

$ git revert HEAD

Fix or amend last commit

$ git commit --amend

Reset the branch to a specific point in the past

$ git reset --hard <hash>

--hard parameter also updates the working directory. Reset shouldn't be used in a group project on a remote repository because of its potential for confusion.

Ignore files

Temporary ignore changes to a file
$ git update-index --assume-unchanged <filename>
Back to tracking changes to the file
$ git update-index --no-assume-unchanged <filename>
Get a list of folders and files that are assume-unchanged
$ git ls-files -v | grep '^h'

Ignore file modes

This is useful when working with multiple file systems.

$ git config core.filemode false

Maintenance command

Verify the connectivity and validity of the objects

$ git fsck --unreachable

Manage reflog information

$ git reflog expire --expire=0 --all

Pack unpacked objects in a repository

$ git repack -a -d -l

Prune all unreachable objects from the object database

$ git prune

Clean up unnecessary files and optimize the local repository

$ git gc --aggressive

Useful git aliases

This is a snapshot of my ~/.gitconfig

[user]
        name = Michael Han
        email = mhan@pfg.fjfj.com
[core]
        editor = vim
        autocrlf = input
        safecrlf = true
[color]
        ui = always
[alias]
        co = checkout
        ci = commit
        st = status
        br = branch
        df = difftool
        hist = log --pretty=tformat:\"%C(yellow)%h %C(green)%ad %Creset| %s%C(red)%d %C(bold blue)[%an]\" --graph --date=short
	hist2 = log --graph --abbrev-commit --decorate --date=relative --format=tformat:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold blue)\" --graph --date=short
        histall = log --pretty=tformat:\"%C(yellow)%h %C(green)%ad %Creset| %s%C(red)%d %C(blue)[%an]\" --graph --date=short --all
        hist10 = !git log --pretty=tformat:\"%C(yellow)%h %C(green)%ad %Creset| %s%C(red)%d %C(blue)[%an]\" --graph --date=short | head -n 10
        hist10all = !git log --pretty=tformat:\"%C(yellow)%h %C(green)%ad %Creset| %s%C(red)%d %C(blue)[%an]\" --graph --date=short --all | head -n 10
        listnames = diff-tree --no-commit-id --name-status -r
        type = cat-file -t
        dump = cat-file -p
        ignore = update-index --assume-unchanged
        track = update-index --no-assume-unchanged
        ignorelist = !git ls-files -v | grep -s ^'h ' | cut -b 1-2 --complement[diff]
        tool = vimdiff
[difftool]
        prompt = false
[merge]
        defaultToUpstream = true

Links

http://qugstart.com/blog/ruby-and-rails/create-a-new-git-remote-repository-from-some-local-files-or-local-git-repository/
http://gitimmersion.com/
http://kris.me.uk/2010/09/30/git-repository-server-gitolite.html
http://www.lucidlynx.com/how-to-install-gitweb-in-ubuntu/
http://mustalikachwala.blogspot.com/2012/07/ubuntu-1204-installing-gitolite-and.html
http://ben.kulbertis.org/2011/10/synchronizing-a-mysql-database-with-git-and-git-hooks/
http://viget.com/extend/backup-your-database-in-git
http://longair.net/blog/2009/04/16/git-fetch-and-merge/
a warning against pull

'http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging