Jump to content
MediaWiki

User:Aude/Git

From mediawiki.org

Git

[edit ]

Clone

[edit ]
gitclonessh://aude@gerrit.wikimedia.org:29418/mediawiki/core.gitmediawiki

Clone a specific tag or branch of a git repo:

gitclone--branch0.16.0https://github.com/wikimedia/data-values-value-view.git

Checkout

[edit ]

Checkout version from a specific date or point in time:

gitcheckout`gitrev-list-n1--before="2012年10月24日 13:37"master`

or:

gitcheckout'master@{2013年09月19日 12:00:00}'

Config

[edit ]

Set global configuration, if only or primarily doing Wikimedia development:

gitconfig--globaluser.name'aude'
gitconfig--globaluser.email'my.email@gmail.com'

It is important for the email to match your email set in Gerrit, and the name is less important.

Set configuration for individual git repositories:

gitconfig--localuser.name'aude'
gitconfig--localuser.email'my.email@gmail.com'

View global config:

gitconfig--global-l

View local config:

gitconfig--local-l

If git or gerrit complain about the committer not matching, check the config, and you can also check how your commits are credited:

gitlog--format=full

General workflow

[edit ]

To commit stuff

[edit ]
gitcommit-a

This will bring up your text editor where you can enter a commit summary.

You can configure which text editor to use with the export command or add it to your ~/.bash_profile file.

exportEDITOR=vim

or specify in git config:

gitconfig--globalcore.editor"vim"

Using the text editor to provide more detailed commit summary is better, however it is also possible to provide the commit summary as an argument in the commit command:

gitcommit-a-m'blah blah blah'

Add an empty directory:

Since git keeps track of files, a directory cannot be committed when it is entirely empty. A workaround is to add an empty .gitkeep file to the directory and then commit.

Stash

[edit ]
gitstashsave'blah blah'

Update local code

[edit ]

in the morning, do:

gitpull--rebaseoriginmaster

Submit code for review

[edit ]

Sanity check, make sure you are on the correct branch, etc.

gitstatus
# On branch coolnewfeature
nothingtocommit(workingdirectoryclean)

See what you are about to submit:

gitshowHEAD
commite08067e92aa72d9bf5a740e1933469f4d249df96
Author:aude<my.email@gmail.com>
Date:SatJan517:25:332013+0000
addREADME

Change-Id:I4ff31405425e27b974d44aabb78d5c3538db92cc

Signed-off-by:aude<my.email@gmail.com>

Instead of git push origin, use git review to submit your change to gerrit:

gitreview

Branching

[edit ]

Make a local branch:

gitcheckout-bmyfeature

Create a branch based on a commit hash:

If making a branch of a gerrit repo, be sure to use the merge commit hash and not the commit hash of the patch itself
gitbranchtestbranch35fcd4632057dc807665205a0df000e72c771937

Checkout a remote branch into a local branch:

gitcheckout-bWikidataorigin/Wikidata

Make some changes, commit them and push to review.

Create a new remote branch

[edit ]
gitcheckout-bmw1.21-wmf7//localbranch
gitpushgerritmw1.21-wmf7//pushtogerrit

If needed, gerrit remote needs to be added:

gitremoteaddgerritssh://aude@gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase.git

List branches

[edit ]

List remote branches:

gitbranch-r
gerrit/0.1.x
gerrit/0.2.x
gerrit/master
gerrit/mw1.21-wmf5
gerrit/mw1.21-wmf6
gerrit/wikidata-wmfphase1beta
origin/0.1.x
origin/0.2.x
origin/HEAD->origin/master
origin/master
origin/mw1.21-wmf5
origin/mw1.21-wmf6
origin/wikidata-wmfphase1beta

List all branches:

gitbranch-a
mycoolfeature
myothercoolstuff
gerrit/0.1.x
gerrit/0.2.x
gerrit/master
gerrit/mw1.21-wmf5
gerrit/mw1.21-wmf6
gerrit/wikidata-wmfphase1beta
origin/0.1.x
origin/0.2.x
origin/HEAD->origin/master
origin/master
origin/mw1.21-wmf5
origin/mw1.21-wmf6
origin/wikidata-wmfphase1beta

See current remote branches:

gitremote-v
gerritssh://aude@gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase.git(fetch)
gerritssh://aude@gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase.git(push)
originhttps://gerrit.wikimedia.org/r/mediawiki/extensions/Wikibase.git(fetch)
originhttps://gerrit.wikimedia.org/r/mediawiki/extensions/Wikibase.git(push)

Get details for a remote:

gitremoteshowgerrit
Enterpassphraseforkey'/home/aude/.ssh/gerrit':
*remotegerrit
FetchURL:ssh://aude@gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase.git
PushURL:ssh://aude@gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase.git
HEADbranch:master
Remotebranches:
0.1.xtracked
0.2.xtracked
mastertracked
mw1.21-wmf5tracked
mw1.21-wmf6tracked
wikidata-wmfphase1betatracked
Localrefsconfiguredfor'git push':
masterpushestomaster(localoutofdate)
mw1.21-wmf5pushestomw1.21-wmf5(localoutofdate)
mw1.21-wmf6pushestomw1.21-wmf6(uptodate)
wikidata-wmfphase1betapushestowikidata-wmfphase1beta(uptodate)

Deleting branches

[edit ]
gitbranch-dmyfeature

To batch delete (wildcard) branches:

gitbranch|grepreview|xargsgitbranch-d

or to force delete branches with "not fully merged" stuff:

gitbranch|grepreview|xargsgitbranch-D

To delete remote branches, such as on github, where one might have permission to do so:

gitpushgithub:sitelinkcomments

To batch delete remote branches:

gitbranch|grepreview|xargsgitpushgithub--delete

Remove untracked files / clean

[edit ]

Removes directories and files:

gitclean-df

Bash prompt

[edit ]

Display the git branch in the bash prompt, the following can be added to your ~/.bashrc file. To make the change take effect, then source ~/.bashrc

# view git branch in prompt 
functionparse_git_branch{
gitbranch--no-color2>/dev/null|sed-e'/^[^*]/d'-e's/* \(.*\)/(1円)/'
}

functionproml{
localLIGHT_GREEN="\[033円[1;32m\]"
localBOLDWHITE="\[033円[1;37m\]"
localWHITE="\[033円[;37m\]"

PS1="$BOLDWHITE\h: $LIGHT_GREEN\$(parse_git_branch)\ 
$WHITE$ "
}

proml

When were the branches last modified?

[edit ]

See a list of all branches sorted by last modified date:

forkin`gitbranch|seds/^..//`;doecho-e`gitlog-1--pretty=format:"%Cgreen%ci %Cblue%cr%Creset"$k--`\\t"$k";done|sort

Works on windows, mac or linux, but not as pretty:

gitfor-each-ref--sort=-committerdaterefs/heads/

Tags

[edit ]
gittag2013-05-22
gitpushgerrit2013-05-22

Removing files from a repo

[edit ]

To recursively remove a folder and files from a repo:

gitrm-rfwikipedia

Reset

[edit ]
gitreset--hardHEAD

or

gitcheckout--hello.php

To unstage files (e.g., after git add -A):

gitreset--

View log

[edit ]
gitlog--oneline--decoratemasterorigin/master
e5a04a3(HEAD,master)Merge"Fix Sqlite upgrade test when profiling to database is enabled"
81d0388[JobQueue]Incrementstatsforjobsignoredasduplicates.
e6f0bdaMerge"Use Database::addQuotes instead of hardcoded quotes"
3066a50Merge"Partial normalisation of hooks.txt"
6bd45d0Merge"recentchanges.rc_ip is used for retroactive autoblocks"
a6a5ed4Merge"Made the profilers that output text not break js."
ce4c01eMerge"Localisation updates from http://translatewiki.net."
0458ce3Madetheprofilersthatoutputtextnotbreakjs.

Just a plain git log, with details:

gitlog
commite5a04a3f54b1120de34e2627a27f116ab060de9c(HEAD,master)
Merge:81d0388fa4196b
Author:jenkins-bot<jenkins--@gerrit.wikimedia.org>
Date:FriJan423:23:202013+0000
Merge"Fix Sqlite upgrade test when profiling to database is enabled"
commit81d03884cbbe00055db7ab0327919d1d6173d8b0
Author:AaronSchulz<-----@wikimedia.org>
Date:ThuDec2016:38:362012-0800
[JobQueue]Incrementstatsforjobsignoredasduplicates.

Change-Id:If4130f4eb79dc2d55b50a7adebfca43620a36d2c
commite6f0bdadb50eb86a87e3f6567f444ba54de4dfb6
Merge:3066a5026f13ec
Author:IAlex<ialex.wiki@gmail.com>
Date:FriJan421:58:392013+0000
Merge"Use Database::addQuotes instead of hardcoded quotes"

Search commit messages

[edit ]
gitlog--grep='DEPLOY'
gitlog--grep='bug'

Patches

[edit ]

Create a patch

[edit ]

From the master branch:

gitcheckout-bcoolnewfeature

make a bunch of changes

# to add everything with the * or you can add specific files
gitadd*
gitcommit-m'made changes'
# patch is a diff between coolnewfeature branch and master
# saved to a file: coolfeature.patch
gitformat-patchmaster--stdout>~/coolfeature.patch

Apply the patch

[edit ]
# Check the patch first
gitapply--stat~/coolfeature.patch
# See if it can be applied cleanly
gitapply--check~/coolfeature.patch
# Apply the patch
gitam--signoff<~/coolfeature.patch

Revert

[edit ]

Revert a specific commit:

gitrevertfe80633d66218b37fc65dfe3f0aa61603dbb7ad2

Revert a merge commit (with -m 1 or -m 2 depending on which parent branch to revert back to):

gitrevert-m16a8e942880d201e78f85ab7c572306e18c37bc4e

Amending

[edit ]

Amend specific commit in series of patches

[edit ]
gitrebase-iHEAD~2

In interactive mode, then:

edit7d20863IntroduceChangeLineFormatterintheclient
pickde3e6a7SplitChangeHandler::getPagesToUpdateintonewclasswithtests

Then make changes, and then:

gitaddChangeLineFormatterTest.php
gitrebase--continue

Squash a commit

[edit ]
gitrebase-iHEAD~2

In interactive mode, then:

pick7d20863IntroduceChangeLineFormatterintheclient
squashde3e6a7SplitChangeHandler::getPagesToUpdateintonewclasswithtests

Edit the commit message, then save and then git will combine the two commits into a single commit.

If amending and resubmitting to gerrit, then git review -R.

Amend author name

[edit ]
gitcommit--amend--author="New Author Name <email@address.com>"

Merge conflicts

[edit ]
gitmergetool--tool=/path/to/tool

or set it:

gitconfig--globalmergetool.<tool>.path(FileMergeforMac,kdiff3forLinux)

Merge in subsequent commits into a patch

[edit ]
gitreview-d16749
gitrebasemaster
gitaddclient/include/LangLinkHandler.php
gitrebase--continue

When you have resolved this problem run "git rebase --continue".

View diffs

[edit ]
gitdiffHEAD~1--Wikibase.php

Submodules

[edit ]
Be patient! checking out core and the submodules takes some time.
If you are not patient and init only certain extensions, then you cannot make patches against the core branch and submit to gerrit.
Go get some coffee or tea while cloning core and updating submodules.

Checkout wmf core branch and extension submodules:

gitclonessh://aude@gerrit.wikimedia.org:29418/mediawiki/core.git
gitcheckout-b1.21wmf7origin/wmf/1.21wmf7
gitsubmoduleupdate--init

If renaming the core directory, references to the submodules need to be updated.

In core/extensions:

find.-name.git-execsed-i's/php-1.21wmf6/wikibase-repo/g''{}'\;

in core/.git:

find.-typef-execsed-i's/php-1.21wmf6/wikibase-repo/g''{}'\;
Removing submodules
See also

Split subpath

[edit ]

To split subdirectories of a git repo into separate repos:

Create clone copies and then in the clone:

gitfilter-branch--prune-empty--subdirectory-filterValueViewmaster

Git blame and credits

[edit ]

Get number of commits per author for a file:

gitshortlog-snParser.php

Get number of lines per author, based on git blame:

gitls-tree-r--name-only-zHEADincludes/parser/Parser.php|xargs-0-n1gitblame--line-porcelainHEAD|grep"^author "|sort|uniq-c|sort-nr

Number of commits by author:

gitshortlog-sne

Gerrit

[edit ]

Get a list of projects

[edit ]

In the shell:

ssh-p29418-laudegerrit.wikimedia.orggerritls-projects

Or go to https://gerrit.wikimedia.org/r/#/admin/projects/

Improving a patch

[edit ]
gitreview-d<CHANGENUMBER>

make some changes

gitaddsome/dir/file.txt
gitcommit--amend
gitreview-R

Adding a dependency

[edit ]

If you need to amend your patch to have the correct dependency:

gitbranch# Take note of the review/* branch that was created for this, it has an "*" in front of it
gitcheckoutbug/1234# Check out the local topic branch of your change
gitrebasereview/john/7000# The branch name of the gerrit change we checked out earlier
  1. Resolve conflicts if needed,
  2. - use "git status" to see the files that need resolution
  3. - after fixing it in your editor, "git add filename" for each of the fixed files
gitcommit--amend-cHEAD# Re-commit your patch replacing the one with the wrong parent


gitlog-n5--decorate--pretty=oneline# Verify that the last 5 entries of the log now start with:
# * (HEAD, bug/1234) your change
# * (review/john/700) the dependency
# * (gerrit/master)
gitpushgerritHEAD:refs/for/master# Use this instead of `git review`

The git push directly will make sure that the dependency is kept and the other revisions that are already in gerrit will not be re-submitted.

Fix a dependency

[edit ]
gitrebase-igerrit/master

Change a commit to a new patchset, such as [1] 5a05e0e (5a05e0ef437278eff23c5a8b076b91f0cc7541c8) to cbcd5a0 (cbcd5a08c895d038a3ca7dc10e8d94806e2cae10).

Re-commit your patch replacing the one with the wrong parent

gitcommit--amend-cHEAD

Resubmit to gerrit:

gitreview-R

Import github repo into existing empty gerrit repo

[edit ]

Keyboard shortcuts

[edit ]

Gerrit has keyboard shortcuts. A list of shortcuts can be displayed by clicking "?".

Pre-commit checks

[edit ]
find.-name"*.php"-print0|xargs-0-n1-P8php-l

Deployment stuff

[edit ]
Each deployment, if extensions not at master
For new extensions

See also

[edit ]
[edit ]
Shortcuts
  • [2] (unreviewed Wikibase patches)
Gerrit stuff
General

AltStyle によって変換されたページ (->オリジナル) /