git
Some notes for git for better memory
Git is open source control management or what's referred to as SCM.
- Working file: your editable files
- Track: keep an eye of the files
- Stage: ready for commit
- Commit: take a snap shot
Git
Basics
Initialize a git project:
1 | git init |
Track and Stage:
git add fileName.Track or stage all the files:
git add .orgit add --allorgit add -AUntrack:
git rm --cached fileName, git will not track it anymoreUnstage:
git resotre --staged fileName, retrieve from the stageIgnore:
.gitignore1
2
3
4
5
6# ignore files
*.txt
# ignore folder in root
/folder/
# ignore folder names
any_folder/difference from last commit:
git diffCommit: taking a snapshot of your repository at this point in time
git commit -m 'your_message'or justgit commitand enter the message in vim.Edit last commit:
git commit --amend, it will not create a new commit, just edit the last one.Restore:
Restore from stage:
git restore fileNameRestore from last commit:
git restore --cached fileNameRestore from specific commit
git restore --source <commit> fileNameReset:
git reset hashCommitTag--soft: Reset Head to that commit with staging not changed.--mixed(default): Reset Head and staging. Useful to retrieve to specific commit while not changing the Working Files.--hard: Reset Head, staging and Working Files. Completely reset to that stage.History:
git logbrief view
git log --onelinesee what have been changed:
git log -p
Git Operations:
git mv: automatically updates git tracking , whilemvwill only delete the old file and create a new file, you still have to delete the old file in git and track the new file.git rm: remove the file from working files and git. Whilermonly removes the working file, you still have to remove the git file.
Branch
Create a new branch:
git branch newBranchNameCreate a new branch based on the commit of current Head.
Confirm how many branch we have:
git branchRename branch:
git branch -m newNameSwitch branch:
git switch newBranchNameSwitch and create a new branch:
git switch -c newBranchNameGo back to main branch:
git switch mainMerge branch to current branch:
git merge -m 'message' newBranchNameIf main have been changed, there will be a merge conflict. We have to manually edit the file and commit.
Delete branch:
git branch -d newBranchName
Git for GitHub
git config --global user.name username
git config --global user.email email
create a new repository on the command line
1
2
3
4
5echo "# git_test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main # force rename, overwrite if existsadd origin
1
git remote add origin [email protected]:Xingfu-Yi/git_test.git
push
Push current branch to the main branch in github
1
git push -u origin main
Push all branches to GitHub:
git push --all1
git push --all -u origin main
fetch:
1
2
3git fetch origin
git log origin/main
git merge origin/maingit pullequals togit fetch && git merge
GIT LFS
Install Git LFS in git repository: git lfs install,
Ff don't want to download LFS file by default when cloning the repository, run:
1 | git lfs install --skip-smudge |
Track LFS file:
1 | git lfs track "*.bin" |
[Optional: clean git file after git reset --hard]
1 | git reset --hard HEAD~1 |
Clone:
1 | git clone <url> |
Standard Version
Process
Install standard-version:
1 | npm install -g standard-version |
Initialize package.json:
1 | npm init -y |
Generate Version:
1 | npx standard-version --first-release |
Push:
1 | git push origin main --follow-tags |
Conventional Commits
1 | <type>[optional scope]: <description> |
- type
- fix: bug fix
- feat: new feature
- chore: system and tools built that does not affect the code logic
- docs: change the documentation
- style: code style
- refactor: code refactor that does not affect the code logic
- perf: improve the performance of code
- test: add test
- build: like webpack, npm etc.
- scope
- An optional field representing the sphere of influence, for example
feat(auth): add login featurerepresents the authentication module.
- An optional field representing the sphere of influence, for example
- description
- the first letter should be in lower case and do not add . in the end
- body
- detailed description, can be written in multiple lines
- footer
- BREAKING CHANGE: will increase the version from
n.x.xton+1.x.x - Issue/Task Reference: cite the reference.
Fixes #commit_id
- BREAKING CHANGE: will increase the version from
Benefits
Using the Conventional Commits specification brings several advantages:
- Automatic Changelog Generation: Since commit
messages are structured clearly, tools like
standard-versionorconventional-changelogcan automatically generate a changelog, eliminating the need for manual editing. - Automated Version Management: By parsing commit
messages for
featandfix, tools can automatically determine the version increment according to semantic versioning. For example:BREAKING CHANGEincreases the major version (e.g., from1.0.0to2.0.0).- A
featcommit increases the minor version (e.g., from1.0.0to1.1.0). - A
fixcommit increases the patch version (e.g., from1.0.0to1.0.1).
- Clear Commit History: With standardized commit messages, team members can quickly understand the purpose and content of each change.
- Easier Collaboration and Code Review: When team members follow a consistent format for commit messages, reviewing code and locating issues becomes more efficient.