You know and use basic git commands like
init
, clone
, add
, commit
,
branch
, checkout
, merge
, pull
, push
.
You probably know and use git commands like
fetch
, pull --rebase
, rebase
, cherry-pick
.
You want to contribute to a repo you do not have write access to.
You do not know how to or do not feel confident working with repos you do not have write access to.
You do not follow any firm workflow working with your own repos or repos you do not have write access.
- How to keep your fork in sync with a source repo
- How to minimize risks of conflicts
- How to keep your repo history tree as clean as possible
- How to contribute properly via pull requests
Some definitions:
- upstream is a repo you want to contribute to
- origin or fork is your own fork of an upstream
- local is a local clone of origin
- master is a default branch (
master
for older repos ormain
for more recent repos)
- Fork an upstream: on GitHub navigate to the upstream and click Fork button.
git clone <origin-url>.git <project-directory>
on your machine to clone your forkcd <project-directory>
git remote add <upstream-url>.git
to enable upstream-related actions
It is important to have your fork in sync with upstream, employ branches and follow certain workflow to minimize conflicts risks and keep the project's git history tree as clean as possible.
- sync local and origin with upstream
git checkout master && git pull upstream master && git push origin master
- create a feature branch with a meaningful name
git checkout -b <feature-branch-name>
There are many branch naming conventions. Few to mention are:
- start with author initials so other collaborators know who initialized the branch and also find branches per author, e.g.
or-feature-portview
- use slashes to denominate scopes, e.g.
feature/api/rest
,bugfix/sleep
- use ticket/issue/epic/story id, e.g.
feat/epic123/story128/search-people
Useful alias:
git config --global alias.sync-master '!git checkout master && git pull upstream master && git push origin master'
Usage: git sync-master
- do whatever you want on a dedicated feature branch
- commit often:
git add . && git commit -m "Add REST api PATCH method"
- squash eventually grouping commits as appropriate:
git rebase -i <REF>
(Making use of git rebase) - sync eventually local, origin, and feature branch with upstream
git checkout master && git pull upstream master && git push origin master
git checkout - && git merge master
- resolve conflicts using your IDE or command line
- when done
git add . && git commit -m "Resolved merge conflict by incorporating both suggestions"
- see also Resolving a merge conflict using the command line
- go to
#1
- eventually push to origin:
git push origin <feature-branch-name>
Useful alias:
git config --global alias.sync-branch '!git sync-master && git checkout - && git merge master'
Usage: git sync-branch
(it also does sync-master
as a part of the flow)
Commit message
There are numbers of commit message structure conventions. To mention a few:
- start with commit scope, e.g. one of
feat|fix|docs|style|refactor|test|chore
- add feature/fix scope, e.g.
(api)
- use imperative mood in the subject line, e.g. one of
Add|Change|Fix|Refactor|Remove|Bump version|Release version
If you resolve an issue (in upstream or fork) it is useful to have a note on issue resolution.
Your commit message may look like the following
fix(foo api): Fix ABC component render conditions
Resolves: #12
See also: #34, #78
Further reading:
- Conventional Commits 1.0.0-beta.2
- How to Write a Git Commit Message
- 5 Useful Tips For A Better Commit Message
- Semantic release
- even more...
DON'Ts
Do not squash or rebase what is already on remotes!
Do not overwrite hsitory on remotes!
NB
The above doesn't cover the case when you work on the same branch
with someone else,
when you will prefer git pull --rebase
over git pull
.
Contributing is not necessarily required. You may develop your fork for your own purposes (adhering to upstream license conditions).
Otherwise stick to the following contribution workflow:
git push origin <feature-branch-name>
to push your feature branch to the origin.- Navigate to the cloud git hub (e.g. GitHub)
- Use cloud UI to create the PR
- Pass through code review process until your changes are merged or declined
Once your feature branch gets merged you will find your commits
in upon next sync with upstream. So, you don't need merging those
to your master
locally.
However, you will need to merge into master
if you do not
contribute to the upstream or your PR is declined and you want
to keep those for yourself.
Once your PR approved and merged you can get rid of your feature branch:
git branch -D feature-branch
locallygit push --delete origin feature-branch
on your remote
Work In Progress under the cut
- Keep things in sync
- When base branch (
master
) gets updated, rebase feature branch onto it- NB! If a feature branch is on remote already then after rebase
push --force
is required to get the remote updated; USE WITH CAUTION - an alternative solution: branch off from master (new branch) and cherry-pick from old branch or rebase from the old branch onto new branch; remove old branch both locally and on remote
- NB! If a feature branch is on remote already then after rebase
- When ready to update
master
- cherry-pick or rebase onto master and squash (using e.g. PR)
- remove feature branch
Hello @OleksiyRudenko, great gist. I am attaching below a spanish version of it perhaps you find it useful to add it to your original.
Also I would like to ask you if I may use this spanish version for educational proposes (givin the proper credit to you of course).
Regards
Contribuir a un repositorio de terceros (para principiantes)
created by @OleksiyRudenko original english version
translated to Spanish @realmariano
Tabla de contenido
Requisitos previos
Conoces y usas comandos básicos de git como
init
,clone
,add
,commit
,branch
,checkout
,merge
,pull
,push
.Probablemente conoces y usas comandos de git como
fetch
,pull --rebase
,rebase
,cherry-pick
.Quiere contribuir a un repositorio al que no tiene acceso de escritura.
No sabes o no te sientes seguro trabajando
con repositorios a los que no tiene acceso de escritura.
No sigue ningún flujo de trabajo firme trabajando con el suyo propio.
repos o repos no tiene acceso de escritura.
Que aprenderás
Un buen flujo de trabajo para empezar
Algunas definiciones:
master
para repositorios más antiguos omain
para repositorios más recientes)Inicializar cosas
git clone <origin-url>.git <project-directory>
en su máquina para clonar su bifurcación on your machine to clone your fork
cd <project-directory>
git remote add <upstream-url>.git
para habilitar acciones relacionadas con el flujo ascendenteAgregue sus cambios
Es importante tener su bifurcación sincronizada con upstream,
emplear sucursales y seguir cierto flujo de trabajo
para minimizar los riesgos de conflictos y mantener el árbol histórico de git del proyecto lo más limpio posible.
Inicializar una branch (rama) de características
git checkout master && git pull upstream master && git push origin master
git checkout -b <feature-branch-name>
Alias útil:
git config --global alias.sync -master '!git checkout master && git pull upstream master && git push origin master'
Uso:
git sync-master
Ciclo de desarrollo de características
git add. && git commit -m "Agregar método REST api PATCH"
git rebase - i <REF>
(Haciendo uso de git rebase)
git checkout master && git pull upstream master && git push origin master
git checkout - && git merge master
git add. && git commit -m "Se resolvió el conflicto de fusión incorporando ambas sugerencias"
Resolución de un conflicto de fusión mediante la línea de comandos
#1
git push origin <feature-branch-name>
Alias útil:
git config --global alias.sync -branch '!git sync-master && git checkout - && git merge master'
Uso:
git sync-branch
(también hacesync-master
como parte del flujo)Mensaje de Commit (confirmación)
Hay varias convenciones de estructura de mensajes de commit.
Por mencionar algunos:
feat|fix|docs|style|refactor|test| chore
( api )
Add|Change|Fix|Refactor|Remove|Bump version|Release version
Si resuelve un problema (en upstream o bifurcación --fork--), es útil tener una nota sobre la resolución del problema.
Su mensaje de confirmación puede parecerse al siguiente
Otras lecturas:
*[Como escribir un mensaje de commit de Git ] (https://chris.beams.io/posts/git-commit/)
NO HACER
¡No aplastes ni hagas rebase de lo que ya está en el remoto!
¡No sobrescriba la historia de los remotos!
NB
Lo anterior no cubre el caso cuando trabajas en la misma branch (rama)
con alguien más,
en ese caso es preferible
git pull --rebase
sobregit pull
.Contribuir a través de una solicitud de extracción (pull request)
Contribuir no es necesariamente obligatorio.
Puedes desarrollar tu tenedor para tus propios fines.
(cumpliendo con las condiciones de la licencia upstream).
De lo contrario, siga el siguiente flujo de trabajo de contribución:
git push origin <feature-branch-name>
para enviar la rama de funciones al origen.los cambios se fusionan o rechazan
Una vez que su rama de funciones se fusione, encontrará sus confirmaciones
en la próxima sincronización con upstream. Por lo tanto, no necesita fusionar esos
a su
master
localmente.Sin embargo, deberá fusionarse con
master
si no contribuye al upstream o su PR es rechazado y deseaguardarlos para usted.
Limpiar
Una vez que su PR sea aprobado y fusionado, puede deshacerse de su rama de características:
git branch -D feature-branch
localmentegit push --delete origin feature-branch
en tu control remotoUn mejor flujo de trabajo
maestro
) se actualice, rebase (cree una nueva base) la rama de características en ellaSe requiere
push --force
para actualizar el control remoto; USE CON PRECAUCIÓNelegir individualmente desde la rama vieja o cambiar la base de la rama vieja a la nueva rama;
eliminar la rama antigua tanto de forma local como remota
master
Flujo de trabajo visualizado
Flujo de trabajo básico de contribución de git visualizado