Git的使用方法概述

Git是一种版本控制系统。

如果团队进行共同开发,可以使用技术来共享代码并进行工作,可以使用Git命令或者UI工具来管理代码。此外,如果充分利用CI/CD,可以实现自动构建和发布。

如果是Windows PC的情况下

安装Windows版的Git。

个人的身份信息设置。

在Git上配置以识别谁正在进行操作。如果不进行此配置,将无法进行提交。

$ git config --global user.name "your name"
$ git config --global user.email "your email"

常常使用的Git指令

# Githubからクローンする(1回目のみ)
git clone https://<address>

# 2回目以後のクローンではなくoriginと同期する
git pull

# ローカルの変更ファイルすべて追加する
git add .

# 限定したファイルを追加(Pythonファイルのみ)
git add *.py

# 追加変更の内容を確認する
git status

# コミットする
git commit -m 'コミット内容'

# プッシュする
git push

# ローカルブランチとリモートブランチを指定してpushする
git push origin <local branch>:<remote branch>

# ローカルブランチとリモートブランチを指定してpushする(強制)
git push -f origin <local branch>:<remote branch>

#現在のブランチを確認する
git branch -a

#ブランチを切り替える
git switch <branch_name>

禁用SSL验证

請勿禁用git的SSL驗證設定,這是出於安全考慮。如果已經進行了以下設定,請刪除此設定:禁用這個倉庫的自簽SSL證書驗證設定。

$ git config --global http.sslVerify false

删除禁用SSL验证的设置。

$ git config --global --unset http.sslVerify

如果在特定环境下无法成功连接,请执行以下操作。

操作系统信息:
Linux raspberrypi 4.19.118-v7+
Debian 10.4

$ sudo apt-get install --reinstall ca-certificates
$ sudo mkdir /usr/local/share/ca-certificates/cacert.org
$ sudo wget -P /usr/local/share/ca-certificates/cacert.org http://www.cacert.org/certs/root.crt http://www.cacert.org/certs/class3.crt
$ sudo update-ca-certificates
$ git config --global http.sslCAinfo /etc/ssl/certs/ca-certificates.crt
$ git config http.sslVerify false

Git 命令

虽然有许多Git命令可供选择,但下面将介绍最先使用的git clone命令用于复制gitbucket存储库。

拉取(克隆)git仓库

由于使用了基于自我认证的https,所以在执行git clone命令时,请附加选项“GIT_SSL_NO_VERIFY=true”。
示例:目标仓库“respi_works/basic”。

GIT_SSL_NO_VERIFY=true git clone https://mjec.ddns.net:39862/git/raspi_works/basic.git

如何使用Git


#ローカルのブランチをリモートのブランチから強制上書きする
git reset --hard origin/<branch_name>

#強制Push
git push -f

#ブランチの切り替え
git switch <branch_name>

#変更を追加、コミット、Pushの一連操作
git add .
git commit -m "<commit text>"

#コミットの内容を上書き(変更Editerを表示しない)
git commit --amend --no-edit

#push (上記のコミットの場合は、強制上書きしないとPushできない)
git push -f origin local_branch:origin_branch

# gitコメントまとめる(2はコミットの階層を示す)

git rebase -i HEAD~2
editerでpick=>squashにして閉じる
再度#を入れて閉じる
git push -f origin local_branch:origin_branch

#現在のブランチを確認する
git branch -a   

#ブランチ削除(Local)
git branch -D <Local_branch>
 
#リモートブランチ削除する場合
git push --delete origin branch_name

#リモートブランチで削除されたブランチを反映する
git fetch -p 

#リモートブランチをマージする場合
git merge origin <ブランチ名>

#取得と作成を同時に行うとき
git checkout -b create_branch origin/copy_from_branch 

#コミットをなかったにすることができる
git rest --soft HEAD

#ローカルブランチを最新にする方法
git branch -a
git fetch origin -p
git reset --hard origin/<branch_name>
git log

#上書きコミットする方法
git add .
git commit --amend --no-edit
git push -f origin <local branch>:<remote branch>

#状態を確認
git log

#行動履歴を見る
git reflog

#Originの情報を見る
git remote -v
广告
将在 10 秒后关闭
bannerAds