Git入門操作(一)
- 2020 年 4 月 4 日
- 筆記
最近真正用到了Git,感覺還是需要好好整理一下最最基礎用法,與萌新共享。^_^
關於Git的基礎介紹,這裡不再贅述,下面擼程式碼了(主要是命令行的操作,屬於linux作業系統的,可能沒聽過,但記住就好了)。
1. 在Git Bash中運行,並切換到你要進行操作的文件夾下。
1 linux指令: 2 ls 查看目錄下的內容 3 ls -a 查看所有內容(包含隱藏文件) 4 mkdir 創建目錄 5 cd 切換目錄 6 cd.. 返回上級目錄
2. Git的操作主要在這樣幾個區域進行:操作區,暫存區,版本庫。
1 git常用指令: 2 git init 初始化倉庫 3 git status 查看轉態,被修改的文件顯示紅色,添加的文件顯示綠色 4 git add 將工作區的文件添加到暫存區 5 git add 1.txt 將工作區的文件1.txt添加到暫存區 6 git add . 將當前工作區的所有文件添加到暫存區 7 git commit -m '描述資訊' 將暫存區的文件提交到版本庫
【注】程式碼git commit提交步驟可能會報錯,會提示讓你配置用戶資訊:
配置用戶資訊 git config --global user.email '111@qq.com' git config --global user.name 'xiaohua' 配置完成後會在C盤/用戶名/目錄看到一個.gitconfig文件。這個文件中就是我們配置的用戶資訊,有了它才可以進行提交操作。
下面是我寫的一個簡單的Git實操。前提:在桌面建立一個名為git1的文件夾,該文件夾中有1.txt文件。
zx@zxDELL MINGW64 ~ $ cd /c/Users/婷婷/Desktop/git1//切換到git1所在目錄 zx@zxDELL MINGW64 ~/Desktop/git1 $ git init //初始化git1倉庫,同時文件夾會生成.git默認隱藏文件夾 Initialized empty Git repository in C:/Users/婷婷/Desktop/git1/.git/ zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ ls //查看git1文件夾下內容 1.txt zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git status //未提交之前,查看狀態 On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) 1.txt //未被添加,所以呈現紅色 nothing added to commit but untracked files present (use "git add" to track) zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git add . //將工作區的文件添加到暫存區 zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git status //查看狀態 On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: 1.txt //被添加成功,所以顯示為綠色 zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git commit -m'first-submit' //將暫存區的文件提交到版本庫 *** Please tell me who you are. Run //提交失敗,顯示填寫用戶資訊 git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got '婷婷@zxDELL.(none)') zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git config --global user.email "[email protected]" zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git config --global user.name "zx" //按照提示進行填寫 zx@zxDELL MINGW64 ~/Desktop/git1 (master) $ git commit -m'second-submit' //填寫完成後,再次提交,顯示成功 [master (root-commit) afb4dfd] second-submit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 1.txt
3.遠程庫:將自己的本地版本推送到Github上
https方式【容易操作,但更好的方式是SSH】:
1. 在Github上新建一個空的倉庫,勾選初始化。 2. 複製https的地址,進行clone git clone https://github.com/.... 3. 切換到建立的文件夾下 將需要推送的文件等,放入該文件夾中,並提交到版本庫。 4. git push origin master 將本地master版本庫的內容推送到遠程github倉庫中
【注】如果是多人開發,並且遠程庫有更新,首先應該先拉取github倉庫的內容(git pull),然後再進行開發,再推送到遠程,如果直接推送會出現問題。
【Tips】關於複雜的分支操作等,此處並未列舉,以防止混亂。希望上述所有可以對你有幫助,下篇我將寫一些關於Github的操作事項,歡迎查看━(*`∀´*)ノ亻