作为开发者,Git 是我们日常工作中不可或缺的工具。本文整理了我在项目中经常使用的 Git 命令,希望能帮助大家更高效地管理代码版本。
一、基础配置
在使用 Git 之前,需要进行基本的用户信息配置:
git config --global user.name Your Name
git config --global user.email your.email@example.com
git config --list
二、日常操作命令
1. 仓库初始化与克隆
git init
git clone https://github.com/user/repo.git
git clone -b develop https://github.com/user/repo.git
2. 添加与提交文件
git add filename.txt
git add .
git commit -m feat: 添加新功能
git commit --amend
3. 分支管理
git branch -a
git branch feature/login
git checkout develop
git checkout -b hotfix/bug-fix
git branch -d branch_name
4. 远程仓库操作
git remote add origin https://github.com/user/repo.git
git push -u origin master
git pull origin master
git remote -v
三、高级技巧
1. 暂存工作区
git stash
git stash list
git stash pop
git stash apply stash@{0}
2. 版本回退
git reset --hard HEAD^
git reset --hard commit_id
git reflog
四、实用别名
为常用命令设置别名可以大大提高效率:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
总结
熟练掌握这些 Git 命令,能够让你的代码管理工作更加顺畅。建议将这些命令写在一个方便查阅的地方,用多了自然就会熟练。


发表回复