Git常用命令汇总

- git init: 在当前目录初始化一个新的 Git 仓库。
- git clone [url]: 克隆远程仓库到本地。
- git add [file]: 将文件添加到暂存区。
- git commit -m "[message]": 将暂存区的文件提交到本地仓库,并添加提交信息。
- git status: 查看当前仓库的状态,包括已修改、已暂存等文件。
- git diff: 查看文件在工作目录和暂存区的区别。
- git log: 查看提交历史记录。
- git branch: 查看本地分支列表。
- git checkout [branch_name]: 切换到指定分支。
- git merge [branch_name]: 将指定分支合并到当前分支。
- git push: 将本地提交推送到远程仓库。
- git pull: 从远程仓库拉取更新到本地。
- git remote -v: 查看远程仓库信息。
- git remote add [name] [url]: 添加一个新的远程仓库。
- git fetch [remote]: 从远程仓库拉取更新到本地,但不自动合并。
- git reset [file]: 从暂存区中移除文件,但保留在工作目录中。
- git reset --hard [commit]: 将当前分支的 HEAD 指向指定的提交,并重置暂存区和工作目录。
- git rm [file]: 从版本控制中删除文件。
- git tag [tag_name]: 标记当前提交。
- git stash: 将当前工作目录中的修改保存到栈中,以便稍后恢复。
更改本地配置
git config --list #查看当前配置
git config --global user.name "Your Name"
通过使用 --global 参数,这些配置将被应用到你系统中所有的 Git 仓库。如果你想在特定的仓库中使用不同的用户名和邮箱,你可以在该仓库中使用上述命令,但不加 --global 参数。
合并远程的代码
# 如果远程仓库还没添加
git remote add origin https://gitee.com/gaosinihaha/my-yudao-cloud.git

# 获取远程仓库的数据
git fetch origin

# 查看当前所在分支
git branch

# 切换到要合并的分支(如果需要)
git checkout master

# 合并远程分支到本地分支
git merge origin/master --allow-unrelated-histories

# 解决冲突并提交
git add .
git commit -m "Resolved merge conflicts"