加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

git操作常用命令

发布时间:2020-12-14 16:40:54 所属栏目:百科 来源:网络整理
导读:1. .gitignore not ignoring .idea path 无法忽略某些目录或文件 因为这些文件已经被添加到repo中了,最好在第一次提交前,把不需要的文件添加到.gitignore .gitignore only ignores newly added (untracked) files. If you have files that have already be

1. .gitignore not ignoring .idea path 无法忽略某些目录或文件

因为这些文件已经被添加到repo中了,最好在第一次提交前,把不需要的文件添加到.gitignore

.gitignore only ignores newly added (untracked) files.
If you have files that have already been added to the repository,all their changes will be tracked as usual,even if they are matched by .gitignore rules.

To remove that folder from the repository (without deleting it from disk),do:

git rm --cached -r .idea

然后再向远端推一次,远端上的相应文件也消除了

2. 常见用法

git config --global user.name "sunchenguang"
git config --global user.email "809200299@qq.com"

git remote remove origin
git remote set-url origin git://new.url.here

git config --global alias.ci "commit -v"

git config --global core.autocrlf false

3. 新repo推送

…or create a new repository on the command line

echo "# blog" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:sunchenguang/blog.git
git push -u origin master

…or push an existing repository from the command line

git remote add origin git@github.com:sunchenguang/blog.git
git push -u origin master

4. 查看远程分支

git pull // 保持代码最新
git branch -r 

5. 拉取远程分支并创建本地分支

git checkout -b <本地分支名> origin/<远程分支名>
使用该方式会在本地分支,并自动切换到该本地分支。

6. 合并分支

git merge <指定分支>
git merge命令用于合并指定分支到当前分支。

7. 删除分支

git branch -d <指定分支>

8. 切换分支

git checkout <指定分支>

9. stash暂时隐藏修改

git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。

git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。

git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

10. 合并两个git仓库

If you want to merge project-a into project-b:

cd path/to/project-b
git remote add project-a path/to/project-a
git fetch project-a
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
git remote remove project-a

This method worked pretty well for me,it's shorter and in my opinion a lot cleaner.

Note: The --allow-unrelated-histories parameter only exists since git >= 2.9.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读