1. 程式人生 > >Git命令操作(一)

Git命令操作(一)

1.建立內容

下面建立一些檔案,它們會被放到版本控制之中

#Switch to homecd ~/# Create a directorymkdir ~/repo01# Switch into itcd repo01# Create a new directorymkdir datafiles# Create a few filestouch test01touch test02touch test03touch datafiles/data.txt# Put a little text into the first filels >test01

2. 建立倉庫、新增檔案和提交更改

每個Git倉庫都是放置在.git資料夾下.這個目錄包含了倉庫的所有歷史記錄,.git/config檔案包含了倉庫的本地配置。

以下將會建立一個Git倉庫,新增檔案倒倉庫的索引中,提交更改。

# Initialize the local Git repositorygit init# Add all (files and directories) to the Git repositorygit add .# Make a commit of your file to the local repositorygit commit -m "Initial commit"# Show the log filegit log

3. diff命令與commit更改

通過git diff命令,使用者可以檢視更改。通過改變一個檔案的內容,看看git diff命令輸出什麼,然後提交這個更改到倉庫中

# Make some changes to the file

echo "This is a change" >
test01

echo "and this is another change" > test02# Check the changes via the diff command git diff# Commit the changes, -a will commit changes for modified files# but will not add automatically new filesgit commit -a -m "These are new changes"

4. Status, Diff 和 Commit Log

下面會向你展示倉庫現有的狀態以及過往的提交歷史

# Make some changes in the fileecho "This is a new change" > test01echo "and this is another new change" > test02# See the current status of your repository # (which files are changed / new / deleted)git status# Show the differences between the uncommitted files # and the last commit in the current branchgit diff# Add the changes to the index and commitgit add . && git commit -m "More chaanges - typo in the commit message"# Show the history of commits in the current branchgit log# This starts a nice graphical view of the changesgitk --all

5. 更正提交的資訊 - git amend

通過git amend命令,我們可以修改最後提交的的資訊

上述的提交資訊中存在錯誤,下面會修改這個錯誤

git commit --amend -m "More changes - now correct"

6. 刪除檔案

如果你刪除了一個在版本控制之下的檔案,那麼使用git add .不會在索引中刪除這個檔案。需要通過帶-a選項的git commit命令和-A選項的git add命令來完成

# Create a file and put it under version controltouch nonsense.txtgit add . && git commit -m "a new file has been created"# Remove the filerm nonsense.txt# Try standard way of committing -> will not work git add . && git commit -m "a new file has been created"# Now commit with the -a flaggit commit -a -m "File nonsense.txt is now removed"# Alternatively you could add deleted files to the staging index viagit add -A . git commit -m "File nonsense.txt is now removed"