jeremystretch 3 лет назад
Родитель
Сommit
f98f1647da
1 измененных файлов с 72 добавлено и 20 удалено
  1. 72 20
      docs/development/git-cheat-sheet.md

+ 72 - 20
docs/development/git-cheat-sheet.md

@@ -23,6 +23,29 @@ Receiving objects: 100% (95112/95112), 60.40 MiB | 45.82 MiB/s, done.
 Resolving deltas: 100% (74979/74979), done.
 ```
 
+### Pull New Commits
+
+To update your local branch with any recent upstream commits, run `git pull`.
+
+``` title="Command"
+git pull
+```
+
+``` title="Example"
+$ git pull
+remote: Enumerating objects: 1, done.
+remote: Counting objects: 100% (1/1), done.
+remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
+Unpacking objects: 100% (1/1), done.
+From https://github.com/netbox-community/netbox
+   28bc76695..e0741cc9a  develop    -> origin/develop
+Updating 28bc76695..e0741cc9a
+Fast-forward
+ docs/release-notes/version-3.3.md | 1 +
+ netbox/netbox/settings.py         | 1 +
+ 2 files changed, 2 insertions(+)
+```
+
 ### List Branches
 
 `git branch` lists all local branches. Appending `-a` to this command will list both local (green) and remote (red) branches.
@@ -198,6 +221,36 @@ $ git commit -m "Fixes #123: Fixed the thing that was broken"
 !!! tip "Automatically Closing Issues"
     GitHub will [automatically close](https://github.blog/2013-01-22-closing-issues-via-commit-messages/) any issues referenced in a commit message by `Fixes:` or `Closes:` when the commit is merged into the repository's default branch. Contributors are strongly encouraged to follow this convention when forming commit messages. (Use "Closes" for feature requests and "Fixes" for bugs.)
 
+### Push a Commit Upstream
+
+Once you've made a commit locally, it needs to be pushed upstream to the _remote_ repository (typically called "origin"). This is done with the `git push` command. If this is a new branch that doesn't yet exist on the remote repository, you'll need to set the upstream for it when pushing.
+
+``` title="Command"
+git push -u origin $branchname
+```
+
+``` title="Example"
+$ git push -u origin testing
+Counting objects: 3, done.
+Delta compression using up to 16 threads.
+Compressing objects: 100% (3/3), done.
+Writing objects: 100% (3/3), 377 bytes | 377.00 KiB/s, done.
+Total 3 (delta 2), reused 0 (delta 0)
+remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
+remote: 
+remote: Create a pull request for 'testing' on GitHub by visiting:
+remote:      https://github.com/netbox-community/netbox/pull/new/testing
+remote: 
+To https://github.com/netbox-community/netbox
+ * [new branch]          testing -> testing
+Branch 'testing' set up to track remote branch 'testing' from 'origin'.
+```
+
+!!! info
+    If this branch already exists on the remote repository, `git push` is sufficient.
+
+## Fixing Mistakes
+
 ### Modify the Previous Commit
 
 Sometimes you'll find that you've overlooked a necessary change and need to commit again. If you haven't pushed your most recent commit and just need to make a small tweak or two, you can _amend_ your most recent commit instead of creating a new one.
@@ -218,33 +271,32 @@ $ git commit --amend --no-edit
  create mode 100644 newfile.py
 ```
 
-!!! warning "Don't Amend After Pushing"
+!!! danger "Don't Amend After Pushing"
     Never amend a commit you've already pushed upstream, as doing so will break the commit tree. Create a new commit instead.
 
-### Push a Commit Upstream
+### Undo the Last Commit
 
-Once you've made a commit locally, it needs to be pushed upstream to the _remote_ repository (typically called "origin"). This is done with the `git push` command. If this is a new branch that doesn't yet exist on the remote repository, you'll need to set the upstream for it when pushing.
+The `git reset` command can be used to undo the most recent commit. (`HEAD~` is equivalent to `HEAD~1` and references the commit prior to the current HEAD.) After making and staging your changes, commit using `-c ORIG_HEAD` to replace the erroneous commit.
 
 ``` title="Command"
-git push -u origin $branchname
+git reset HEAD~
 ```
 
 ``` title="Example"
-$ git push -u origin testing
-Counting objects: 3, done.
-Delta compression using up to 16 threads.
-Compressing objects: 100% (3/3), done.
-Writing objects: 100% (3/3), 377 bytes | 377.00 KiB/s, done.
-Total 3 (delta 2), reused 0 (delta 0)
-remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
-remote: 
-remote: Create a pull request for 'testing' on GitHub by visiting:
-remote:      https://github.com/netbox-community/netbox/pull/new/testing
-remote: 
-To https://github.com/netbox-community/netbox
- * [new branch]          testing -> testing
-Branch 'testing' set up to track remote branch 'testing' from 'origin'.
+$ git add -A
+$ git commit -m "Erroneous commit"
+[testing 09ce06736] Erroneous commit
+ Date: Mon Aug 29 15:20:04 2022 -0400
+ 1 file changed, 1 insertion(+)
+ create mode 100644 BADCHANGE
+$ git reset HEAD~
+$ rm BADFILE
+$ git add -A
+$ git commit -m "Fixed commit"
+[testing c585709f3] Fixed commit
+ Date: Mon Aug 29 15:22:38 2022 -0400
+ 1 file changed, 65 insertions(+), 20 deletions(-)
 ```
 
-!!! info
-    If this branch already exists on the remote repository, `git push` is sufficient.
+!!! danger "Don't Reset After Pushing"
+    Resetting only works until you've pushed your local changes upstream. If you've already pushed upstream, use `git revert` instead. This will create a _new_ commit that reverts the erroneous one, but ensures that the git history remains intact.