Git 是一个分布式版本控制系统,常用于软件开发中的代码管理。下面我将从基础到高级,全面介绍 Git 的使用方法。
首先需要安装 Git,然后进行全局配置,设置用户名和邮箱:
sudo apt-get install git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list
git init
git clone https://github.com/username/repo.git
git add filename.txt
git add .
git commit -m "提交说明"
git status
git log
git log --oneline
git branch
git branch new-branch
git checkout new-branch
git checkout -b new-branch
git checkout main
git merge new-branch
git branch -d new-branch
git branch -D new-branch
git remote add origin https://github.com/username/repo.git
git remote -v
git push -u origin main
git push
git pull origin main
git fetch origin
git checkout -- filename.txt
git reset HEAD filename.txt
git reset --hard commit-hash
git revert commit-hash
git tag v1.0
git tag -a v1.0 -m "版本1.0发布"
git tag
git push origin v1.0
git push origin --tags
git stash
git stash list
git stash apply
git stash pop
git stash drop stash@{0}
git checkout main
git pull
git checkout feature
git rebase main
git submodule add https://github.com/username/submodule.git
git clone --recursive https://github.com/username/repo.git
git submodule update --init --recursive
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git co main
创建 .gitignore
文件,列出要忽略的文件和目录:
# 忽略所有 .log 文件
*.log
# 忽略 node_modules 目录
node_modules/
# 忽略特定文件
secrets.txt
当合并分支或拉取更新时可能会发生冲突,需要手动解决:
git pull
git status
git add conflicted-file.txt
git commit
以上就是 Git 的主要使用方法,掌握这些命令可以满足日常开发中的大部分需求。