Git + Git Hub Guide#01
Branches, Processes, Cheat Sheet, and Useful Commands
Summary
- Git Branches
- Git Process
- Git Commands
- Git SSH Setup
- Git LFS
- Git Tips
- Commit Messages
- Git File Config .gitconfig
- Octokit a GitHub API Tool
- GitHub Pull Request
- GitHub Issue
- GitHub CLI
- GitHub Hub
- References
1. Git Branches
2.Git Process
3. Git Commands
4. Git SSH Setup
a. Ubuntu
ssh-keygen-o
❯ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/home/biolabs/.ssh/id_rsa):
/home/biolabs/.ssh/id_rsa already exists.
Overwrite (y/n)?
(base) biolabs in ~/.ssh took 2s
b. Windows
$ ssh-keygen -t rsa -b 4096
5. Git LFS
a. Upload Files
git lfs install
git lfs track "*.psd"
git add .gitattributesgit add file.psd
git commit -m "Add design file"
git push origin main
b. Download Files
git lfs fetch
git lfs pull
c. Setup Ubuntu
sudo apt-get install git-lfs
git lfs install
6. Git Tips
a) How to change the commit message?
git commit -m “First Commmit” // wrong commit message
git commit -m “First Commit” — ammend
b) Git Reset
- Hard
- Soft
git reset --soft HEAD~3
c) Git Pager
git config --global pager.log truegit config --global pager.log fale
d) Git Garbage Collector
git gc // Garbage Collector
e) Git Rebase
f) Git Merge
g) BFG Repo Cleaner
written in Scala
$ bfg --strip-blobs-bigger-than 100M --replace-text banned.txt repo.git
an alternative to git-filter-branch
The BFG is a simpler, faster alternative to git-filter-branch
for cleansing bad data out of your Git repository history:
- Removing Crazy Big Files
- Removing Passwords, Credentials & other Private data
The git-filter-branch
command is enormously powerful and can do things that the BFG can't - but the BFG is much better for the tasks above, because:
- Faster : 10–720x faster
- Simpler : The BFG isn’t particularily clever, but is focused on making the above tasks easy
- Beautiful : If you need to, you can use the beautiful Scala language to customise the BFG. Which has got to be better than Bash scripting at least some of the time.
Usage
First clone a fresh copy of your repo, using the --mirror
flag:
$ git clone --mirror git://example.com/some-big-repo.git
This is a bare repo, which means your normal files won’t be visible, but it is a full copy of the Git database of your repository, and at this point you should make a backup of it to ensure you don’t lose anything.
Now you can run the BFG to clean your repository up:
$ java -jar bfg.jar --strip-blobs-bigger-than 100M some-big-repo.git
The BFG will update your commits and all branches and tags so they are clean, but it doesn’t physically delete the unwanted stuff. Examine the repo to make sure your history has been updated, and then use the standard git gc
command to strip out the unwanted dirty data, which Git will now recognise as surplus to requirements:
$ cd some-big-repo.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
Finally, once you’re happy with the updated state of your repo, push it back up (note that because your clone command used the --mirror
flag, this push will update all refs on your remote server):
$ git push
7. Commit Messages
Example
feat: add hat wobble
^--^ ^------------^
| |
| +-> Summary in present tense.
|
+-------> Type: chore, docs, feat, fix, refactor, style, or test.
More Examples:
feat
: (new feature for the user, not a new feature for build script)fix
: (bug fix for the user, not a fix to a build script)docs
: (changes to the documentation)style
: (formatting, missing semi colons, etc; no production code change)refactor
: (refactoring production code, eg. renaming a variable)test
: (adding missing tests, refactoring tests; no production code change)chore
: (updating grunt tasks etc; no production code change)
8. Git File Config .gitconfig
[user]email = xx@gmail.comname = xxx[filter "lfs"]clean = git-lfs clean -- %fsmudge = git-lfs smudge -- %fprocess = git-lfs filter-processrequired = true[alias]lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit#s = !git status -smudge#c = !git add --all && git commit -m#l = !git log --graph --pretty=format:'%C(blue)%h %C(blue)%d %C(white)%s - %C(cyan)%cn, %C(green)%crs = status -sc = add --all && git commit -ml = log --graph --pretty=format:'%C(blue)%h %C(red)%d %C(white)%s - %C(cyan)%cn, %C(green)%cr'editor = config --global --edit[core]editor = code --wait[init]defaultBranch = main[pager]status = truediff = falselog = false
9. Octokit a GitHub API Tool
- Official clients for the GitHub API
Open a Pull Request via the GitHub API
npm install --save-dev @octokit/core
import { Octokit } from "@octokit/core";
2
3const octokit = new Octokit({ auth: 'your-token!' }),
4 owner = 'test-user',
5 repo = 'test-repo',
6 title = 'My Test Pull Request',
7 body = 'This pull request is a test!',
8 head = 'my-feature-branch',
9 base = 'develop-branch';
10
11const response = await octokit.request(
12 `POST /repos/{owner}/{repo}/pulls`, { owner, repo, title, body, head, base }
13);
10. GitHub Pull Request
GitHub Pull Request Script
11. GitHub Issue
12. GitHub CLI
Installation
conda install gh — channel conda-forge
Conda
Install:conda install gh --channel conda-forge
Upgrade:conda update gh --channel conda-forge
gh
Work seamlessly with GitHub from the command line.
Core commands
Actions commands
Additional commands
Options
--version
Show gh version
Examples
$ gh issue create $ gh repo clone cli/cli $ gh pr checkout 321
13. GitHub Hub
# clone your own project
hub clone dotfiles
→ git clone git://github.com/YOUR_USER/dotfiles.git
# clone another project
hub clone github/hub
→ git clone git://github.com/github/hub.git
# fast-forward all local branches to match the latest state on the remote
cd myproject
hub sync
# list latest open issues in the current repository
hub issue --limit 10
# open the current project's issues page
hub browse -- issues
→ open https://github.com/github/hub/issues
# open another project's wiki
hub browse rbenv/ruby-build wiki
→ open https://github.com/rbenv/ruby-build/wiki
# share log output via Gist
hub gist create --copy build.log
→ (the URL of the new private gist copied to clipboard)
Starting a new project has never been easier:
# create a repo to host a new project on GitHub
git init
git add .
git commit -m "And so, it begins."
hub create
→ (creates a new GitHub repository with the name of the current directory)
git push -u origin HEAD
Lowering the barrier to contributing to open-source
Whether you are beginner or an experienced contributor to open-source, hub makes it easier to fork repositories, check the CI status of a branch, and even submit pull requests from the same environment where you write & commit your code.
hub clone octocat/Spoon-Knife
cd Spoon-Knife
# create a topic branch
git checkout -b feature
# make some changes...
git commit -am "done with feature"# It's time to fork the repo!
hub fork --remote-name origin
→ (forking repo on GitHub...)
→ git remote add origin git@github.com:YOUR_USER/Spoon-Knife.git# push the changes to your new remote
git push origin feature# check the CI status for this branch
hub ci-status --verbose# open a pull request for the branch you've just pushed
hub pull-request
→ (opens a text editor for your pull request message)