Basic Git Commands

Exercise (Instructions): Basic Git Commands

Objectives and Outcomes

In this exercise you will get familiar with some basic Git commands. At the end of this exercise you will be able to:

  • Set up a folder as a Git repository
  • Perform basic Git operations on your Git repository

Basic Git Commands

  • At a convenient location on your computer, create a folder named git-test.
  • Open this git-test folder in your favorite editor.
  • Add a file named index.html to this folder, and add the following HTML code to this file:
  • <!DOCTYPE html>
    <html>
        <head></head>
    
        <body>
            <h1>This is a Header</h1>
        </body>
    </html>

    Initializing the folder as a Git repository

    • Go to the git-test folder in your cmd window/terminal and type the following at the prompt to initialize the folder as a Git repository:
    • git init

      Checking your Git repository status

      • Type the following at the prompt to check your Git repository's status:
        git status

        Adding files to the staging area

        • To add files to the staging area of your Git repository, type:
          git add .

          Commiting to the Git repository

          • To commit the current staging area to your Git repository, type:
            git commit -m "first commit"

            hecking the log of Git commits

            • To check the log of the commits to your Git repository, type
              git log --oneline
              • Now, modify the index.html file as follows:
                <!DOCTYPE html>
                <html>
                    <head></head>
                
                    <body>
                        <h1>This is a Header</h1>
                        <p>This is a paragraph</p>
                    </body>
                </html>
                • Add a sub-folder named templates to your git-test folder, and then add a file named test.html to the templates folder. Then set the contents of this file to be the same as the index.html file above.
                • Then check the status and add all the files to the staging area.
                • Then do the second commit to your repository
                • Now, modify the index.html file as follows:
                  <!DOCTYPE html>
                  <html>
                      <head></head>
                  
                      <body>
                          <h1>This is a Header</h1>
                          <p>This is a paragraph</p>
                          <p>This is a second paragraph</p>
                      </body>
                  </html>
                  • Now add the modified index.html file to the staging area and then do a third commit.

                  Checking out a file from an earlier commit

                  • To check out the index.html from the second commit, find the number of the second commit using the git log, and then type the following at the prompt:
                    git checkout <second commit's number> index.html#<second commit's number>是git log中的number

                    Resetting the Git repository

                    • To discard the effect of the previous operation and restore index.html to its state at the end of the third commit, type:
                      git reset HEAD index.html
                      • Then type the following at the prompt:
                        git checkout -- index.html
                        • You can also use git reset to reset the staging area to the last commit without disturbing the working directory.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值