Git: Difference between revisions

From Alpine Linux
(added note on how to submit a new proj to git.a.o)
m (fixed link)
 
(59 intermediate revisions by 14 users not shown)
Line 1: Line 1:
[http://git.or.cz Git] is now being used for version control of the [http://dev.alpinelinux.org/cgit/cgit.cgi/alpine-baselayout/ alpine-baselayout] and [http://dev.alpinelinux.org/cgit/cgit.cgi/alpine-conf alpine-conf] packages.  
This document describes how to use [https://git-scm.com git] for Alpine Linux development. Using git submit [[Creating_an_Alpine_package|new packages]] that you've created or [[Creating patches|submit patches]] . You can also browse the [[Development_using_git:Developer_repositories|Developer repositories]].


== Configuring git ==
If you are new to git and need quick reference, check [[#Git Basics|Git Basics]] and also refer [[#Further reading|further reading]] section.
First you need to tell your name and email to git. This name and email will show up in all your commits.


  git config --global user.name "Your Name Comes Here"
{{:Development_using_git:Configuration}}
git config --global user.email you@yourdomain.example.com
{{Tip| If you want to use git with colored output use:
  {{Cmd|git config --global color.ui true
git config --global core.pager more}}
}}


== Git Clone ==
{{Tip| If you want to use git with proxy server:
  {{Cmd|git config --global http.proxy <nowiki>http://proxy_ip:proxy_port</nowiki>}}
}}


To get started, clone the [http://dev.alpinelinux.org/cgit git repository] for the package you are interested in:
== General git workflow ==


  git clone git://git.alpinelinux.org/alpine-baselayout
* [[#Cloning a repository via Git|Clone]] the alpine Git repository.
  git clone git://git.alpinelinux.org/alpine-conf
* Make your file edits in your local checkout of the local copy of repository.
* Ensure that your commits meets the [[#Quality assurance|Quality assurance]].  
* [[#Commit|Commit]] the changes in your local repository.
* Bring the rest of your [[#Keeping your local working branch in sync|local repository up to date]].
* [[#List your commits|Check what you are going to push]]
* If you have write access [[#Git push|push]] your changes up to the master else [[Creating patches|create and submit a patch]].


Make your changes.
== Git Basics ==


To prepare a patch:
=== Cloning your forked repository ===


git diff &gt; short-description-of-change.patch
Fork the repository you want to contribute to. For example to contribute a package or open a merge request for aports you would have to fork [https://gitlab.alpinelinux.org/alpine/aports alpine/aports]. Refer [https://docs.gitlab.com/ee/user/project/repository/forking_workflow.html#creating-a-fork Gitlab docs] if you're having problems with that.  


This diff can be sent to [mailto:alpine-devel@lists.alpinelinux.org alpine-devel@lists.alpinelinux.org] mailing list.
After forking you can clone the repository using the command: {{Cmd|git clone git@gitlab.alpinelinux.org:$USER/$REPO.git}}


== Create new git repository for a new project ==
Replace $USER with the nickname of your Gitlab account and $REPO with the repository you want to work on.
Start with creating a new directory. In this example the project will be named myproj.


mkdir myproj
{{Tip|To update your old fork, see [[Include:Git_Basics#Rebasing_against_upstream_master|rebasing]].}}
cd myproj


Lets initialize the git repository. This will create the .git dir.
=== Cloning aports repository ===
git init


Now create the initial files you want. In this example we just create hello.txt
If you want to clone the Alpine Linux aports repository, switch to the directory you want to have the ''aports/'' directory in and launch git. Unless you have necessary Developer permissions, you will not be able to push your changes back to  the repository.
  echo "hello git" > hello.txt
   
{{Cmd|git clone <nowiki>git://git.alpinelinux.org/aports.git</nowiki>}}


We add this to the git "index" and commit it. The -v option shows the diff as a last reiew. Your $EDITOR will be opened and you will need to create a description of the initial commit.
{{Tip| If you are using proxy server:
  git add hello.txt
  {{Cmd|git clone https://git.alpinelinux.org/aports}}
git commit -v
}}


The format of the commit message should be:
If you want only the last 3 revisions:
[first line]One-line descrption thats les than 72 chars long
[second line empty]
[third line]longer description.


Think of first line as the subject in an email and the third line and on as the body of the email, describing what the commit does. You dont need the long description but the first line, the short description should be there as it will be showed in the commit log.
{{Cmd|git clone <nowiki>git://git.alpinelinux.org/aports.git</nowiki> --depth 3}}


Now you can see the initial commit with:
Use the command below to see the full log of the trunk.
git log


Make some more changes. In this example we just append some text to hello.txt
{{Cmd|git log}}
echo "hello again" >> hello.txt


To check what you have done since last commit use:
{{:Include:Git Basics}}
git log


To commit the change do:
== Quality assurance ==
git add hello.txt
{{:Development using git:Quality assurance}}
git commit -v


or alternatively:
== Further reading ==
git commit -a -v
{{:Development_using_git:Documentation}}


The separate ''git add'' is useful when you ave changed many files but just want commit some of the changed files.
== See also ==


== Publish the new repo to git.alpinelinux.org ==
* [[Creating_an_Alpine_package|Submit new packages]]
make a 'bare' copy of your git repository.
* [[Creating an Alpine package]]
cd ..
* [[Package Maintainers]]
git clone --bare myproj myrepo.git
* [[Development_using_git:Developer_repositories|Developer repositories]]
* [[Development_using_git:Cgit| Using Cgit]]
* [[Gitolite]]


Copy that to git.alpinelinux.org. You need an ssh account for this.
[[Category:Development]]
scp -r myproj.git git.alpinelinux.org:
[[Category:Git]]
 
Send an email to Natanael Copa (or ping him on irc) to make a symlink of it to /gitroot on and to make it visible from cgit.
 
Now you can clone the remote repository from git.alpinelinux.org. First you have to remove the old ''myproj'' directory. Then you clone it with ssh.
mv myproj myproj.backup
git clone ssh://git.alpinelinux.org/~/myproj.git
 
Now you have a local copy of the remote repository in ''myproj/'' directory. You can make changes there as much as you want. You can make commits, tags, undo/redo commits/tags (thats not pushed). You can do that without even having internet access. When you want to submit your changes (all you commits since last ''pull'') you do:
git push
 
 
 
== Migrate a subversion repository to git  ==
 
Start with creating a users.txt file where the svn users are mapped to an email address for git.
 
echo "ncopa = Natanael Copa &lt;ncopa@example.com&gt;" &gt; users.txt
 
Create a temp work area.
 
mkdir proj-tmp
 
Init the git repository. If your svn repo does not have the standard trunk branches and tags dirs you shouldnt use the --stdlayout. You can also use -T trunk -b branches -t tags.
 
cd proj-tmp
git svn init svn://svn.alpinelinux.org/proj --stdlayout
 
Connect the users.txt to the empty git repository so users are remapped.
 
git config svn.authorsfile ../users.txt
 
Fetch and import the svn repository. This might take some time.
 
git fetch
 
Now we can create a bare repository and upload it to dev.alpinelinux.org/gitroot.
 
cd ..
git clone --bare proj-tmp proj.git
scp -r proj.git dev.alpinelinux.org:/gitroot
 
== References  ==
 
Some useful documents
 
*[http://git.or.cz/course/svn.html Git - SVN Crash Course] (quickstart if you know svn)
*[http://cworth.org/hgbook-git/tour/ A tour of git: the basics] '''Recommended'''
*[http://book.git-scm.com/ The Git Community Book]

Latest revision as of 09:52, 18 February 2025

This document describes how to use git for Alpine Linux development. Using git submit new packages that you've created or submit patches . You can also browse the Developer repositories.

If you are new to git and need quick reference, check Git Basics and also refer further reading section.

Configure your global git config

Configure your name and email address in git. This name and email address will show up in all your commits:

$ git config --global user.name "Your Full Name" $ git config --global user.email "your@email.address"

Using git config without --global let you configure other details for a specific git repository.

Tip: If you want to use git with colored output use:

git config --global color.ui true git config --global core.pager more

Tip: If you want to use git with proxy server:

git config --global http.proxy http://proxy_ip:proxy_port

General git workflow

Git Basics

Cloning your forked repository

Fork the repository you want to contribute to. For example to contribute a package or open a merge request for aports you would have to fork alpine/aports. Refer Gitlab docs if you're having problems with that.

After forking you can clone the repository using the command:

git clone git@gitlab.alpinelinux.org:$USER/$REPO.git

Replace $USER with the nickname of your Gitlab account and $REPO with the repository you want to work on.

Tip: To update your old fork, see rebasing.

Cloning aports repository

If you want to clone the Alpine Linux aports repository, switch to the directory you want to have the aports/ directory in and launch git. Unless you have necessary Developer permissions, you will not be able to push your changes back to the repository.

git clone git://git.alpinelinux.org/aports.git

Tip: If you are using proxy server:

git clone https://git.alpinelinux.org/aports

If you want only the last 3 revisions:

git clone git://git.alpinelinux.org/aports.git --depth 3

Use the command below to see the full log of the trunk.

git log


Stashing

git stash

if you want to "hide" your changes. Do this if you think there may be other commits against the same things you are working on and want to refresh your local checkout (using a git pull --rebase) from the master. Use git stash apply to get your stash back.

Reset your local repository

git checkout -f master

if you think your tree is pretty hopeless, need a kill-and-fill to bring the master into your local repository. You will lose local changes.

List the local branch

You can now list your local branch by doing

git branch

which should ouput

* master

List your local non committed changes

git status

Commit

Now you can start to work on your tree. As soon as you feel you have reached a step in development where you can commit your work locally, use

git commit -a

or

git commit <specific files>

or

git add <specific files> git commit

If you wish to give credit to someone else's work (e.g. you are applying a third party patch):

git commit <specific files> --author "Name Surname <user@example.com>

The format of the commit message should be:

One-line description that's less than 72 chars long
<second line empty>
Optional longer description with explanation why changes were made. Links to relevant issues
in Bugtracker can be done with:

  ref #<issuenumber>

It is also possible to resolve issues with:

  fixes #<issuenumber>

Think of first line as the subject in an email and the third line and on as the body of the email, describing what the commit does. You don't need the long description but the first line, the short description should be there as it will be showed in the commit log.

Tip: You can add the following line to your ~/.vimrc:
autocmd FileType gitcommit set textwidth=72

List your commits

To view the list your commits:

git log

To Check what you are going to push from local to remote:

git log origin..master

Keeping your local working branch in sync

Pull the changes from upstream (git.alpinelinux.org)

git pull --rebase

Tip: You can tell git to use rebase, rather than merge (means that '--rebase' would automatically be issued at 'git pull').
Run the command:

git config branch.origin.rebase true

Next time you do 'git pull' you are actually doing a 'git pull --rebase'.

Git Tag

Create an annotated tag and push it.

git tag -a tagname -m 'commit message (e.g release 1.x)' git push && git push --tags

Git push

To push your changes up to the master.

git push

Create a new project

Create your own directory that you want to become your new acf-mystuff project.

mkdir acf-mystuff cd acf-mystuff git init

Create your files and add/commit them to your git-project

git add ./ git commit

Rebasing against upstream master

It's best to always stay up-to-date with the state of the upstream Alpine Linux repository to ensure that no merge conflicts happen later on. To do that you first have to add a new git remote which points to the upstream repository (instead of your fork):

git remote add upstream https://gitlab.alpinelinux.org/alpine/$REPO

Now you can fetch all changes with:

git fetch --all

And then you can rebase with:

git rebase

Quality assurance

Before pushing anything to it is good to make sure that:

  1. The package actually builds
  2. Commit message is good
  3. pkgrel is bumped if needed
  4. no whitespace damage (last chars of a line is whitespace)

The following git hook will help you catch some common errors early:

#!/bin/sh

# Redirect output to stderr.
exec 1>&2

git diff --cached --name-only HEAD | grep 'APKBUILD$' | while read f; do
        olddir=$PWD
        cd ${f%/APKBUILD}
        if ! abuild sanitycheck && verify; then
                exit 1
        fi
        cd "$olddir"
done

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached HEAD --

Install it as .git/hooks/pre-commit and make it executable.

Further reading

See also