DEV Community

VidyaR
VidyaR

Posted on • Updated on

Git Goodness

Very first we need to download Git.
Set up a folder as a Git repository. and follow along ~

Basic Git Commands

  1. At a convenient location on your computer, create a folder named git-test.
  2. 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>
Enter fullscreen mode Exit fullscreen mode

3.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
Enter fullscreen mode Exit fullscreen mode

4.Checking your Git repository status.

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

git status
Enter fullscreen mode Exit fullscreen mode

5.Adding files to the staging area

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

git add .
Enter fullscreen mode Exit fullscreen mode

6.Commiting to the Git repository

To commit the current staging area to your Git repository, type:

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

7.Checking the log of Git commits

To check the log of the commits to your Git repository, type:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
  • Now add the modified index.html file to the staging area and then do a third commit.

8.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
Enter fullscreen mode Exit fullscreen mode

9.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
Enter fullscreen mode Exit fullscreen mode

Then type the following at the prompt:

git checkout -- index.html
Enter fullscreen mode Exit fullscreen mode

10.You can also use git reset to reset the staging area to the last commit without disturbing the working directory.

Conclusion

This is the end of some basic Git commands. Experiment with these and more git commands...KEEP EXPERIMENTING.

Top comments (0)