Post

Git CLI

Git CLI

workflow

  • basic commands
    1
    2
    3
    4
    5
    
    git pull
    git add .
    git commit -m "[commit-message]"
    git status # check
    git push
    
  • amend local commit message in vim mode
    1
    
    git commit --amend
    
  • clear the Git cache and apply new or updated .gitignore rules
    1
    2
    3
    4
    
    # ... commit current change beforehand
    git rm -r --cached .
    git add .
    git commit -m "Applied .gitignore rules and cleared cache"
    

    setup

  • clone using ssh to commit to a repo
    1
    
    ssh-keygen -t ed25519 -C "your_email@example.com"
    
  • init a local folder
    1
    
    git init
    
  • set before first time commit in the current repo
    1
    2
    
    git config --global user.name [name]
    git config --global user.mail [mail-address]
    

edit stage

  • detailed changes that is not staged
    1
    
    git diff
    
  • staged but yet commit
    1
    
    git diff --staged
    
  • check modified files that is staged for next commit
    1
    
    git status
    
  • add a file for next staged
    1
    
    git add <file-name>
    
  • remove a file from current stage
    1
    
    git reset <file-name>
    

commit message

convention

  • feat: add, adjust, remove features
  • fix: fix bugs
  • test: add or correct tests
  • build: build related
  • docs: exclusively modify documentation
  • opt: operational components: backup, recovery procedures…
  • chores: initalize, others…

    not affecting application behavior

  • refactor: rewrite or restructure
  • perf: improve performance
  • style: code style

branching

official guild

1
2
3
4
5
6
- create a new branch to try out new idea
- do a few commits
- switch back to where you branched from
- solve conflicts
- merge the new branch
- if it's not going to work -> delete it

branch role

  • mainor master: production
  • develop: where you merge work into for testing features
  • smaller ones: features…

branching cli

  • check current commit log
    1
    
    git log
    
  • list existing branch
    1
    
    git branch
    
  • create a new branch
    1
    
    git branch <new-branch-name>
    
  • delete a branch
    1
    
    git branch -d <branch-name>
    
  • check out another branch
    1
    
    git checkout <branch-name>
    
  • merge specified branch into current one -> add all the commit to current branch
    1
    
    git merge <specified-branch>
    

remote version control

  • fetch changes from remote repo
    1
    
    git fetch
    
  • merge: preserving the full history of both
  • rebase: rewrites history, which can be problematic in shared branches if others have already based their work on the original commits.
1
2
git merge
git rebase
  • fetch + merge those changes into local repo
  • fetch + rebase
    1
    2
    
    git pull
    git pull --rebase
    

SSH keypair detail

set up ssh keypair for github account authentication official tutorial

generate ssh keypair

1
ssh-keygen -t rsa -b 4096 -C "youremail@example.com"

This command will create two files, id_rsa and id_rsa.pub under /User/<username>/.ssh.

We need to copy public key to github account setting https://github.com/settings/keys

1
pbcopy < /User/<username>/.ssh/id_rsa.pub

pbcopy pasteboard: you can press C^v at other places to paste the content of the specified file

test the key

1
ssh -T git@github.com

you should see the welcome message

change remote

1
git remote set-url origin git@github.com:organization/your-repo.git

check remote

1
git remote -v

now we can push without password authenticatoin

1
git push
This post is licensed under CC BY 4.0 by the author.

Trending Tags