4,348
edits
→Delete a branch: Delete a remote branch that as no actual reference on the remote |
No edit summary |
||
(28 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:git}} | |||
== Setting the initial config variables == | |||
Set user name and email address. Without <code>--global</code> parameter it will save to the current project's .<code>/.git/config</code> file.<syntaxhighlight lang="bash"> | |||
$ git config --global user.name "Joe Shmoe" | |||
$ git config --global user.email "jshmoe@domain.com" | |||
</syntaxhighlight> | |||
<code>push.autoSetupRemote</code> is the variable to prevent the following message for a repo that hasn't been directly pulled from the remote: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
fatal: The current branch master has no upstream branch. | |||
To push the current branch and set the remote as upstream, use | |||
git push --set-upstream origin master | |||
To have this happen automatically for branches without a tracking | |||
upstream, see 'push.autoSetupRemote' in 'git help config'. | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ git | $ git config --global --add push.autoSetupRemote true | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Signing work == | |||
Get your GPG configured, and a personal key installed. Configure Git to use your personal key. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ git | $ git config --global user.signingkey 0A46826A | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Signing commits == | === Signing commits === | ||
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. | ||
Line 54: | Line 65: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Commands = | == Workflows == | ||
=== Making a pull request === | |||
1. Fork the project | |||
2. Clone your fork | |||
<syntaxhighlight lang="console"> | |||
$ git clone git@gitrepo.com:mhan/forkedproject.git | |||
</syntaxhighlight> | |||
3. Set up environment to keep fork updated | |||
Set up upstream remote: | |||
<syntaxhighlight lang="console"> | |||
$ git remote add upstream git@gitrepo.com:unmit/project.git | |||
</syntaxhighlight> | |||
Update your fork: | |||
<syntaxhighlight lang="console"> | |||
$ git fetch upstream | |||
$ git branch -va | |||
$ git checkout master | |||
$ git merge upstream/master | |||
</syntaxhighlight> | |||
4. Make updates of your own | |||
Create a new branch before making any changes | |||
<syntaxhighlight lang="console"> | |||
$ git checkout master | |||
$ git branch newfeature | |||
$ git checkout newfeature | |||
</syntaxhighlight> | |||
5. Submit a pull request | |||
Rebase your development branch against upstream master branch. | |||
<syntaxhighlight lang="console"> | |||
$ git fetch upstream | |||
$ git checkout master | |||
$ git merge upstream/master | |||
$ git checkout newfeature | |||
$ git rebase master | |||
</syntaxhighlight> | |||
Squash multiple commits into fewer commits | |||
<syntaxhighlight lang="console"> | |||
$ git checkout | |||
$ git rebase -i master | |||
</syntaxhighlight> | |||
Submit | |||
* After pushing to the forked project, select development branch and click the pull request button. | |||
== Accepting and merging a pull request == | |||
== Commands == | |||
=== Add === | |||
Add only modified and deleted files: | |||
<syntaxhighlight lang="console"> | |||
$ git add -u | |||
</syntaxhighlight> | |||
=== Tags === | |||
Signing tags: | |||
<syntaxhighlight lang="bash"> | |||
$ git tag -s v2.17 -m 'version 2.17 signed by MH' | |||
$ git show v2.17 | |||
</syntaxhighlight> | |||
With the signer's public key in the keyring, you can verify the tag: | |||
<syntaxhighlight lang="bash"> | |||
$ git tag -v v2.17 | |||
</syntaxhighlight> | |||
Upload a specific tag: | |||
<syntaxhighlight lang="bash"> | |||
$ git push origin v2.29.1 | |||
</syntaxhighlight> | |||
Upload all tags: | |||
<syntaxhighlight lang="bash"> | |||
$ git push origin --tags | |||
</syntaxhighlight> | |||
Remove a tag locally | |||
== Get a build number == | <syntaxhighlight lang="bash"> | ||
$ git tag -d <tagname> | |||
</syntaxhighlight> | |||
Remove a tag on the remote repo | |||
<syntaxhighlight lang="bash"> | |||
$ git push --delete origin v0.1.8 | |||
</syntaxhighlight> | |||
Get the latest tag in the current branch | |||
<syntaxhighlight lang="console"> | |||
$ 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] | |||
=== Clone a specific branch === | |||
<syntaxhighlight lang="bash"> | |||
$ git clone --depth 1 --single-branch -b 2.0.0 https://github.com/user/reponame.git | |||
</syntaxhighlight> | |||
=== Get a build number === | |||
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): | ||
Line 70: | Line 214: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Add a local branch that tracks a remote branch == | To get the number of commits without merges for develop branch: | ||
<syntaxhighlight lang="bash"> | |||
$ git rev-list --no-merges --count develop | |||
</syntaxhighlight> | |||
=== Add a local branch that tracks a remote branch === | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 78: | Line 228: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Pulling changes from a remote == | === Pulling changes from a remote === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 91: | Line 241: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Rebase a branch == | === Cleaning up remote branch list === | ||
This cleans up and removes any branches that are no longer on the remote | |||
<syntaxhighlight lang="bash"> | |||
$ git remote prune origin | |||
</syntaxhighlight> | |||
=== Rebase a branch === | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 106: | Line 264: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | === Start a new branch === | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ git checkout -b newbranch | |||
Switched to a new branch 'newbranch' | |||
</syntaxhighlight> | |||
=== Merge a branch === | |||
<syntaxhighlight lang="console"> | |||
$ git checkout autonotif | $ git checkout autonotif | ||
Switched to branch 'autonotif' | Switched to branch 'autonotif' | ||
Line 121: | Line 286: | ||
Merges <branchname> onto autonotif branch | Merges <branchname> onto autonotif branch | ||
== Delete a branch == | |||
=== Rename a branch === | |||
and update in the remote as well | |||
<syntaxhighlight lang="console"> | |||
$ git checkout <old_name> | |||
$ git branch -m <new_name> | |||
$ git push origin --delete <old_name> | |||
$ git push origin -u <new_name> | |||
</syntaxhighlight> | |||
=== Delete a branch === | |||
* 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 === | ||
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> | </syntaxhighlight> | ||
== Undo last commit == | === Undo last commit === | ||
This undo is recorded as a part of the git log. | This undo is recorded as a part of the git log. | ||
Line 175: | Line 349: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Fix or amend last commit == | === Fix or amend last commit === | ||
< | <syntaxhighlight lang="bash"> | ||
$ git commit --amend | $ git commit --amend | ||
</ | </syntaxhighlight> | ||
== Reset the branch to a specific point in the past == | === Undo changes to a file === | ||
<syntaxhighlight lang="bash"> | |||
$ git checkout -- filename | |||
</syntaxhighlight> | |||
=== Reset the branch to a specific point in the past === | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 189: | Line 369: | ||
--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. | ||
== Ignore files == | === Reset the branch to a specific branch on a remote === | ||
<syntaxhighlight lang="bash"> | |||
$ git reset --hard origin/master | |||
</syntaxhighlight> | |||
=== Ignore files === | |||
;Temporary ignore changes to a file | ;Temporary ignore changes to a file | ||
Line 209: | Line 395: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Ignore file modes == | === Ignore file modes === | ||
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 === | ||
Verify the connectivity and validity of the objects | Verify the connectivity and validity of the objects | ||
Line 249: | Line 435: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Useful git aliases = | == Useful git aliases == | ||
This is a snapshot of my ~/.gitconfig | This is a snapshot of my ~/.gitconfig | ||
Line 287: | Line 473: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Links = | == Quick Start Guide == | ||
* [[Git - Quick Start|Using Git for collaborating on text-based data files]] | |||
== Links == | |||
;http://qugstart.com/blog/ruby-and-rails/create-a-new-git-remote-repository-from-some-local-files-or-local-git-repository/ | ;http://qugstart.com/blog/ruby-and-rails/create-a-new-git-remote-repository-from-some-local-files-or-local-git-repository/ | ||
Line 307: | Line 497: | ||
'http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging | 'http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging | ||
=== Clients === | |||
* [https://sapling-scm.com/docs/introduction/getting-started/ Sapling] | |||
**[https://engineering.fb.com/2022/11/15/open-source/sapling-source-control-scalable/ Meta engineering blog entry (2022-11)] |