# **透過 Git Bash 上傳 Github** :::info - 基本配置 - Window Version - macOS Version - 檔案操作 ::: ## 基本配置 ### Window Version 下載 [Git for Windows](https://gitforwindows.org/)  安裝好.exe 文件後,我選擇使用git bash  配置基本資料 ```= git config --global user.name "Your Name" git config --global user.email "your.email@example.com" ``` 初次使用 ```= git init ``` 正常來說,在c槽使用者裡面會自動出現git資料夾  這時候先到github上創建repository   >也可以在 git bash 中使用 mkdir repository_name 創建 下載repository ```= git clone https://github.com/yourusername/yourrepository.git ``` 下載過的repository,會出現在剛才的git資料夾中  <br/> ### macOS Version 進到Terminal 透過 Homebrew (套件管理工具)來安裝 Git ```= /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ```  使用命令安裝git ```= brew install git ``` 配置配置基本資料 ```= git config --global user.name "Your Name" git config --global user.email "your.email@example.com" ``` git config --global user.name "" git config --global user.email "" 初次使用 ```= git init ``` 正常來說,在c槽使用者裡面會自動出現git資料夾 導向到git資料夾 ```= cd your_path/git ``` 這時候先到github上創建repository   >也可以在 git bash 中使用 mkdir repository_name 創建 > 下載repository ```= # git clone 後面直接加入網址 git clone https://github.com/yourusername/yourrepository.git ``` 下載過的repository,會出現在剛才的git資料夾中 <br/> ## 檔案操作 先cd到git 資料夾 檔案加到暫存區 ```= git add <file_name.txt> git add . # 全部檔案 ``` 暫存區中的變更提交至本機版本庫 ```= git commit -m "Your commit message" ``` 將本機版本庫中的變更推送到遠端倉庫(remote repository),每次本地端的檔案都要重新提交,github上的才會更新 ```= git push ``` >如果我有在github上手動更動任何檔案,本地端也要記得 git pull 將最新版本拉到本地端 <br/> 只要還沒push到遠端倉庫,都還能撤回 假設add錯文件到暫存區 ```= git reset HEAD <unwanted-file.txt> # 會將特定檔案已新增至暫存區的檔案移回工作目錄 git reset # 會將所有已新增至暫存區的檔案移回工作目錄 ``` 假設已經commit了,但需要更改內容 ``` git reset --soft HEAD~1 ``` 假設add、commit 都要撤回 ```= git reset --mixed HEAD~1 ``` 假設add、commit 都要撤回,並恢復到上一次提交的狀態,慎用!!! ```= git reset --hard HEAD~1 ``` >情境: >提交1: 文件 script.py 內容為 "print('Hello, World!')" >提交2: 對 script.py 檔案進行修改,將內容更改為 "print('Hello, Git!')" > >目前 script.py 檔案的內容是 "print('Hello, Git!')"(最近提交commit後的狀態) > >執行 git reset --mixed HEAD~1 後,檔案 內容仍然是 "print('Hello, Git!')" > >執行 git reset --hard HEAD~1 後,檔案 script.py 會恢復到提交1 的狀態 "print('Hello, World!')" <br/> 查看目前狀態 ```= git status ``` 查看log ```= git log ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up