Git 快速参考指南
创建本地仓库
git init
创建一个git仓库的副本。
从远程存储库复制到本地
git clone <リポジトリ名>
将变更添加到阶段中
git add <ファイル名>
git add <ディレクトリ名>
git add .
记录更改(提交)
将舞台上的快照记录到远程仓库中。
git commit
# CLI上でコミットメッセージを書ける。
git commit -m "<メッセージ>"
# 変更内容の差分を表示
git commit -v
#空のコミット
git commit --allow-empty -m "<メッセージ>"
确认当前的变更情况
git status
确认更改的差异
# git addする前の変更分
git diff
git diff <ファイル名>
# git addした後の変更分
git diff --staged
查看更改历史
git log
# 一行で表示する
git log --oneline
# ファイルの変更差分を表示する
git log -p index.html
# 表示するコミット数を制限する
git log -n <コミット数>
# グラフにして表示する
git log --graph
记录删除文件的操作
# ファイルごと削除
git rm <ファイル>
git rm -r <ディレクトリ>
# ファイルを残したいとき
git rm --cached <ファイル>
记录文件的移动
git mv <旧ファイル> <新ファイル>
# 以下のコマンドと同じ
mv <旧ファイル> <新ファイル>
git rm <旧ファイル>
git add <新ファイル>
新增远程仓库(GitHub)。
git remote add origin https://github.com/<ユーザ名>/<リポジトリ名>.git
将代码发送到远程仓库(GitHub)。
从本地存储库向远程存储库传送
git push <リモート名> <ブランチ名>
git push origin main
给命令设置别名
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co commit
将不需要管理的文件从Git管理中移除。
.gitignoreファイル
# 指定したファイルを除外
index.html
# ルートディレクトリを指定
/root.html
# ディレクトリ以下を除外
dir/
# /以外の文字列にマッチ「*」
/*/*.css
撤销对文件的更改。
撤销本地环境工作区内的更改。
git checkout -- <ファイル名>
git checkout -- <ディレクトリ名>
#全変更を取り消す
git checkout -- .
撤销已经进行的舞台变更
只需清除舞台上的状态,工作树中没有任何更改。
git reset HEAD <ファイル名>
git reset HEAD <ディレクトリ名>
# 全変更を取り消す
git reset HEAD .
重新做最近的提交。
不要重新提交远程仓库中推送的提交。
git commit --amend
显示远程
git remote
# 対応するURLを表示
git remote -v
新添加一个远程仓库
git remote add <リモート名> <リモートURL>
git remote add hoge https://github.com/<ユーザ名>/<リポジトリ名>.git
从远程获取信息(提取)
获取 → 本地仓库 → 合并 → 工作树
git fetch <リモート名>
git fetch origin
从远程获取信息并进行合并(拉取)
git pull <リモート名> <ブランチ名>
git pull origin main
# 上記コマンドは、省略可能
git pull
#同じ
git fetch origin main
git merge orgin/main
显示远程的详细信息
git remote show <リモート名>
git remote show origin
更改或删除远程。
# 変更する
git remote rename <旧リモート名> <新リモート名>
git remote rename hoge new_hoge
# 削除する
git remote rm <旧リモート名> <新リモート名>
git remote rename new_hoge
添加分支
git branch <ブランチ名>
git branch feature
展示分支列表
git branch
# 全てのブランチを表示する
git branch -a
切换分支
git checkout <既存のブランチ名>
git checkout feature
#ブランチを新規作成して切り替える
git checkout -b <新ブランチ名>
合并更改记录
git merge <ブランチ名>
git merge <リモート名/ブランチ名>
git merge origin/main
更改或删除分支
# 作業しているブランチ名を変更
git branch -m <ブランチ名>
git branch -m new_branch
# ブランチを削除する
git branch -d <ブランチ名>
git branch -d feature
#強制削除する
git branch -D <ブランチ名>
以做出历史记录整理的形式将更改合并。
将分支的起始点移动到另一个提交点上。
git rebase <ブランチ名>
拉式的底座
只在想要获取GitHub内容时使用的方法,因为合并的提交不会被保存。
远程存储库→提取→本地存储库→变基→工作区
git pull --rebase <リモート名> <ブランチ名>
git pull --rebase origin main
将Pull请求设置为rebase类型
git config --global pull pull.rebase true
# mainブランチでgit pullするときだけ
git config branch.main.rebase true
重新进行多个提交
git rebase -i <コミットID>
git rebase -i HEAD~3
pick 3ee3651 add:file
pick 7bbf693 Create README.md
pick 366aa90 add:dir
# やり直したいcommitをeditにする。
edit 3ee9251 add:chapter3.3
pick 7bbe693 Create README.md
pick 366aa32 add:file
# やり直したら実行する
git commit --amend
# 次のコミットへ進む(リベース完了)
git rebase --continue
重组或删除提交。
git rebase -i HEAD~3
pick 3ee3651 add:file
pick 7bbf693 Create README.md
pick 366aa90 add:dir
# 削除したり、順番を入れ替えたりできる。
pick 7bbf693 Create README.md
pick 3ee3651 add:file
整合化提交
git rebase -i HEAD~3
pick 3ee3651 add:file
pick 7bbf693 Create README.md
pick 366aa90 add:dir
# コミットを1つにまとめる。
squash 3ee3651 add:file
squash 7bbf693 Create README.md
pick 366aa90 add:dir
将提交拆分
git rebase -i HEAD~3
pick 3ee3651 add:file
pick 366aa90 add:dir
pick 7bbf693 README.mdとindex.htmlを追加
# コミットを1つにまとめる。
pick 3ee3651 add:file
pick 366aa90 add:dir
edit 7bbf693 README.mdとindex.htmlを追加
git reset HEAD^
git add README.me
git commit -m 'READEME.meを追加'
git add index.html
git commit -m 'index.htmlを追加'
git rebase --continue
显示标签列表
git tag
创建标签
git tag -a [タグ名] -m "[メッセージ]"
git tag -a 20230704 -m "version 20230704"
# 軽量バージョン
git tag [タグ名]
git tag -a 20230704
# 後からタグ付けをする
git tag [タグ名] [コミット名]
8a6hd8f
显示标签数据
git show [タグ名]
git show 20230704
将标签发送到远程存储库
git push [リモート名] [タグ名]
git push origin 20230704
# タグを一斉送信
git push origin --tags
将任务暂时搁置
将变更部分暂时存放在stash。
git stash
git stash save
确认避难后的工作
git stash list
恢复被撤离的工作
# 最新の作業を復元する
git stash apply
# stageの状況も復元する
git stash apply --index
# 特定の作業を復元する
git stash apply [スタッシュ名]
git stash apply stash@{1}
删除被避难的工作
# 最新の作業を削除する
git stash drop
# 特定の作業を削除する
git stash drop [スタッシュ名]
git stash drop stash@{1}
#全作業を削除する
git stash clear
为计算机上的所有仓库设置Git用户名和电子邮件地址。
# ユーザ名を設定
git config --global user.name "<ユーザ名>"
# メールアドレスを設定
git config --global user.email "<メールアドレス>"
#確認方法
git config --global user.name
git config --global user.email
为了单独的Git仓库,设置Git用户名和电子邮件地址。
# ユーザ名を設定
git config user.name "<ユーザ名>"
# メールアドレスを設定
git config user.email "<メールアドレス>"
#確認方法
git config user.name
git config user.email
将现有的远程存储库更改为新的远程存储库。
git remote set-url origin <新しいリモートリポジトリURL>