1.Git 創建倉庫
2.使用git命令行將本地倉庫代碼上傳到github或gitlab遠程倉庫
一、初始化本地代碼倉庫
git init
二、修改代碼,並提交到遠程倉庫
# 01 修改代碼 eg. hello.py
# 02 添加
git add hello.py
# 03 註釋
git commit -m "update hello.py"
# 04 推送至遠程倉: git push <遠程主機名> <本地分支名>:<遠程分支名>
git push origin master
三、添加一個新建的文件夾
# 添加文件夾 eg. word
git add word/
但是必須注意:該新建文件夾裏面必須有內容纔可以添加成功
四、下載分支到本地
# 01 下載主分支
git clone http://git.××××××.cn/××××××/××××××.git
# 02 查看分支
git branch -r
origin/HEAD -> origin/master
origin/master
origin/test
# 更新爲test分支
git checkout origin/test
五、只提交指定修改的文件夾或文件
# 修改了文件夾test/裏的內容,可以一次性提交test/文件夾裏所有文件的修改
git add test/
git commit -m "update"
git push origin master
# 提交指定文件的修改,例如test/post.py
git add test/post.py
git commit -m "update"
git push origin master
六、查看當前關聯的分支
git branch
或者
git branch -a
七、git add 後如何撤銷
git reset HEAD
八、同步本地代碼時出現衝突:Please, commit your changes or stash them before you can merge.
出現這個問題的原因是其他人修改了xxx.php並提交到版本庫中去了,而你本地也修改了xxx.php,這時候你進行git pull操作就好出現衝突了,解決方法,在上面的提示中也說的很明確了
git stash
git pull
git stash pop
git stash: 備份當前的工作區的內容,從最近的一次提交中讀取相關內容,讓工作區保證和上次提交的內容一致。同時,將當前的工作區內容保存到Git棧中。
git stash pop: 從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容。由於可能存在多個Stash的內容,所以用棧來管理,pop會從最近的一個stash中讀取內容並恢復。
git stash list: 顯示Git棧內的所有備份,可以利用這個列表來決定從那個地方恢復。
git stash clear: 清空Git棧。此時使用gitg等圖形化工具會發現,原來stash的哪些節點都消失了。
九、git clone 指定分支
git clone -b 分支名稱 項目地址
十、解決衝突 error: The following untracked working tree files would be overwritten by merge
git fetch origin
git clean -f
git reset --hard origin/分支名稱
十一、回滾代碼(回退至某個版本)
首先查找要回滾的版本
git log
然後回滾:
git reset --hard {commit_id}
示例:
git reset --hard 7d723423d823i349b49e4805o94v390b
十二、回滾代碼後提交
回滾代碼後,修改本地代碼,再提交會讓你先pull,但是pull之後本地修改的代碼就被覆蓋了,方法是:
進行強制推送
git push --force origin 分支名稱