初識Git

  • git init 初始化git文件夾,即生成.git文件夾,該文件夾爲隱藏文件,不要手動修改
  • git add file1 file2 添加多個文件到暫存區(Stage),添加成功時,沒有提示信息返回
  • git commit -m "comments" 將暫存區(Stage)的全部內容一次提交到當前分支,並添加註釋(即:提交改動之前需要先執行 git add file 將修改的內容添加到暫存區Stage,沒有添加到暫存區Stage的改動不會提交到分支)
  • git status 查看提交前哪些文件改動
  • git diff 查看文件改動的具體內容
  • git log 查看提交的歷史記錄,參數--pretty=oneline在一行顯示日誌記錄,參數--graph顯示分支圖形,參數--abbrev-commit縮寫顯示commit號
  • git reset --hard HEAD^ 回退到上個版本,上上個版本使用HEAD^^,上一百個版本HEAD~100,特定的版本使用commit id前幾位(位數越多越精確,以免找到多個版本)
  • git reflog 記錄每次命令,當忘記了需要回退的版本號,可以使用該命令查看
  • git checkout -- file 將工作區的修改全部撤銷。如果file文件的修改已經添加到暫存區State,將從暫存區的file回覆到工作區;如果file文件沒有添加到暫存區,將從分支上的file回覆到工作區。這兩種情況都會將最近一次add或者commit之後做的修改擦除。
  • git reset HEAD file 把添加到暫存區的修改撤銷掉(unstage),重新放回工作區。
  • git rm -- filegit commit -m "delete file" 從版本庫刪除文件file的兩步
  • ssh-keygen -t rsa -C "[email protected]" 在用戶主目錄創建公鑰私鑰,在GitHub賬戶上添加公鑰
  • git remote add origin [email protected]:username/learnGit.git 在本地倉庫執行關聯遠程倉庫命令
  • git push -u origin master 第一次將本地master分支推送到遠程倉庫時使用參數-u可以使本地master分支與遠程倉庫master分支建立關聯,在之後的推送和拉取簡化命令
  • git push origin master 推送本地master分支到遠程倉庫
  • git pull origin master 從遠程倉庫拉取master分支
  • git clone [email protected]:username/gitskills.git 默認使用ssh協議從遠程倉庫clone項目,速度較於https快
  • git checkout -b dev 創建分支dev並且切換到dev分支,git checkout命令參數-b標識創建並切換分支
    • git branch dev 創建分支dev
    • git checkout dev 切換到dev分支
  • git branch查看所有的分支,當前正在使用的分支前多個星號*
  • git merge dev合併指定的分支到當前分支
  • git branch -d dev刪除dev分支
  • git log --graph --pretty=oneline --abbrev-commit 查看分支合併圖,美化縮寫顯示
  • git merge --no-ff -m "merge with no-ff" dev禁用Fast forward,在分支刪除之後仍保留分支合併的信息
  • 分支策略
    • 每個人使用自己的分支
    • 開發版本的合併使用dev分支
    • master分支應該是非常穩定,僅用來發布新版本時使用
  • git stash把當前工作現場“儲藏”起來,等以後恢復現場後繼續工作;在執行此操作前,需要將工作區的改動添加到暫存區
  • git stash list查看所有保存的工作現場
  • git stash pop恢復現場並刪除stash內容
  • 工作區和暫存區是一個公開的工作臺,任何分支都會用到,並能看到工作臺上最新的內容,只要在工作區、暫存區的改動未能夠提交到某一個版本庫(分支)中,那麼在任何一個分支下都可以看得到這個工作區、暫存區的最新實時改動。
    使用git stash就可以將暫存區的修改藏匿起來,使整個工作臺看起來都是乾淨的。所以要清理整個工作臺,那麼前提是必須先將工作區的內容都add到暫存區中去。之後在乾淨的工作臺上可以做另外一件緊急事件與藏匿起來的內容是完全獨立的
  • git branch -D feature_fly 強行刪除未合併的分支
  • 多人協作模式
    • 首先,可以試圖用git push origin branch-name推送自己的修改
    • 如果推送失敗,則因爲遠程分支比你的本地更新,需要先用git pull試圖合併;
    • 如果合併有衝突,則解決衝突,並在本地提交;
    • 沒有衝突或者解決掉衝突後,再用git push origin branch-name推送就能成功!
    • 如果git pull提示no tracking information,則說明本地分支和遠程分支的鏈接關係沒有創建,用命令git branch --set-upstream-to=origin/branch-name branch-name
  • git tag v1.2 Git的標籤是版本庫的快照,和分支一樣是一個指向某個commit的指針(分支可以移動,標籤不能移動),標籤比commit號更具有可讀性(v1.2),默認打在最新提交的commit上,也可以根據commit號打在特定提交上git tag v1.0 1456134
  • git tag 查看所有的標籤
  • git show tag-name 查看單個標籤的詳細信息
  • git tag -a v0.1 -m "first version" 1312323 -a指定標籤名 -m添加標籤描述
  • git tag -d tag-name 本地刪除一個標籤
  • git push origin tag-name 將一個本地標籤推送到遠程
  • git push origin --tags 將所有未推送到遠程的標籤推送
  • git tag -d tag-name,git push origin :refs/tags/tag-name 刪除遠程上的一個標籤,需要先刪除本地
  • 忽略某些文件時,需要編寫.gitignore文件,且放於版本庫中

  • 內容學自廖雪峯的官網網站Git官網Try Git

Git Advices

  • It is healthy to run git status often.Sometimes things change and you do not notice it.
  • staged Files are ready to be commmitted.
  • unstaged Files with changes that have not been prepared to be committed.
  • untracked Files are not tracked by Git yet.This usually indicates a newly created file.
  • deleted File has been deleted and is waiting to be removed from Git.
  • add all : You can also type git add -A. where the dot stands for the current directory,so everything in and beneath it is added.The -A ensures even file deletions are included.
  • git reset : You can use git reset <filename> to remove a file or files from the staging area.
  • Staging Area : A place where we can group files together before we “commit” them to Git.
  • Commit : A “commit” is a snapshot of our repository.This way if we ever need to look back at the changes we have made(or if someone else does),we will see a nice timeline of all changes.
  • Wildcards git add *.txt : We need quotes so that Git will receive the wildcard before our shell can interfere with it.Without quotes our shell will only execute the wildcard search within the current directory.Git will receive the list of files the shell found instead of the wildcard and it will not be able to add the files inside of the octofamily directory.
  • Check all the things : When using wildcards you want to be extra careful when doing commits.Make sure to check what files and folders are staged by using git status before you do the actual commit.This way you can be sure you are committing only the things you want.
  • Git remote : Git does not care what you name your remotes,but it is typical to name your main one origin.It is also a good idea for your main repository to be on a remote server like GitHub in case your machine is lost at sea during a transatlantic boat cruise or crushed by three monkey statues during an earthquake.
  • git stash : Sometimes when you go to pull you may have changes you do not want to commit just yet.One option you have,other than commiting,is to stash the changes.Use the command git stash to stash your changes,and git stash apply to re-apply your changes after your pull.Use git stash drop to delete the stash.git stash pop is used to apply and delete the stash.
  • HEAD : The HEAD is a pointer that holds your position within all your different commits.By default HEAD points to your most recent commit,so it can be used as a quick way to reference that commit without having to look up the SHA.
  • Using git diff gives you a good overview of changes you have made and lets you add files or directories one at a time and commit them separately.
  • The -- : So you may be wondering,why do I have to use this -- thing?git checkout seems to work fine without it.It is simply promising the command line that there are no more options after the --.This way if you happen to have a branch named the same as the filename,it will still revert the file,instead of switching to the branch of the same name.
  • Remove all the things : Removing one file is great and all,but what if you want to remove an entire folder?You can use the recursive option on git rm git rm -r folder_of_cats.This will recursively remove all folders and files from the given directory.
  • Force delete : What if you have been working on a feature branch and you decide you really do not want this feature anymore?You might decide to delete the branch since you are scrapping the idea.You will notice that git branch -d bad_feature does not work.This is because -d will not let you delete something that has not been merged.You can either add the --force(-f) option or use -D which combines -d -f together into one command.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章