git的本地備份,遠端備份
- 相關代碼
-
- 本地備份
-
- 遠端備份
相關代碼
git init 初始化倉庫
git status檢視狀态
git add 添加追加 git add --all
git commit -m “xxx” 送出
git log 檢視曆史版本 git log -p 檢視曆史送出詳細
git file.txt 檢視曆史版本 -2 最近兩個曆史版本
git reset --hard/soft +版本号 復原
(–hard復原曆史版本,檔案内容也會復原
–soft復原曆史版本,檔案内容不會復原)
git reflog 檢視每次操作的版本号
git remote -v 檢視遠端倉庫别名設定
git remote add origin +遠端倉庫位址 (添加别名映射)
git remote remove origin 删除映射别名
git push origin master 推
git pull origin master 拉
git pull origin master --allow-UNrelated-histories 強制拉取,合并
本地備份
1.初始化一個倉庫在D盤下面的檔案夾
git init
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git init
Reinitialized existing Git repository in D:/git/.git/
2.此時會多出一個.git倉庫
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ ls -al
total 45
drwxr-xr-x 1 admin 197609 0 Jan 3 17:30 ./
drwxr-xr-x 1 admin 197609 0 Jan 3 15:05 ../
drwxr-xr-x 1 admin 197609 0 Jan 4 00:05 .git/
-rw-r--r-- 1 admin 197609 2179 Jan 3 17:30 .gitignore
drwxr-xr-x 1 admin 197609 0 Jan 3 15:30 bbbb/`在這裡插入代碼片`
-rw-r--r-- 1 admin 197609 12524 Dec 19 23:48 ddddd.txt
-rw-r--r-- 1 admin 197609 14 Jan 3 16:25 file.txt
3.試着将這個.git删除
rm -rf .git
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ rm -rf .git
4.檢視送出狀态
git status
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git status
On branch master
No commits yet(#還未送出)
Untracked files:(#沒有被追蹤的檔案)
(use "git add <file>..." to include in what will be committed)
.gitignore
ddddd.txt
file.txt
nothing added to commit but untracked files present (use "git add" to track)
5.送出(追蹤)一個檔案(file.txt)到本地備份,并且再次檢視狀态
git file.txt
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git add file.txt
warning: LF will be replaced by CRLF in file.txt.
The file will have its original line endings in your working directory
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
ddddd.txt
(#可以看到file.txt已經被送出到曆史版本)
6.将所有追蹤的檔案進行送出
git commit -m (“進行說明”)
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git commit -m(進行一個說明) "add file.txt"
[master (root-commit) a638f5f] add file.txt
1 file changed, 4 insertions(+)
create mode 100644 file.txt
7.檢視曆史送出記錄
git log
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git log
commit a638f5fe5757128f0e96e26fd77cdf3ab3a1fb48 (HEAD -> master)
Author: tgzzwjz <[email protected]>
Date: Tue Jan 4 00:24:26 2022 +0800
add file.txt#所送出的檔案
8.檢視曆史送出詳細
git log -p
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git log -p
commit a638f5fe5757128f0e96e26fd77cdf3ab3a1fb48 (HEAD -> master)
Author: tgzzwjz <[email protected]>
Date: Tue Jan 4 00:24:26 2022 +0800
add file.txt
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..2d3385e
--- /dev/null
+++ b/file.txt
@@ -0,0 +1,4 @@
+dds#送出的内容
+ddsf
+dsf
+
9.将所有的檔案進行送出
git -al
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git add --all
10.復原(回到)曆史版本
git reset --hard commit a638f5fe5757128f0e96e26fd77cdf3ab3a1fb48
#commit後面為曆史版本号,知道版本号可以回到任意版本
–hard復原曆史版本,檔案内容也會復原
–soft復原曆史版本,檔案内容不會復原
預設為soft
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git reset --hard a638f5fe5757128f0e96e26fd77cdf3ab3a1fb48
HEAD is now at a638f5f add file.txt
11.檢視曆史版本号
從上往下是最新的版本号
git reflog
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git reflog
a638f5f (HEAD -> master) HEAD@{0}: reset: moving to a638f5fe5757128f0e96e26fd77cdf3ab3a1fb48
ac5a950 HEAD@{1}: commit: add c.txt
a638f5f (HEAD -> master) HEAD@{2}: commit (initial): add file.txt
遠端備份
1.在gitte中建立一個倉庫
在語言和添加 .gitignore選項中都選擇為python
2.點選,gitignore
在最後一行加上.idea/表示在該檔案夾下的修改暫時無效
3.檢視遠端倉庫别名設定
git remote -v
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git remote -v
4.添加别名映射(倉庫位址)
origin為别名(可以自己改)
git remote add origin https://gitee.com/tgzzwjz/bbb
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git remote add origin https://gitee.com/tgzzwjz/bbb
5.進行push與pull操作
并且輸入gitte使用者名及密碼
git push origin master
git pull origin master
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git push origin master
To https://gitee.com/tgzzwjz/bbb
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/tgzzwjz/bbb'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git pull origin master
From https://gitee.com/tgzzwjz/bbb
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
6.強制進行pull與push操作
并且傳輸成功的表現
git pull origin master --allow-unrelated-histories
[email protected]-N4C2QNI5 MINGW64 /d/git (master)
$ git pull origin master --allow-unrelated-histories
From https://gitee.com/tgzzwjz/bbb
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 139 insertions(+)
create mode 100644 .gitignore