github使用小記錄

參考博客:https://blog.csdn.net/qq_43570369/article/details/93208776

下載git: https://git-scm.com/downloads

git config --global user.name “Huang Haihui” //設置用戶名
git config --global user.email “[email protected]” //設置郵箱

使用:
ssh-keygen -t rsa -C “[email protected]
打開.ssh文件夾下面的id_rsa.pub,將他複製到github主頁設置下ssh and Gpg keys,完成綁定。

在github上創建一個repos

存到本地中
git clone [email protected]:huanghhdw/IMU_process.git

關聯(git下來的話可以省略,因爲會自動關聯)
git remote add origin [email protected]:huanghhdw/IMU_process.git

添加文件
git add .

提交註解
git commit -m ‘Your comment’

提交到github中
git push -u origin master

git push <遠程主機名> <本地分支名> <遠程分支名> ,例如 git push origin master:refs/for/master ,即是將本地的master分支推送到遠程主機origin上的對應master分支, origin 是遠程主機名,第一個master是本地分支名,第二個master是遠程分支名。-u推送多個關聯的分支。
git branch --set-upstream electric origin/electric // 本地分支關聯遠程分支
git push origin -d A // 刪除遠程分支 (用本地分支名,前面不加origin)
git branch -d A // 刪除本地分支

使用過程中,每次編輯的時候記得同步一下
git pull origin master

分支:
查看分支
git branch 遠程分支
git branch -r 本地分支

創建本地新的分支
git branch branch_name
切換到本地分支
git checkout branch_name
推送到遠程服務器中
git push origin branch_name

回退版本:
查看有哪些版本:
git log --pretty=oneline
本地版本回退:
git reset --hard fae6966548e3ae76cfa7f38a461c438cf75ba965
推送到遠程:
git push -f -u origin master

gedit中ubuntu16.04顯示亂碼:
gsettings set org.gnome.gedit.preferences.encodings candidate-encodings “[‘GB18030’, ‘UTF-8’, ‘CURRENT’, ‘ISO-8859-15’, ‘UTF-16’]”

git本地使用
git init
git add .
git commit -m “Your Comment”
git reflog 查看各個版本
git reset – hard ID 回退到ID的版本
git reset --hard HEAD^ 回退到上一個版本
git status -s 查看工作區和版本庫的不同

其中status狀態:
新添加的未跟蹤文件前面有 ?? 標記;
新添加到暫存區中的文件前面有 A 標記;
修改過的文件前面有 M 標記。 M 有兩個可以出現的位置:
出現在右邊的 M 表示該文件被修改了但是還沒放入暫存區,
出現在靠左邊的 M 表示該文件被修改了並放入了暫存區。
加到暫存區之後,又修改了一次,修改之後,並沒有添加到暫存區,前面有MM標記。

git reflog和 git log 區別:git reflog可以查看所有分支的所有操作記錄(包括已經被刪除的 commit 記錄和 reset 的操作)

有些文件無需納入 Git 的管理,也不希望它們總出現在未跟蹤文件列表。 通常都是些自動生成的文件,比如日誌文件,或者編譯過程中創建的臨時文件等。 在這種情況下,我們可以創建一個名爲 .gitignore 的文件,列出要忽略的文件模式

星號(*)匹配零個或多個任意字符;

[abc] 匹配任何一個列在方括號中的字符
(這個例子要麼匹配一個 a,要麼匹配一個 b,要麼匹配一個c);

問號(?)只匹配一個任意字符;
如果在方括號中使用短劃線分隔兩個字符,表示所有在這兩個字符範圍內的都可以匹配
(比如 [0-9]表示匹配所有 0 到 9 的數字)。

使用兩個星號(*) 表示匹配任意中間目錄
比如a/**/z 可以匹配 a/z, a/b/z 或a/b/c/z等

工作區有一個隱藏目錄.git,這個不算工作區,而是Git的版本庫。
Git的版本庫裏存了很多東西,其中最重要的就是稱爲stage(或者叫index)的暫存區,還有Git爲我們自動創建的第一個分支master,以及指向master的一個指針叫HEAD。

在這裏插入圖片描述

撤銷修改
(1)、沒有放到暫存區:

git checkout -- file1

(2)、放到暫存區(已經add進去):

git reset HEAD file1
git checkout -- file1

沒放到暫存區的demo:

[root@git1 demo]# cat file1 
redhat.org
hello
world
ma
[root@git1 demo]# echo linux >> file1 
[root@git1 demo]# cat file1 
redhat.org
hello
world
ma
linux
[root@git1 demo]# git status -s
 M file1
[root@git1 demo]# git checkout -- file1
[root@git1 demo]# cat file1 
redhat.org
hello
world

放到暫存區的demo示例:

[root@git1 demo]# echo centos >> file1 
[root@git1 demo]# git status -s
 M file1
[root@git1 demo]# git add file1 
[root@git1 demo]# git status -s
M  file1
[root@git1 demo]# cat file1 
redhat.org
hello
world
centos
[root@git1 demo]# git status -s
M  file1
[root@git1 demo]# git reset HEAD file1
Unstaged changes after reset:
M	file1
[root@git1 demo]# git status -s
 M file1
[root@git1 demo]# git checkout -- file1 
[root@git1 demo]# git status -s
[root@git1 demo]# cat file1 
redhat.org
hello
world

刪除文件復原,同樣用checkout

[root@git1 demo]# git status -s
 D file1
[root@git1 demo]# git checkout file1
[root@git1 demo]# git status -s
[root@git1 demo]# cat file1
redhat.org
hello
world
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章