Browse Source

[Docs] Restructure of Git Best Practices doc (#7231)

* Add "Resynchronizing an Out-of-Sync Git Branch" doc

* Update (Git) Best Practices doc title and filename

* Rename Branch Resync doc

* fork Best Practices doc into multiple files

* Add the doc list to Git Best Practices doc

* Update sidebar

* Update internal references

* Update sidebar - add subsection

* Update Your Fork's Master page title

* title case on Git Best Practices main doc

* ... and in the Resynchronizing a Branch doc

* Please read Part 1

I worked really hard on this, okay?

* Please use branches, too.

* suggestions by mtei

* change note about adding multiple files

* note that the name given the remote repo is arbitrary

* suggestions by fauxpark

* Git Best Practices -> Best Git Practices

Reads more naturally.

* rephrase hint block regarding remote name

* rework the resynchronization instructions per mtei

* use hint boxes for reference to Part 1 doc

I may be addicted to hint boxes. I'm sorry fauxpark. 😢

* add some clarity about the upstream repo

* wordsmithing per mtei

* restyle the shell code blocks

Makes them more consistent to the other docs in this section.
pull/7543/head
James Young 4 years ago
committed by GitHub
parent
commit
3152bf572b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 244 additions and 164 deletions
  1. +4
    -1
      docs/_summary.md
  2. +0
    -163
      docs/newbs_best_practices.md
  3. +16
    -0
      docs/newbs_git_best_practices.md
  4. +79
    -0
      docs/newbs_git_resolving_merge_conflicts.md
  5. +71
    -0
      docs/newbs_git_resynchronize_a_branch.md
  6. +74
    -0
      docs/newbs_git_using_your_master_branch.md

+ 4
- 1
docs/_summary.md View File

@ -3,7 +3,10 @@
* [Building Your First Firmware](newbs_building_firmware.md)
* [Flashing Firmware](newbs_flashing.md)
* [Testing and Debugging](newbs_testing_debugging.md)
* [Git Best Practices](newbs_best_practices.md)
* [Best Git Practices](newbs_git_best_practices.md)
* [Using Your Fork's Master](newbs_git_using_your_master_branch.md)
* [Resolving Merge Conflicts](newbs_git_resolving_merge_conflicts.md)
* [Resynchronizing a Branch](newbs_git_resynchronize_a_branch.md)
* [Learning Resources](newbs_learn_more_resources.md)
* [QMK Basics](README.md)


+ 0
- 163
docs/newbs_best_practices.md View File

@ -1,163 +0,0 @@
# Best Practices
## Or, "How I Learned to Stop Worrying and Love Git."
This document aims to instruct novices in the best ways to have a smooth experience in contributing to QMK. We will walk through the process of contributing to QMK, detailing some ways to make this task easier, and then later we'll break some things in order to teach you how to fix them.
This document assumes a few things:
1. You have a GitHub account, and have [forked the qmk_firmware repository](getting_started_github.md) to your account.
2. You've [set up your build environment](newbs_getting_started.md#set-up-your-environment).
## Your fork's master: Update Often, Commit Never
It is highly recommended for QMK development, regardless of what is being done or where, to keep your `master` branch updated, but ***never*** commit to it. Instead, do all your changes in a development branch and issue pull requests from your branches when you're developing.
To reduce the chances of merge conflicts — instances where two or more users have edited the same part of a file concurrently — keep your `master` branch relatively up-to-date, and start any new developments by creating a new branch.
### Updating your master branch
To keep your `master` branch updated, it is recommended to add the QMK Firmware repository ("repo") as a remote repository in git. To do this, open your Git command line interface and enter:
```
git remote add upstream https://github.com/qmk/qmk_firmware.git
```
To verify that the repository has been added, run `git remote -v`, which should return the following:
```
$ git remote -v
origin https://github.com/<your_username>/qmk_firmware.git (fetch)
origin https://github.com/<your_username>/qmk_firmware.git (push)
upstream https://github.com/qmk/qmk_firmware.git (fetch)
upstream https://github.com/qmk/qmk_firmware.git (push)
```
Now that this is done, you can check for updates to the repo by running `git fetch upstream`. This retrieves the branches and tags &mdash; collectively referred to as "refs" &mdash; from the QMK repo, which now has the nickname `upstream`. We can now compare the data on our fork `origin` to that held by QMK.
To update your fork's master, run the following, hitting the Enter key after each line:
```
git checkout master
git fetch upstream
git pull upstream master
git push origin master
```
This switches you to your `master` branch, retrieves the refs from the QMK repo, downloads the current QMK `master` branch to your computer, and then uploads it to your fork.
### Making Changes
To make changes, create a new branch by entering:
```
git checkout -b dev_branch
git push --set-upstream origin dev_branch
```
This creates a new branch named `dev_branch`, checks it out, and then saves the new branch to your fork. The `--set-upstream` argument tells git to use your fork and the `dev_branch` branch every time you use `git push` or `git pull` from this branch. It only needs to be used on the first push; after that, you can safely use `git push` or `git pull`, without the rest of the arguments.
!> With `git push`, you can use `-u` in place of `--set-upstream` &mdash; `-u` is an alias for `--set-upstream`.
You can name your branch nearly anything you want, though it is recommended to name it something related to the changes you are going to make.
By default `git checkout -b` will base your new branch on the branch that is checked out. You can base your new branch on an existing branch that is not checked out by adding the name of the existing branch to the command:
```
git checkout -b dev_branch master
```
Now that you have a development branch, open your text editor and make whatever changes you need to make. It is recommended to make many small commits to your branch; that way, any change that causes issues can be more easily traced and undone if needed. To make your changes, edit and save any files that need to be updated, add them to Git's *staging area*, and then commit them to your branch:
```
git add path/to/updated_file
git commit -m "My commit message."
```
`git add` adds files that have been changed to Git's *staging area*, which is Git's "loading zone." This contains the changes that are going to be *committed* by `git commit`, which saves the changes to the repo. Use descriptive commit messages so you can know what was changed at a glance.
!> If you've changed a lot of files, but all the files are part of the same change, you can use `git add .` to add all the changed files that are in your current directory, rather than having to add each file individually.
### Publishing Your Changes
The last step is to push your changes to your fork. To do this, enter `git push`. Git now publishes the current state of `dev_branch` to your fork.
## Resolving Merge Conflicts
Sometimes when your work in a branch takes a long time to complete, changes that have been made by others conflict with changes you have made to your branch when you open a pull request. This is called a *merge conflict*, and is what happens when multiple people edit the same parts of the same files.
### Rebasing Your Changes
A *rebase* is Git's way of taking changes that were applied at one point, reversing them, and then applying the same changes to another point. In the case of a merge conflict, you can rebase your branch to grab the changes that were made between when you created your branch and the present time.
To start, run the following:
```
git fetch upstream
git rev-list --left-right --count HEAD...upstream/master
```
The `git rev-list` command entered here returns the number of commits that differ between the current branch and QMK's master branch. We run `git fetch` first to make sure we have the refs that represent the current state of the upstream repo. The output of the `git rev-list` command entered returns two numbers:
```
$ git rev-list --left-right --count HEAD...upstream/master
7 35
```
The first number represents the number of commits on the current branch since it was created, and the second number is the number of commits made to `upstream/master` since the current branch was created, and thus, the changes that are not recorded in the current branch.
Now that the current states of both the current branch and the upstream repo are known, we can start a rebase operation:
```
git rebase upstream/master
```
This tells Git to undo the commits on the current branch, and then reapply them against QMK's master branch.
```
$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: Commit #1
Using index info to reconstruct a base tree...
M conflicting_file_1.txt
Falling back to patching base and 3-way merge...
Auto-merging conflicting_file_1.txt
CONFLICT (content): Merge conflict in conflicting_file_1.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0001 Commit #1
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
```
This tells us that we have a merge conflict, and gives the name of the file with the conflict. Open the conflicting file in your text editor, and somewhere in the file, you'll find something like this:
```
<<<<<<< HEAD
<p>For help with any issues, email us at support@webhost.us.</p>
=======
<p>Need help? Email support@webhost.us.</p>
>>>>>>> Commit #1
```
The line `<<<<<<< HEAD` marks the beginning of a merge conflict, and the `>>>>>>> Commit #1` line marks the end, with the conflicting sections separated by `=======`. The part on the `HEAD` side is from the QMK master version of the file, and the part marked with the commit message is from the current branch and commit.
Because Git tracks *changes to files* rather than the contents of the files directly, if Git can't find the text that was in the file previous to the commit that was made, it won't know how to edit the file. Re-editing the file will solve the conflict. Make your changes, and then save the file.
```
<p>Need help? Email support@webhost.us.</p>
```
Now run:
```
git add conflicting_file_1.txt
git rebase --continue
```
Git logs the changes to the conflicting file, and continues applying the commits from our branch until it reaches the end.

+ 16
- 0
docs/newbs_git_best_practices.md View File

@ -0,0 +1,16 @@
# Best Git Practices for Working with QMK
## Or, "How I Learned to Stop Worrying and Love Git."
This section aims to instruct novices in the best ways to have a smooth experience in contributing to QMK. We will walk through the process of contributing to QMK, detailing some ways to make this task easier, and then later we'll break some things in order to teach you how to fix them.
This section assumes a few things:
1. You have a GitHub account, and have [forked the qmk_firmware repository](getting_started_github.md) to your account.
2. You've set up both [your build environment](newbs_getting_started.md#set-up-your-environment) and [QMK](newbs_getting_started.md#set-up-qmk).
---
- Part 1: [Your Fork's Master: Update Often, Commit Never](newbs_git_using_your_master_branch.md)
- Part 2: [Resolving Merge Conflicts](newbs_git_resolving_merge_conflicts.md)
- Part 3: [Resynchronizing an Out-of-Sync Git Branch](newbs_git_resynchronize_a_branch.md)

+ 79
- 0
docs/newbs_git_resolving_merge_conflicts.md View File

@ -0,0 +1,79 @@
# Resolving Merge Conflicts
Sometimes when your work in a branch takes a long time to complete, changes that have been made by others conflict with changes you have made to your branch when you open a pull request. This is called a *merge conflict*, and is what happens when multiple people edit the same parts of the same files.
?> This document builds upon the concepts detailed in [Your Fork's Master: Update Often, Commit Never](newbs_git_using_your_master_branch.md). If you are not familiar with that document, please read it first, then return here.
## Rebasing Your Changes
A *rebase* is Git's way of taking changes that were applied at one point in the commit history, reversing them, and then applying the same changes at another point. In the case of a merge conflict, you can rebase your branch to grab the changes that were made between when you created your branch and the present time.
To start, run the following:
```
git fetch upstream
git rev-list --left-right --count HEAD...upstream/master
```
The `git rev-list` command entered here returns the number of commits that differ between the current branch and QMK's master branch. We run `git fetch` first to make sure we have the refs that represent the current state of the upstream repo. The output of the `git rev-list` command entered returns two numbers:
```
$ git rev-list --left-right --count HEAD...upstream/master
7 35
```
The first number represents the number of commits on the current branch since it was created, and the second number is the number of commits made to `upstream/master` since the current branch was created, and thus, the changes that are not recorded in the current branch.
Now that the current states of both the current branch and the upstream repo are known, we can start a rebase operation:
```
git rebase upstream/master
```
This tells Git to undo the commits on the current branch, and then reapply them against QMK's master branch.
```
$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: Commit #1
Using index info to reconstruct a base tree...
M conflicting_file_1.txt
Falling back to patching base and 3-way merge...
Auto-merging conflicting_file_1.txt
CONFLICT (content): Merge conflict in conflicting_file_1.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0001 Commit #1
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
```
This tells us that we have a merge conflict, and gives the name of the file with the conflict. Open the conflicting file in your text editor, and somewhere in the file, you'll find something like this:
```
<<<<<<< HEAD
<p>For help with any issues, email us at support@webhost.us.</p>
=======
<p>Need help? Email support@webhost.us.</p>
>>>>>>> Commit #1
```
The line `<<<<<<< HEAD` marks the beginning of a merge conflict, and the `>>>>>>> Commit #1` line marks the end, with the conflicting sections separated by `=======`. The part on the `HEAD` side is from the QMK master version of the file, and the part marked with the commit message is from the current branch and commit.
Because Git tracks *changes to files* rather than the contents of the files directly, if Git can't find the text that was in the file previous to the commit that was made, it won't know how to edit the file. Re-editing the file will solve the conflict. Make your changes, and then save the file.
```
<p>Need help? Email support@webhost.us.</p>
```
Now run:
```
git add conflicting_file_1.txt
git rebase --continue
```
Git logs the changes to the conflicting file, and continues applying the commits from our branch until it reaches the end.

+ 71
- 0
docs/newbs_git_resynchronize_a_branch.md View File

@ -0,0 +1,71 @@
# Resynchronizing an Out-of-Sync Git Branch
Suppose you have committed to your `master` branch, and now need to update your QMK repository. You could `git pull` QMK's `master` branch into your own, but GitHub will tell you that your commit is a number of commits ahead of `qmk:master`, which can create issues if you want to make a pull request to QMK.
?> This document builds upon the concepts detailed in [Your Fork's Master: Update Often, Commit Never](newbs_git_using_your_master_branch.md). If you are not familiar with that document, please read it first, then return here.
## Backing Up the Changes on Your Own Master Branch (Optional)
No one wants to lose work if it can be helped. If you want to save the changes you've already made to your `master` branch, the simplest way to do so is to simply create a duplicate of your "dirty" `master` branch:
```sh
git branch old_master master
```
Now you have a branch named `old_master` that is a duplicate of your `master` branch.
## Resynchronizing Your Branch
Now it's time to resynchronize your `master` branch. For this step, you'll want to have QMK's repository configured as a remote in Git. To check your configured remotes, run `git remote -v`, which should return something similar to:
```sh
QMKuser ~/qmk_firmware (master)
$ git remote -v
origin https://github.com/<your_username>/qmk_firmware.git (fetch)
origin https://github.com/<your_username>/qmk_firmware.git (push)
upstream https://github.com/qmk/qmk_firmware.git (fetch)
upstream https://github.com/qmk/qmk_firmware.git (push)
```
If you only see one fork referenced:
```sh
QMKuser ~/qmk_firmware (master)
$ git remote -v
origin https://github.com/qmk/qmk_firmware.git (fetch)
origin https://github.com/qmk/qmk_firmware.git (push)
```
add a new remote with:
```sh
git remote add upstream https://github.com/qmk/qmk_firmware.git
```
Then, redirect the `origin` remote to your own fork with:
```sh
git remote set-url origin https://github.com/<your_username>/qmk_firmware.git
```
Now that you have both remotes configured, you need to update the references for the upstream repository, which is QMK's, by running:
```sh
git fetch upstream
```
At this point, resynchronize your branch to QMK's by running:
```sh
git reset --hard upstream/master
```
These steps will update the repository on your computer, but your GitHub fork will still be out of sync. To resynchronize your fork on GitHub, you need to push to your fork, instructing Git to override any remote changes that are not reflected in your local repository. To do this, run:
```sh
git push --force-with-lease
```
!> **DO NOT** run `git push --force-with-lease` on a fork to which other users post commits. This will erase their commits.
Now your GitHub fork, your local files, and QMK's repository are all the same. From here you can make further needed changes ([use a branch!](newbs_git_using_your_master_branch.md#making-changes)) and post them as normal.

+ 74
- 0
docs/newbs_git_using_your_master_branch.md View File

@ -0,0 +1,74 @@
# Your Fork's Master: Update Often, Commit Never
It is highly recommended for QMK development, regardless of what is being done or where, to keep your `master` branch updated, but ***never*** commit to it. Instead, do all your changes in a development branch and issue pull requests from your branches when you're developing.
To reduce the chances of merge conflicts &mdash; instances where two or more users have edited the same part of a file concurrently &mdash; keep your `master` branch relatively up-to-date, and start any new developments by creating a new branch.
## Updating your master branch
To keep your `master` branch updated, it is recommended to add the QMK Firmware repository ("repo") as a remote repository in git. To do this, open your Git command line interface and enter:
```
git remote add upstream https://github.com/qmk/qmk_firmware.git
```
?> The name `upstream` is arbitrary, but a common convention; you can give the QMK remote any name that suits you. Git's `remote` command uses the syntax `git remote add <name> <url>`, `<name>` being shorthand for the remote repo. This name can be used with many Git commands, including but not limited to `fetch`, `pull` and `push`, to specify the remote repo on which to act.
To verify that the repository has been added, run `git remote -v`, which should return the following:
```
$ git remote -v
origin https://github.com/<your_username>/qmk_firmware.git (fetch)
origin https://github.com/<your_username>/qmk_firmware.git (push)
upstream https://github.com/qmk/qmk_firmware.git (fetch)
upstream https://github.com/qmk/qmk_firmware.git (push)
```
Now that this is done, you can check for updates to the repo by running `git fetch upstream`. This retrieves the branches and tags &mdash; collectively referred to as "refs" &mdash; from the QMK repo, which now has the nickname `upstream`. We can now compare the data on our fork `origin` to that held by QMK.
To update your fork's master, run the following, hitting the Enter key after each line:
```
git checkout master
git fetch upstream
git pull upstream master
git push origin master
```
This switches you to your `master` branch, retrieves the refs from the QMK repo, downloads the current QMK `master` branch to your computer, and then uploads it to your fork.
## Making Changes
To make changes, create a new branch by entering:
```
git checkout -b dev_branch
git push --set-upstream origin dev_branch
```
This creates a new branch named `dev_branch`, checks it out, and then saves the new branch to your fork. The `--set-upstream` argument tells git to use your fork and the `dev_branch` branch every time you use `git push` or `git pull` from this branch. It only needs to be used on the first push; after that, you can safely use `git push` or `git pull`, without the rest of the arguments.
?> With `git push`, you can use `-u` in place of `--set-upstream` &mdash; `-u` is an alias for `--set-upstream`.
You can name your branch nearly anything you want, though it is recommended to name it something related to the changes you are going to make.
By default `git checkout -b` will base your new branch on the branch that is currently checked out. You can base your new branch on an existing branch that is not checked out by adding the name of the existing branch to the command:
```
git checkout -b dev_branch master
```
Now that you have a development branch, open your text editor and make whatever changes you need to make. It is recommended to make many small commits to your branch; that way, any change that causes issues can be more easily traced and undone if needed. To make your changes, edit and save any files that need to be updated, add them to Git's *staging area*, and then commit them to your branch:
```
git add path/to/updated_file
git commit -m "My commit message."
```
`git add` adds files that have been changed to Git's *staging area*, which is Git's "loading zone." This contains the changes that are going to be *committed* by `git commit`, which saves the changes to the repo. Use descriptive commit messages so you can know what was changed at a glance.
?> If you've changed multiple files, you can use `git add -- path/to/file1 path/to/file2 ...` to add all your desired files.
## Publishing Your Changes
The last step is to push your changes to your fork. To do this, enter `git push`. Git will then publish the current state of `dev_branch` to your fork.

Loading…
Cancel
Save