root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory) modified: ReadMe no changes added to commit (use "git add" and/or "git commit -a")
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git add ReadMe root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: ReadMe
提示有文件已经进入暂存区,准备好被提交。我们提交后再次查看仓库状态
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git commit -m "modify ReadMe"[master a5e5b49] modify ReadMe 1 file changed, 1 insertion(+) root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master nothing to commit, working tree clean
root@iZbp16khlytibqc83gbmonZ:~/gitcode# rm file1 root@iZbp16khlytibqc83gbmonZ:~/gitcode# ls ReadMe root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory) deleted: file1 no changes added to commit (use "git add" and/or "git commit -a")
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git rm file1 rm'file1' root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: file1
仅仅再需要commit一次就可以了,git rm已经替我们完成了对工作区和暂存区的删除
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git commit -m "delete file1"[master 9123342] delete file1 1 file changed, 1 deletion(-) delete mode 100644 file1 root@iZbp16khlytibqc83gbmonZ:~/gitcode# git status On branch master nothing to commit, working tree clean
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git checkout dev Switched to branch 'dev' root@iZbp16khlytibqc83gbmonZ:~/gitcode# cat ReadMe hello git update dev dev dev
root@iZbp16khlytibqc83gbmonZ:~/gitcode# git branch -d dev3 error: The branch 'dev3' is not fully merged.If you are sure you want to delete it, run 'git branch -D dev3'
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# touch s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# touch i.ini root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# ls file.txt i.ini README.en.md README.md s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# vim .gitignore root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# ls -a file.txt .git .gitee .gitignore i.ini README.en.md README.md s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# cat .gitignore# 可以直接写文件名*.so *.ini
.so、.ini 中*表示忽略后缀名为 so、ini 的文件
通过git status命令来检验.gitignore ⽂件是否生效
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed).gitignore no changes added to commit (use "git add" and/or "git commit -a") root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git add . root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: .gitignore
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# vim .gitignore root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# cat .gitignore # 可以直接写文件名*.so *.ini !s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: .gitignore Untracked files: (use "git add <file>..." to include in what will be committed) s.so
使用git add -f 文件名命令
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# ls -a file.txt .git .gitee .gitignore i.ini README.en.md README.md s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# cat .gitignore # 可以直接写文件名*.so *.ini root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: .gitignore root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git add -f s.so root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: .gitignore new file: s.so
如果你觉得这样还不够形象,Git 还提供给我们给标签提供描述,使用git tag -a 标签名 -m "标签描述" [commit_id]命令,配合git show 标签名,更详细得查看标签信息
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.8 -m "important tag:XX" 4786ad782154384f9b005ce959cd7b82720d4456 root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.8 v0.9 v1.0 root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git show v0.8 tag v0.8 Tagger: hrKiley <[email protected]> Date: Mon Mar 16 17:26:14 2026 +0800 important tag:XX commit 4786ad782154384f9b005ce959cd7b82720d4456 (tag: v0.8) Author: hrKiley <[email protected]> Date: Sun Mar 15 21:10:55 2026 +0800 create file diff--git a/file.txt b/file.txt new file mode 100644 index 0000000..ad80c42 ---/dev/null +++ b/file.txt @@ -0,0 +1 @@ +create file
删除标签
删除标签很简单,使用git tag -d 标签名命令即可删除标签
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.8 v0.9 v1.0 root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag -d v1.0 Deleted tag 'v1.0'(was e69a904) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.8 v0.9
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag -d v0.8 Deleted tag 'v0.8'(was 25f8881) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.9
再推送执行删除远程标签命令git tag 远程主机名 :标签名
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag -d v0.8 Deleted tag 'v0.8'(was 25f8881) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git tag v0.9 root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git push origin :v0.8 Username for'https://gitee.com': hrKiley Password for'https://[email protected]': remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag f54d628d To https://gitee.com/hrKiley/remove-gitcode.git -[deleted] v0.8
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# vim file.txt root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# cat file.txt create file pull aaa root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git status On branch dev Your branch is up to date with 'origin/dev'. Changes not staged for commit: (use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory) modified: file.txt Untracked files: (use "git add <file>..." to include in what will be committed) i.ini no changes added to commit (use "git add" and/or "git commit -a") root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git add file.txt root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git commit -m "md file:aaa"[dev dcad356] md file:aaa 1 file changed, 2 insertions(+), 1 deletion(-) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git push Username for'https://gitee.com': hrKiley Password for'https://[email protected]': Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 2 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag 59434582 To https://gitee.com/hrKiley/remove-gitcode.git e4931b9..dcad356 dev -> dev
push 成功,我们再码云查看后,可以看到修改内容
开发者 2:
开发者 2 的操作和开发者 1 一样,只不过是新增加的是'bbb'。
在本地创建 dev 分支,再与远程 dev 分支进行连接,对 file.txt 文件进行修改,并提交 push 到远程仓库
PS D:\code\tests\remove-gitcode> git checkout -b dev origin/dev Switched to a new branch 'dev' Branch 'dev'set up to track remote branch 'dev'from'origin'.PS D:\code\tests\remove-gitcode> git branch -vv * dev e4931b9 [origin/dev] update file.txt. master e4931b9 [origin/master] update file.txt.
PS D:\code\tests\remove-gitcode> cat.\file.txt create file pull bbb PS D:\code\tests\remove-gitcode> git add .\file.txt PS D:\code\tests\remove-gitcode> git commit -m "md file:bbb"[dev 711c72e] md file:bbb 1 file changed, 2 insertions(+), 1 deletion(-)PS D:\code\tests\remove-gitcode> git push info: detecting host provider for'https://gitee.com/'... info: detecting host provider for'https://gitee.com/'... info: detecting host provider for'https://gitee.com/'... To https://gitee.com/hrKiley/remove-gitcode.git ![rejected] dev -> dev (fetch first) error: failed to push some refs to 'https://gitee.com/hrKiley/remove-gitcode.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g.,'git pull ...') before pushing again. hint: See the 'Note about fast-forwards'in'git push --help'for details.
PS D:\code\tests\remove-gitcode> cat.\file.txt create file pull bbb aaa PS D:\code\tests\remove-gitcode> git status On branch dev Your branch and 'origin/dev' have diverged, and have 1 and 1 different commits each, respectively.(use "git pull" to merge the remote branch into yours) You have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: file.txt no changes added to commit (use "git add" and/or "git commit -a")PS D:\code\tests\remove-gitcode> git add .\file.txt PS D:\code\tests\remove-gitcode> git commit -m "merge dev"[dev e95ebf8] merge dev PS D:\code\tests\remove-gitcode> git push info: detecting host provider for'https://gitee.com/'... info: detecting host provider for'https://gitee.com/'... info: detecting host provider for'https://gitee.com/'... warning: auto-detection of host provider took too long (>2000ms) warning: see https://aka.ms/gcmcore-autodetect for more information. Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 16 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 536 bytes | 536.00 KiB/s, done. Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag a14c5691 To https://gitee.com/hrKiley/remove-gitcode.git dcad356..e95ebf8 dev -> dev
这样子 push 成功了,查看码云效果也是如此
将内容合并到 master 分支
开发者们在 dev 分支做的差不多了最后一步就是将开发后的 dev 分支代码合并到 master 上,因为两个开发者的操作一样,这边以开发者 1 为例
分为两个方式:
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git branch dev * master root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git pull Already up to date.
为了防止 dev 合并到 master 中发生冲突(不允许 master 上修改),我们先将 master 合并到 dev 分支
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git checkout dev Switched to branch 'dev' Your branch is up to date with 'origin/dev'. root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git branch * dev master root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git merge master Already up to date.
并没有发生冲突,我们在将 dev 合并到 master 中就不会出现问题
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git branch dev * master root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git merge dev Updating e69a904..84a529b Fast-forward file.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# cat file.txt create file pull bbb aaa
最后一步,将合并后的 master 分支内容 push 到远程仓库 master 分支,并删除已经没有的 dev 分⽀
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git push Username for'https://gitee.com': hrKiley Password for'https://[email protected]': Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Delta compression using up to 2 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 614 bytes | 614.00 KiB/s, done. Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag d444c094 To https://gitee.com/hrKiley/remove-gitcode.git e4931b9..84a529b master -> master
root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git push #推送失败 fatal: The current branch feature-1 has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin feature-1 To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'. root@iZbp16khlytibqc83gbmonZ:~/remove-gitcode# git push origin feature-1 Username for'https://gitee.com': hrKiley Password for'https://[email protected]': Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 2 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag 4a48b7a1 remote: Create a pull request for'feature-1' on Gitee by visiting: remote: https://gitee.com/hrKiley/remove-gitcode/pull/new/hrKiley:feature-1...hrKiley:master To https://gitee.com/hrKiley/remove-gitcode.git *[new branch] feature-1 -> feature-1