Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| git [2023/05/04 15:08] – mgupton | git [2024/12/13 12:37] (current) – [Branches] mgupton | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======Git====== | + | ======git====== |
| A general guide for the distributed version control system Git. | A general guide for the distributed version control system Git. | ||
| Line 11: | Line 11: | ||
| git config --global user.email your@email.com | git config --global user.email your@email.com | ||
| git config --global core.excludesFile ~/ | git config --global core.excludesFile ~/ | ||
| + | git config --global init.defaultBranch main | ||
| </ | </ | ||
| Line 25: | Line 26: | ||
| Since a project that is just starting tends to be rudimentary it makes sense to initially create the repo with an empty master branch and then immediately create a //working// branch where the initial work will begin. Once the project gets further along and hits certain milestones the changes can be merged into the master branch. | Since a project that is just starting tends to be rudimentary it makes sense to initially create the repo with an empty master branch and then immediately create a //working// branch where the initial work will begin. Once the project gets further along and hits certain milestones the changes can be merged into the master branch. | ||
| - | * '' | + | * '' |
| Below is an example using Powershell to only run commands if there is not an existing Git repo configured. | Below is an example using Powershell to only run commands if there is not an existing Git repo configured. | ||
| Line 36: | Line 37: | ||
| if (-not(Test-Path -Path " | if (-not(Test-Path -Path " | ||
| - | git init | + | git init --initial-branch=main |
| git config --global user.name "John Doe" | git config --global user.name "John Doe" | ||
| git config --global user.email $email | git config --global user.email $email | ||
| git config --global core.excludesFile ~/ | git config --global core.excludesFile ~/ | ||
| - | git commit --allow-empty -m " | + | git commit --allow-empty -m " |
| git checkout -b working | git checkout -b working | ||
| } | } | ||
| Line 55: | Line 56: | ||
| *.pyc | *.pyc | ||
| __pycache__/ | __pycache__/ | ||
| - | misc/ | + | **/.working/* |
| - | working/ | + | |
| - | support/ | + | |
| </ | </ | ||
| Line 82: | Line 81: | ||
| <code bash> | <code bash> | ||
| git diff HEAD | git diff HEAD | ||
| - | git add -A | + | git add -u |
| git commit -m " | git commit -m " | ||
| git fetch | git fetch | ||
| Line 91: | Line 90: | ||
| </ | </ | ||
| - | ==== Seeing Changes ==== | + | ====== Seeing Changes |
| See what's new or modified in the working directory. | See what's new or modified in the working directory. | ||
| <code bash> | <code bash> | ||
| Line 97: | Line 96: | ||
| </ | </ | ||
| - | List files that were part of the specified commit. | + | List files that were part of the specified commit. |
| <code bash> | <code bash> | ||
| - | git ls-tree --name-only -r $commit | + | git ls-tree --full-tree --name-only -r $commit |
| </ | </ | ||
| Show the details, including changes made, for the specified commit. | Show the details, including changes made, for the specified commit. | ||
| <code bash> | <code bash> | ||
| - | git show --name-only | + | git show $commit |
| </ | </ | ||
| Line 112: | Line 111: | ||
| </ | </ | ||
| - | * git diff: Show differences between your working directory and the index. | + | * '' |
| - | * git diff –cached: Show differences between the index and the most recent commit. | + | * '' |
| - | * git diff HEAD: Show the differences between your working directory and the most recent commit. | + | * '' |
| + | * '' | ||
| ==== History ==== | ==== History ==== | ||
| Line 122: | Line 121: | ||
| </ | </ | ||
| - | ====Branches==== | + | ====== Branches ====== |
| + | * Checkout the branch the new branch is based on | ||
| + | * Then create branch | ||
| + | <code bash> | ||
| + | git branch <new branch name> | ||
| + | </ | ||
| + | * Create a ranch and check it out in one command | ||
| <code bash> | <code bash> | ||
| git checkout -b <new branch name> | git checkout -b <new branch name> | ||
| Line 128: | Line 133: | ||
| === Deleting Branches === | === Deleting Branches === | ||
| + | * Delete remote branch, if it exists | ||
| + | * Then delete the local branch | ||
| <code bash> | <code bash> | ||
| - | git push -d < | + | git push -d < |
| - | git branch -d <branchname> | + | git branch -d <branch name> |
| </ | </ | ||
| - | ==== Remotes ==== | + | To force delete an unmerged branch |
| + | <code bash> | ||
| + | git branch -D <branch name> | ||
| + | </ | ||
| + | |||
| + | ====== Remotes | ||
| Push branch to remote repo | Push branch to remote repo | ||
| <code bash> | <code bash> | ||
| Line 139: | Line 151: | ||
| </ | </ | ||
| + | ====== Squashing Commits ====== | ||
| + | To tidy up a messy trail of commits | ||
| + | ====Squash last n commits to one==== | ||
| + | < | ||
| + | git reset --soft HEAD~n && git commit -m 'new commit message' | ||
| + | git push < | ||
| + | </ | ||
| ====== Archive/ | ====== Archive/ | ||