git.md (2978B)
1 # git 2 3 undo merge that hasn't been pushed 4 ```zsh 5 git reset --merge HEAD~1 6 ``` 7 8 roll back hard 9 ``` 10 git reset --hard <commit/tag> 11 ``` 12 13 force push of a previous commit 14 ``` 15 git push -f origin <commit_id>:<branch> 16 ``` 17 18 delete remote branch 19 ``` 20 git push --delete origin <branch> 21 ``` 22 23 reset local branch after a forced-update (above) 24 ``` 25 git fetch 26 git reset origin/<branch> --hard 27 ``` 28 29 renaming branch and updating remote 30 ``` 31 git branch -m old-name new-name 32 git push origin --set-upstream new-name 33 git push origin :old-name 34 ``` 35 36 set username for [single repo][] 37 ``` 38 git config user.username 'name' 39 ``` 40 41 set signing key for local repo 42 ``` 43 git config user.signingkey <id> 44 ``` 45 46 [signing][] commits 47 ``` 48 git commit -S -m 'msg' 49 ``` 50 51 compare diff between two commits 52 ``` 53 git diff <commit>...<commit> 54 ``` 55 56 compare file diff between two branches 57 ``` 58 git diff newbranch main -- file.txt 59 git diff newbranch..main -- file.txt 60 ``` 61 using the latter syntax, if either side is `HEAD` it may be ommitted (e.g., `main..` compares `main` to `HEAD`) 62 63 adding separate hunks from file 64 ``` 65 git add --patch <filename> 66 git add -p <filename> 67 ``` 68 69 options: 70 * `y` stage this hunk for the next commit 71 * `n` do not stage this hunk for the next commit 72 * `q` quit; do not stage this hunk or any of the remaining hunks 73 * `a` stage this hunk and all later hunks in the file 74 * `d` do not stage this hunk or any of the later hunks in the file 75 * `g` select a hunk to go to 76 * `/` search for a hunk matching the given regex 77 * `j` leave this hunk undecided, see next undecided hunk 78 * `J` leave this hunk undecided, see next hunk 79 * `k` leave this hunk undecided, see previous undecided hunk 80 * `K` leave this hunk undecided, see previous hunk 81 * `s` split the current hunk into smaller hunks 82 * `e` manually edit the current hunk 83 * You can then edit the hunk manually by replacing `+`/`-` by `#` 84 * `?` print hunk help 85 86 stash 87 ``` 88 git stash 89 git stash show 90 ``` 91 92 unstash 93 ``` 94 git stash pop 95 ``` 96 97 add remote origin 98 ``` 99 git remote add origin git@gitserver/path/to/repo 100 ``` 101 102 add multiple push repos 103 ``` 104 git remote set-url --add --push origin git@gitserver/original/repo 105 git remote set-url --add --push origin https://gitserver/another/repo 106 ``` 107 108 archive branch 109 ``` 110 git archive --format zip --outpu /path/to/output.zip <branch> 111 ``` 112 113 ## using `hub` 114 ### pull requests 115 ``` 116 hub pr list 117 hub pr checkout <num> 118 ``` 119 120 ## helpful links 121 122 [making a pull request][] 123 124 [branching and rebasing][] 125 126 [branching model][] 127 128 [merging and rebasing][] 129 130 [making a pull request]: https://www.atlassian.com/git/tutorials/making-a-pull-request 131 [branching and rebasing]: https://git-scm.com/book/en/v2/Git-Branching-Rebasing 132 [branching model]: https://nvie.com/posts/a-successful-git-branching-model/ 133 [single repo]: https://help.github.com/articles/setting-your-username-in-git/ 134 [merging and rebasing]: https://www.atlassian.com/git/tutorials/merging-vs-rebasing 135 [signing]: https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work