目录

Git帮助手册

Git命令整理

Git 新仓库到提交代码完整实例

# 创建新仓库
mkdir my_project
cd my_project
git init

# 添加远程仓库
git remote add origin https://github.com/username/repo.git

# 创建 .gitignore 文件来排除文件
# 例如,排除所有 .log 文件和 node_modules 目录
echo "*.log" >> .gitignore
echo "node_modules/" >> .gitignore

# 添加 .gitignore 文件
git add .gitignore

# 添加当前文件夹所有文件
git add .

# 或者,添加部分文件
# 例如,仅添加 README.md 和 src 目录
git add README.md
git add src/

# 提交文件 (-m 表示添加提交信息)
git commit -m "Initial commit"

# 推送到远程仓库 (-u 表示设置 upstream,即关联本地分支与远程分支,并推送到远程仓库)
git push -u origin main

Git 后期使用常用命令


# 查看现有的远程仓库
git remote -v

# 添加一个新的远程仓库
git remote add backup https://github.com/username/backup-repo.git

# 更改 origin 的名称
git remote rename origin new-origin

# 修改 new-origin 的 URL
git remote set-url new-origin https://github.com/username/new-repo.git

# 移除 backup 仓库
git remote remove backup

# 切换远程仓库
# 例如,将 origin 切换到新的远程仓库 URL
git remote set-url origin https://github.com/username/new-origin-repo.git

# 获取远程分支
git fetch

# 查看所有分支信息
git branch -a

# 创建并切换到新分支
git checkout -b new-branch

# 切换到已有分支
git checkout main

# 查看当前分支信息
git branch -v

# 创建新的分支
git branch feature-branch

# 更改分支名称
git branch -m new-branch renamed-branch