Git命令操作(一)
1.创建内容
下面创建一些文件,它们会被放到版本控制之中
#Switch to home cd ~/ # Create a directory mkdir ~/ repo01 # Switch into it cd repo01 # Create a new directory mkdir datafiles # Create a few files touch test01 touch test02 touch test03 touch datafiles/ data.txt # Put a little text into the first file ls >test012. 创建仓库、添加文件和提交更改
每个Git仓库都是放置在.git文件夹下.这个目录包含了仓库的所有历史记录,.git/config文件包含了仓库的本地配置。
以下将会创建一个Git仓库,添加文件倒仓库的索引中,提交更改。
# Initialize the local Git repository git init # Add all (files and directories) to the Git repository git add . # Make a commit of your file to the local repository git commit -m "Initial commit" # Show the log file git log3. diff命令与commit更改
通过git diff命令,用户可以查看更改。通过改变一个文件的内容,看看git diff命令输出什么,然后提交这个更改到仓库中
# Make some changes to the fileecho "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 files git commit -a -m "These are new changes"
4. Status, Diff 和 Commit Log
下面会向你展示仓库现有的状态以及过往的提交历史
# Make some changes in the file echo "This is a new change" > test01 echo "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 branch git diff # Add the changes to the index and commit git add . && git commit -m "More chaanges - typo in the commit message" # Show the history of commits in the current branch git log # This starts a nice graphical view of the changes gitk --all5. 更正提交的信息 - 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 control touch nonsense.txt git add . && git commit -m "a new file has been created" # Remove the file rm 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 flag git commit -a -m "File nonsense.txt is now removed" # Alternatively you could add deleted files to the staging index via git add - A . git commit -m "File nonsense.txt is now removed"