首先
修改完成之后
git add.
git commit
这个时候是将修改提交到了本地,此时先不要push,先做一下远端代码与本地代码的合并(如果有冲突,push也会失败的)
git pull --rebase
这个表示拉取远端代码与本地代码合并,合并过程中你会发现可能有冲突,
对于有冲突的代码,进行修改,修改结束之后需要再次将修改过的文件执行add,表示修改完毕
git add modified_file
然后继续进行远端代码和本地代码的合并
git rebase --continue
此时合并成功,本地代码与远端代码一致,此时可以通过push进行提交,将本地的代码提交到远端
git push
如果提交的时候有一些本地的修改不想提交,比如一些测试代码,这时候提交的时候会报错,提示有modified文件没有提交,这时候可以先隐藏这些修改
git statsh
然后提交成功后再放开这些隐藏
git stash pop
- 删除跟踪某个文件或者文件夹
如果是文件
git rm --cached file
如果是文件夹
git rm -f --cached directory
如果你想同时删除本地文件那么去掉参数 --cached
git commit -m "comment"
git push origin master
- git pull and overwrite local files
git fetch -all
git reset --hard origin/branch_name
- 本地新建一个分支并且推送到远程
git checkout -b feature_branch_name
git push -u origin feature_branch_name
- 回退到上一个版本
- 查看旧版本
git log --oneline e2f9a78 Revert "Try something crazy" 872fa7e Try something crazy a1e8fb5 Make some important changes to hello.txt 435b61d Create hello.txt 9773e52 Initial import
你可以checkout到某个具体的commit
git checkout a1e8fb5
你当前的工作目录与a1e8fb5这个commit内容一致,你可以查看文件,编辑文件,编译项 目,运行测试等,不要担心,你所做的更改不会被保存。如果要继续开发,可以切换回相应分支,比如master
git checkout master
- 使用git checkout进行回退
首先我可以checkout到a1e8fb5这个commit
git checkout a1e8fb5
checkout到一个具体commit会使得repo处于 "detached HEAD" 状态。这意味着你不在任何分支上。当你切换到具体分支的时候,你做的任何commits都会成为孤儿,也就是Orphaned commit,这些孤儿会被Git's garbage collector删除,这与JVM的辣鸡收机器道理相通,不可达队形最终会被GC回收。
在detached HEAD状态下,执行
git checkout -b new_branch_without_crazy_commit
现在这个repo有一个新的时间线,872fa7e不在其中。我们可以继续这个分支上工作,但是如果你需要在一条不变的分支上工作,比如master,这方式就不太合适,我们可以用revet
- git revert回退
我们使用
git revert HEAD
来退回到上一次提交。这个命令其实是创建了一次新的commit,这个commit内容是回退到上一次提交,我们看一下时间线
git log --oneline e2f9a78 Revert "Try something crazy" 872fa7e Try something crazy a1e8fb5 Make some important changes to hello.txt 435b61d Create hello.txt 9773e52 Initial import
- Force "git pull" to overwrite local files
git fetch --all
and then
git reset --hard origin/master
OR If you are on some other branch:
git reset --hard origin/<branch_name>
Explanation:
git fetch
downloads the latest from remote without trying to merge or rebase anything.Then the
git reset
resets the master branch to what you just fetched. The--hard
option changes all the files in your working tree to match the files inorigin/master
- 将本地已有的仓库推动到远程仓库
git init git add -A git commit -m 'Added my project' git remote add origin git@github.com:scotch-io/my-new-project.git git push -u -f origin master