top of page
< Back

Git Lab Exercises

Git-Day-1

1. Create a directory named "myrepo":
  ```
  mkdir myrepo
  ```

2. Navigate to the "myrepo" directory:
  ```
  cd myrepo
  ```

3. Initialize Git in the directory:
  ```
  git init
  ```

To configure your Git global user email and name, use the following commands:

4. Set your global user email:
  ```
  git config --global user.email "username@gmail.com"
  ```

Replace "username@gmail.com" with the email address you want to associate with your Git commits.

5. Set your global user name:
  ```
  git config --global user.name "Your Name"
  ```

Replace "Your Name" with the name you want to associate with your Git commits.

These commands will set the specified email and name as your global Git configuration, allowing Git to identify you when making commits. Make sure to replace the email and name with your own information.


6. Create a file named "file1.txt" (you can use any text editor of your choice):
  ```
  touch file1.txt
  ```

7. Check the Git status:
  ```
  git status
  ```

This will show you the current status of your repository. At this point, Git should indicate that "file1.txt" is an untracked file.

8. Add "file1.txt" to Git:
  ```
  git add file1.txt
  ```

9. Check the Git status again:
  ```
  git status
  ```

Now Git should show that "file1.txt" has been added to the staging area.

10. Commit "file1.txt" to Git with the message "new file":
  ```
  git commit -m "new file"
  ```

11. Check the Git status one more time:
  ```
  git status
  ```

The status should now indicate that there are no changes to be committed.

That's it! You have successfully created a directory, initialized Git, added a file, committed it with a message, and checked the Git status along the way.



1. List the files in the current directory:
  ```
  ls
  ```

This will display the list of files and directories in the current directory.

2. List the files tracked by Git:
  ```
  git ls-files
  ```

This will show the list of files currently tracked by Git.

3. Create a file named "file2.txt" and write "hello world" to it:
  ```
  echo "hello world" > file2.txt

ls -l

git ls-files

```

This will create the file "file2.txt" and write the text "hello world" into it.

4. Check the Git status:
  ```
  git status
  ```

Git should show that "file2.txt" is an untracked file.

5. Add "file2.txt" to Git:
  ```
  git add file2.txt
  ```

6. Check the Git status again:
  ```
  git status
  ```

Now Git should show that "file2.txt" has been added to the staging area.

7. Commit "file2.txt" to Git with the message "second file":
  ```
  git commit -m "second file"
  ```

8. List the files in the current directory:
  ```
  ls
  ```

Now you should see "file1.txt" and "file2.txt" listed.

9. List the files tracked by Git:
  ```
  git ls-files
  ```

This will show the list of files currently tracked by Git, which should include "file1.txt" and "file2.txt".

That's it! You have created and added another file to Git, committed it with a message, and checked the file list using both the `ls` command and the `git ls-files` command.



1. View the Git commit history:
  ```
  git log
  ```

This will display a detailed log of all the commits in your repository.

2. View the Git commit history in a concise format:
  ```
  git log --oneline
  ```

This will show a summarized version of the commit history, with each commit represented by a single line.

3. View the command history:
  ```
  history
  ```

This will display the command history of your current session.

4. Append the text "new line 1st time" to "file1.txt":
  ```
  echo "new line 1st time" >> file1.txt
  ```

This command appends the text to the end of "file1.txt".

5. Check the Git status:
  ```
  git status
  ```

Git should show that "file1.txt" has been modified.

6. Commit all changes, including the modifications to "file1.txt", to Git:
  ```
  git commit -a -m "updated with 2nd line"
  ```

The `-a` flag tells Git to automatically stage any modified files.

7. Check the Git status again:
  ```
  git status
  ```

The status should now indicate that there are no changes to be committed.

8. List the files in the current directory:
  ```
  ls
  ```

This will display the list of files in the current directory, which should include "file1.txt" and "file2.txt".

9. View the content of "file1.txt":
  ```
  cat file1.txt
  ```

This will display the contents of "file1.txt", including the newly added line.

That's it! You have viewed the Git commit history, command history, appended a new line to "file1.txt", added and committed it to Git using the `-a` flag, checked the Git status, listed the files in the directory, and viewed the content of "file1.txt".


Here are the commands you can use to perform the requested tasks:

1. Create a file named "file3.txt":
  ```
  touch file3.txt
  ```

2. Add the first line to "file3.txt":
  ```
  echo "added first line to file3" > file3.txt
  ```

3. Check the Git status:
  ```
  git status
  ```

Git should show that "file3.txt" is an untracked file.

4. Add "file3.txt" to Git:
  ```
  git add file3.txt
  ```

5. Check the Git status again:
  ```
  git status
  ```

Now Git should show that "file3.txt" has been added to the staging area.

6. Commit "file3.txt" to Git with the message "add file3":
  ```
  git commit -m "add file3"
  ```

7. Check the Git status:
  ```
  git status
  ```

The status should now indicate that there are no changes to be committed.

8. Add the second line to "file3.txt":
  ```
  echo "added second line to file3" >> file3.txt
  ```

9. Check the Git status:
  ```
  git status
  ```

Git should show that "file3.txt" has been modified but not yet staged.

10. View the differences in "file3.txt" that are not yet staged:
   ```
   git diff file3.txt
   ```

11. View the differences in "file3.txt" that are staged (already added):
   ```
   git diff --staged file3.txt
   ```

12. Add the changes in "file3.txt" to Git:
   ```
   git add file3.txt
   ```

13. Check the Git status:
   ```
   git status
   ```

The status should now indicate that there are no changes to be committed.

14. View the differences in "file3.txt":
   ```
   git diff file3.txt
   ```

15. View the differences in "file3.txt" that are staged (already added):
   ```
   git diff --staged file3.txt
   ```

16. Add the third line to "file3.txt":
   ```
   echo "added third line to file3" >> file3.txt
   ```

17. View the differences in "file3.txt":
   ```
   git diff file3.txt
   ```

18. View the differences in "file3.txt" that are staged (already added):
   ```
   git diff --staged file3.txt
   ```

19. Check the Git status:
   ```
   git status
   ```

20. Commit the changes in "file3.txt" to Git with the message "updated file3 with new lines":
   ```
   git commit -m "updated file3 with new lines"
   ```

21. Check the Git status:
   ```
   git status
   ```

22. View the differences in "file3.txt":
   ```
   git diff file3.txt
   ```

23. View the differences in "file3.txt" that are staged (already added):
   ```
   git diff --staged file3.txt
   ```

That's it! You have created, modified, and committed changes to "file3.txt" in Git, and checked the Git status and differences at various stages.

bottom of page