4,461
edits
mNo edit summary Tag: visualeditor |
mNo edit summary |
||
Line 11: | Line 11: | ||
Get your GPG configured, and a personal key installed. Configure Git to use your personal key. | Get your GPG configured, and a personal key installed. Configure Git to use your personal key. | ||
< | <syntaxhighlight lang="bash"> | ||
$ git config --global user.signingkey 0A46826A | $ git config --global user.signingkey 0A46826A | ||
</ | </syntaxhighlight> | ||
=== Signing commits === | === Signing commits === | ||
Line 19: | Line 19: | ||
You can sign commits simply by adding -S once your environment is configured. | You can sign commits simply by adding -S once your environment is configured. | ||
< | <syntaxhighlight lang="bash"> | ||
$ git commit -S -m 'push a signed commit' | $ git commit -S -m 'push a signed commit' | ||
</ | </syntaxhighlight> | ||
You can check and verify via <span class="package">git log</span>: | You can check and verify via <span class="package">git log</span>: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git log --show-signature -1 | $ git log --show-signature -1 | ||
</ | </syntaxhighlight> | ||
You can configure <span class="package">git log</span> to check any signatures and list them in output via <span class="package">%G?</span> format. | You can configure <span class="package">git log</span> to check any signatures and list them in output via <span class="package">%G?</span> format. | ||
< | <syntaxhighlight lang="bash"> | ||
$ git log --pretty="format:%h %G? %aN %s" | $ git log --pretty="format:%h %G? %aN %s" | ||
</ | </syntaxhighlight> | ||
You can also reject commits that are unsigned and invalid: | You can also reject commits that are unsigned and invalid: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git merge --verify-signature non-verify | $ git merge --verify-signature non-verify | ||
$ git merge --verify-signatures signed-branch | $ git merge --verify-signatures signed-branch | ||
</ | </syntaxhighlight> | ||
Sign the merge commit itself: | Sign the merge commit itself: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git merge --verify-signatures -S signed-branch | $ git merge --verify-signatures -S signed-branch | ||
</ | </syntaxhighlight> | ||
== Workflows == | == Workflows == | ||
Line 58: | Line 58: | ||
2. Clone your fork | 2. Clone your fork | ||
< | <syntaxhighlight lang="console"> | ||
$ git clone git@gitrepo.com:mhan/forkedproject.git | $ git clone git@gitrepo.com:mhan/forkedproject.git | ||
</ | </syntaxhighlight> | ||
Line 67: | Line 67: | ||
Set up upstream remote: | Set up upstream remote: | ||
< | <syntaxhighlight lang="console"> | ||
$ git remote add upstream git@gitrepo.com:unmit/project.git | $ git remote add upstream git@gitrepo.com:unmit/project.git | ||
</ | </syntaxhighlight> | ||
Update your fork: | Update your fork: | ||
< | <syntaxhighlight lang="console"> | ||
$ git fetch upstream | $ git fetch upstream | ||
$ git branch -va | $ git branch -va | ||
$ git checkout master | $ git checkout master | ||
$ git merge upstream/master | $ git merge upstream/master | ||
</ | </syntaxhighlight> | ||
Line 86: | Line 86: | ||
Create a new branch before making any changes | Create a new branch before making any changes | ||
< | <syntaxhighlight lang="console"> | ||
$ git checkout master | $ git checkout master | ||
$ git branch newfeature | $ git branch newfeature | ||
$ git checkout newfeature | $ git checkout newfeature | ||
</ | </syntaxhighlight> | ||
Line 97: | Line 97: | ||
Rebase your development branch against upstream master branch. | Rebase your development branch against upstream master branch. | ||
< | <syntaxhighlight lang="console"> | ||
$ git fetch upstream | $ git fetch upstream | ||
$ git checkout master | $ git checkout master | ||
Line 103: | Line 103: | ||
$ git checkout newfeature | $ git checkout newfeature | ||
$ git rebase master | $ git rebase master | ||
</ | </syntaxhighlight> | ||
Squash multiple commits into fewer commits | Squash multiple commits into fewer commits | ||
< | <syntaxhighlight lang="console"> | ||
$ git checkout | $ git checkout | ||
$ git rebase -i master | $ git rebase -i master | ||
</ | </syntaxhighlight> | ||
Line 127: | Line 127: | ||
Add only modified and deleted files: | Add only modified and deleted files: | ||
< | <syntaxhighlight lang="console"> | ||
$ git add -u | $ git add -u | ||
</ | </syntaxhighlight> | ||
=== Tags === | === Tags === | ||
Line 135: | Line 135: | ||
Signing tags: | Signing tags: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git tag -s v2.17 -m 'version 2.17 signed by MH' | $ git tag -s v2.17 -m 'version 2.17 signed by MH' | ||
$ git show v2.17 | $ git show v2.17 | ||
</ | </syntaxhighlight> | ||
With the signer's public key in the keyring, you can verify the tag: | With the signer's public key in the keyring, you can verify the tag: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git tag -v v2.17 | $ git tag -v v2.17 | ||
</ | </syntaxhighlight> | ||
Upload a specific tag: | Upload a specific tag: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git push origin v2.29.1 | $ git push origin v2.29.1 | ||
</ | </syntaxhighlight> | ||
Upload all tags: | Upload all tags: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git push origin --tags | $ git push origin --tags | ||
</ | </syntaxhighlight> | ||
Remove a tag locally | Remove a tag locally | ||
< | <syntaxhighlight lang="bash"> | ||
$ git tag -d <tagname> | $ git tag -d <tagname> | ||
</ | </syntaxhighlight> | ||
Remove a tag on the remote repo | Remove a tag on the remote repo | ||
< | <syntaxhighlight lang="bash"> | ||
$ git push --delete origin v0.1.8 | $ git push --delete origin v0.1.8 | ||
</ | </syntaxhighlight> | ||
Get the latest tag in the current branch | Get the latest tag in the current branch | ||
< | <syntaxhighlight lang="console"> | ||
$ git describe --abbrev=0 --tags | $ git describe --abbrev=0 --tags | ||
</ | </syntaxhighlight> | ||
* Reference: [https://www.xspdf.com/resolution/50066178.html#:~:text=Git%20get%20latest%20tag%20on%20current%20branch&text=To%20get%20the%20latest%20annotated,%2Dmatch%20%2D%2Dabbrev%3D0. Tag-related commands] | * Reference: [https://www.xspdf.com/resolution/50066178.html#:~:text=Git%20get%20latest%20tag%20on%20current%20branch&text=To%20get%20the%20latest%20annotated,%2Dmatch%20%2D%2Dabbrev%3D0. Tag-related commands] | ||
Line 180: | Line 180: | ||
=== Clone a specific branch === | === Clone a specific branch === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git clone --depth 1 --single-branch -b 2.0.0 https://github.com/user/reponame.git | $ git clone --depth 1 --single-branch -b 2.0.0 https://github.com/user/reponame.git | ||
</ | </syntaxhighlight> | ||
=== Get a build number === | === Get a build number === | ||
Line 188: | Line 188: | ||
A commit count can be used as a build number for a revision (e.g. HEAD, master, a commit hash): | A commit count can be used as a build number for a revision (e.g. HEAD, master, a commit hash): | ||
< | <syntaxhighlight lang="bash"> | ||
$ git rev-list --count <revision> | $ git rev-list --count <revision> | ||
</ | </syntaxhighlight> | ||
To get the commit count across all branches: | To get the commit count across all branches: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git rev-list --all --count | $ git rev-list --all --count | ||
</ | </syntaxhighlight> | ||
To get the number of commits without merges for develop branch: | To get the number of commits without merges for develop branch: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git rev-list --no-merges --count develop | $ git rev-list --no-merges --count develop | ||
</ | </syntaxhighlight> | ||
=== Add a local branch that tracks a remote branch === | === Add a local branch that tracks a remote branch === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git branch --track greet origin/greet | $ git branch --track greet origin/greet | ||
$ git branch -a | $ git branch -a | ||
$ git merge origin/master | $ git merge origin/master | ||
</ | </syntaxhighlight> | ||
=== Pulling changes from a remote === | === Pulling changes from a remote === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git fetch | $ git fetch | ||
$ git merge origin/master | $ git merge origin/master | ||
</ | </syntaxhighlight> | ||
is equivalent to: | is equivalent to: | ||
< | <syntaxhighlight lang="bash"> | ||
$ git pull | $ git pull | ||
</ | </syntaxhighlight> | ||
=== Cleaning up remote branch list === | === Cleaning up remote branch list === | ||
Line 229: | Line 229: | ||
This cleans up and removes any branches that are no longer on the remote | This cleans up and removes any branches that are no longer on the remote | ||
< | <syntaxhighlight lang="bash"> | ||
$ git remote prune origin | $ git remote prune origin | ||
</ | </syntaxhighlight> | ||
=== Rebase a branch === | === Rebase a branch === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git checkout cbranch | $ git checkout cbranch | ||
Switched to branch 'cbranch' | Switched to branch 'cbranch' | ||
Line 246: | Line 246: | ||
$ | $ | ||
</ | </syntaxhighlight> | ||
=== Start a new branch === | === Start a new branch === | ||
< | <syntaxhighlight lang="console"> | ||
$ git checkout -b newbranch | $ git checkout -b newbranch | ||
Switched to a new branch 'newbranch' | Switched to a new branch 'newbranch' | ||
</ | </syntaxhighlight> | ||
=== Merge a branch === | === Merge a branch === | ||
< | <syntaxhighlight lang="console"> | ||
$ git checkout autonotif | $ git checkout autonotif | ||
Switched to branch 'autonotif' | Switched to branch 'autonotif' | ||
Line 266: | Line 266: | ||
1 file changed, 1 insertion(+) | 1 file changed, 1 insertion(+) | ||
create mode 100644 foobarrecords_savenew.hns | create mode 100644 foobarrecords_savenew.hns | ||
</ | </syntaxhighlight> | ||
Merges <branchname> onto autonotif branch | Merges <branchname> onto autonotif branch | ||
Line 275: | Line 275: | ||
and update in the remote as well | and update in the remote as well | ||
< | <syntaxhighlight lang="console"> | ||
$ git checkout <old_name> | $ git checkout <old_name> | ||
Line 283: | Line 283: | ||
$ git push origin -u <new_name> | $ git push origin -u <new_name> | ||
</ | </syntaxhighlight> | ||
=== Delete a branch === | === Delete a branch === | ||
Line 289: | Line 289: | ||
* Delete a local branch | * Delete a local branch | ||
< | <syntaxhighlight lang="bash"> | ||
$ git branch -d hotpatch1 | $ git branch -d hotpatch1 | ||
</ | </syntaxhighlight> | ||
* Delete a remote branch | * Delete a remote branch | ||
< | <syntaxhighlight lang="bash"> | ||
git push origin --delete hotpatch1 | git push origin --delete hotpatch1 | ||
</ | </syntaxhighlight> | ||
* Delete a remote branch that as no actual reference on the remote | * Delete a remote branch that as no actual reference on the remote | ||
< | <syntaxhighlight lang="bash"> | ||
git branch -r -d origin/hotpatch1 | git branch -r -d origin/hotpatch1 | ||
</ | </syntaxhighlight> | ||
=== List merged branches === | === List merged branches === | ||
Line 309: | Line 309: | ||
List branches merged into master | List branches merged into master | ||
< | <syntaxhighlight lang="bash"> | ||
$ git branch --merged master | $ git branch --merged master | ||
</ | </syntaxhighlight> | ||
List branches merged into HEAD (i.e. tip of current branch) | List branches merged into HEAD (i.e. tip of current branch) | ||
< | <syntaxhighlight lang="bash"> | ||
$ git branch --merged | $ git branch --merged | ||
</ | </syntaxhighlight> | ||
List branches that have not been merged | List branches that have not been merged | ||
< | <syntaxhighlight lang="bash"> | ||
$ git branch --no-merged | $ git branch --no-merged | ||
</ | </syntaxhighlight> | ||
=== Undo last commit === | === Undo last commit === | ||
Line 329: | Line 329: | ||
This undo is recorded as a part of the git log. | This undo is recorded as a part of the git log. | ||
< | <syntaxhighlight lang="bash"> | ||
$ git revert HEAD | $ git revert HEAD | ||
</ | </syntaxhighlight> | ||
=== Fix or amend last commit === | === Fix or amend last commit === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git commit --amend | $ git commit --amend | ||
</ | </syntaxhighlight> | ||
=== Undo changes to a file === | === Undo changes to a file === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git checkout -- filename | $ git checkout -- filename | ||
</ | </syntaxhighlight> | ||
=== Reset the branch to a specific point in the past === | === Reset the branch to a specific point in the past === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git reset --hard <hash> | $ git reset --hard <hash> | ||
</ | </syntaxhighlight> | ||
--hard parameter also updates the working directory. Reset shouldn't be used in a group project on a remote repository because of its potential for confusion. | --hard parameter also updates the working directory. Reset shouldn't be used in a group project on a remote repository because of its potential for confusion. | ||
Line 355: | Line 355: | ||
=== Reset the branch to a specific branch on a remote === | === Reset the branch to a specific branch on a remote === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git reset --hard origin/master | $ git reset --hard origin/master | ||
</ | </syntaxhighlight> | ||
=== Ignore files === | === Ignore files === | ||
Line 363: | Line 363: | ||
;Temporary ignore changes to a file | ;Temporary ignore changes to a file | ||
< | <syntaxhighlight lang="bash"> | ||
$ git update-index --assume-unchanged <filename> | $ git update-index --assume-unchanged <filename> | ||
</ | </syntaxhighlight> | ||
;Back to tracking changes to the file | ;Back to tracking changes to the file | ||
< | <syntaxhighlight lang="bash"> | ||
$ git update-index --no-assume-unchanged <filename> | $ git update-index --no-assume-unchanged <filename> | ||
</ | </syntaxhighlight> | ||
;Get a list of folders and files that are <code>assume-unchanged</code> | ;Get a list of folders and files that are <code>assume-unchanged</code> | ||
< | <syntaxhighlight lang="bash"> | ||
$ git ls-files -v | grep '^h' | $ git ls-files -v | grep '^h' | ||
</ | </syntaxhighlight> | ||
=== Ignore file modes === | === Ignore file modes === | ||
Line 383: | Line 383: | ||
This is useful when working with multiple file systems. | This is useful when working with multiple file systems. | ||
< | <syntaxhighlight lang="bash"> | ||
$ git config core.filemode false | $ git config core.filemode false | ||
</ | </syntaxhighlight> | ||
=== Maintenance command === | === Maintenance command === | ||
Line 391: | Line 391: | ||
Verify the connectivity and validity of the objects | Verify the connectivity and validity of the objects | ||
< | <syntaxhighlight lang="bash"> | ||
$ git fsck --unreachable | $ git fsck --unreachable | ||
</ | </syntaxhighlight> | ||
Manage reflog information | Manage reflog information | ||
< | <syntaxhighlight lang="bash"> | ||
$ git reflog expire --expire=0 --all | $ git reflog expire --expire=0 --all | ||
</ | </syntaxhighlight> | ||
Pack unpacked objects in a repository | Pack unpacked objects in a repository | ||
< | <syntaxhighlight lang="bash"> | ||
$ git repack -a -d -l | $ git repack -a -d -l | ||
</ | </syntaxhighlight> | ||
Prune all unreachable objects from the object database | Prune all unreachable objects from the object database | ||
< | <syntaxhighlight lang="bash"> | ||
$ git prune | $ git prune | ||
</ | </syntaxhighlight> | ||
Clean up unnecessary files and optimize the local repository | Clean up unnecessary files and optimize the local repository | ||
< | <syntaxhighlight lang="bash"> | ||
$ git gc --aggressive | $ git gc --aggressive | ||
</ | </syntaxhighlight> | ||
== Useful git aliases == | == Useful git aliases == | ||
Line 423: | Line 423: | ||
This is a snapshot of my ~/.gitconfig | This is a snapshot of my ~/.gitconfig | ||
< | <syntaxhighlight lang="ini"> | ||
[user] | [user] | ||
name = Michael Han | name = Michael Han | ||
Line 455: | Line 455: | ||
[merge] | [merge] | ||
defaultToUpstream = true | defaultToUpstream = true | ||
</ | </syntaxhighlight> | ||
== Quick Start Guide == | == Quick Start Guide == |