top of page
< Back

Git Lab Exercises

Git-Day-2

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

1. Remove "file3.txt" from Git and delete the file:
  ```
  git rm file3.txt
  ```

This command removes "file3.txt" from both the Git repository and the file system.

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

Git should show that "file3.txt" has been deleted.

3. Commit the deletion of "file3.txt" with the message "del the file3.txt":
  ```
  git commit -m "del the file3.txt"
  ```

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

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

5. Remove "file2.txt" from the Git repository but keep the file in the file system:
  ```
  git rm --cached file2.txt
  ```

This command removes "file2.txt" from the Git repository but keeps it in the file system.

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

Git should show that "file2.txt" has been deleted but is still present in the file system.

7. List the files in the current directory with detailed information:
  ```
  ls -l
  ```

This will display the list of files in the current directory along with detailed information, such as permissions and file sizes.

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

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

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

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

10. Commit the deletion of "file2.txt" from the Git repository only, with the message "deleted from only local repo":
   ```
   git commit -m "deleted from only local repo"
   ```

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

12. List the files in the current directory with detailed information:
   ```
   ls -l
   ```

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

That's it! You have removed "file3.txt" from Git and the file system, removed "file2.txt" from Git but kept it in the file system, and checked the Git status, file list, and tracked files at various stages.


Purpose: The main purpose of the .gitignore file is to prevent certain files or directories from being accidentally committed to the repository. It helps to avoid cluttering the repository with unnecessary or sensitive files.


  1. Syntax: The .gitignore file uses simple pattern matching rules to specify files and directories to be ignored. Each pattern should be on a separate line. Here are some examples of pattern syntax:
    file.txt: Ignores a specific file named file.txt.
    *.txt: Ignores all files with the .txt extension.
    directory/: Ignores an entire directory named directory.
    !important.txt: Negates a previous pattern and includes important.txt even if it matches an earlier ignore pattern.You can also use wildcards (* for any number of characters, ? for a single character) and directory globbing (**/ to match any subdirectory).

  2. Comments: Lines starting with # are treated as comments and are ignored by Git. You can use comments to provide explanations or notes within the .gitignore file.


Here are the commands to perform the requested tasks:

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

This will show the current status of your Git repository.

2. Create the `.gitignore` file:
  ```
  touch .gitignore
  ```

This command creates a new file named `.gitignore`.

3. Add "file2.txt" to the `.gitignore` file:
  ```
  echo "file2.txt" >> .gitignore
  ```

This appends the line "file2.txt" to the `.gitignore` file.

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

Git should show that the `.gitignore` file is an untracked file.

5. Add the `.gitignore` file to Git:
  ```
  git add .gitignore
  ```

This adds the `.gitignore` file to the staging area.

6. Commit the addition of the `.gitignore` file with the message "ignore file added":
  ```
  git commit -m "ignore file added"
  ```

This commits the `.gitignore` file to the repository.

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

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

That's it! You have created the `.gitignore` file, added "file2.txt" to it, committed the file to Git, and checked the Git status at various stages.


The `git log --oneline` command is used to display the commit history in a condensed format, showing each commit as a single line with a short commit hash and commit message. Here's an example command and its output:

Command:
```
git log --oneline
```

Output:
```
b83cbe8 Updated README.md
a47d0f3 Fixed bug in authentication process
e2c1379 Added new feature
1f6a8d7 Initial commit

```

In the output, each line represents a single commit. The commit hash (abbreviated) is displayed at the beginning of each line, followed by the commit message.

Here's a breakdown of the output:
- `b83cbe8`: Commit hash (abbreviated) of the most recent commit.
- `Updated README.md`: Commit message for the most recent commit.
- `a47d0f3`: Commit hash (abbreviated) of the second most recent commit.
- `Fixed bug in authentication process`: Commit message for the second most recent commit.
- `e2c1379`: Commit hash (abbreviated) of the third most recent commit.
- `Added new feature`: Commit message for the third most recent commit.
- `1f6a8d7`: Commit hash (abbreviated) of the initial commit.
- `Initial commit`: Commit message for the initial commit.

The `git log --oneline` command provides a concise overview of the commit history, making it easier to scan through the commit messages and identify specific commits based on their hash.


Here's an example of using the `git log --oneline` command, taking the latest commit ID, and performing a `git revert` operation:


1. Run `git log --oneline` to view the commit history:
```
git log --oneline
```
Example output:
```
1f6a8d7 Updated README.md
a47d0f3 Fixed bug in authentication process
e2c1379 Added new feature
1f6a8d7 Initial commit
```

- `git revert`: The `git revert` command is used to undo a specific commit by creating a new commit that undoes the changes introduced by the specified commit. It is a safe way to undo changes without modifying the commit history. Revert commits are used to record the fact that a previous commit was undone.

In this example, the latest commit ID is `1f6a8d7`.

2. Take note of the latest commit ID (`1f6a8d7` in this case) from the output.

3. Perform a `git revert` operation using the latest commit ID:
```
git revert 1f6a8d7
```
Replace `1f6a8d7` with the actual commit ID you want to revert.

4. Git will open a text editor to create a revert commit message. You can modify the message if needed and save the file.

5. The `git revert` operation will create a new commit that undoes the changes introduced by the specified commit. Git will automatically generate the commit message indicating that a revert was performed.

6. Once the revert commit is created, it will be added to the commit history, effectively undoing the changes made in the specified commit.


Please note that the `git revert` operation creates a new commit to revert the changes. It is a safe way to undo a specific commit without modifying the commit history.


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

This will show the current status of your Git repository.

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

This command displays the commit history with each commit represented as a single line, showing the abbreviated commit hash and commit message.


- `git reset`: The `git reset` command is used to move the current branch pointer to a different commit, effectively resetting the branch to a specific commit. It allows you to discard commits and move the branch pointer, potentially removing commits from the commit history. This operation modifies the commit history and should be used with caution.


3. Reset the repository and move the HEAD pointer back by 2 commits:
  ```
  git reset --hard HEAD~2
  ```

This command resets the repository to the state of the commit two steps back from the current HEAD position, discarding any changes made in the subsequent commits.

4. View the updated commit history after the reset:
  ```
  git log --oneline
  ```

The commit history will now reflect the changes made by moving the HEAD pointer back by 2 commits.


5. Reset the repository to a specific commit (in this example, commit hash `142ce41`):
  ```
  git reset --hard 142ce41
  ```

This command resets the repository to the specified commit, discarding any changes made in the subsequent commits.

6. View the updated commit history after the reset:
  ```
  git log --oneline
  ```

The commit history will now reflect the changes made by moving the repository back to the specified commit.

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

This will show the current status of your Git repository after the reset operations.

That's it! You have checked the Git status, viewed the commit history, performed `reset --hard` operations, and checked the Git status again at different stages.




1. `git branch`

```

* master

```

Explanation: The `git branch` command lists all the branches in your repository. Since you just initialized the repository, there is only the `master` branch.


2. `git branch b1 master`

No output (creates a new branch named `b1` based on the `master` branch).


3. `git branch`

```

  b1

* master

```

Explanation: The `git branch` command shows that there are two branches: `b1` and `master`. The `*` symbol indicates the currently checked out branch (in this case, `master`).


4. `git checkout b1`

No output (switches to the `b1` branch).


5. `git branch`

```

* b1

  master

```

Explanation: After checking out the `b1` branch, the `git branch` command shows `b1` as the currently checked out branch.


6. `git status`

```

On branch b1

nothing to commit, working tree clean

```

Explanation: The `git status` command shows that you are on the `b1` branch and there are no changes to be committed.


7. `git log --oneline`

No output (no commits have been made yet on the `b1` branch).


8. `touch file3.txt`


9. `echo " new file on b1" >> file3.txt`


10. `git status`

```

On branch b1

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        file3.txt


nothing added to commit but untracked files present (use "git add" to track)

```

Explanation: The `git status` command shows that a new file, `file3.txt`, has been created but is currently untracked.


11. `git add file3.txt`


12. `git commit -m "on b1"`

```

[b1 0000000] on b1

 1 file changed, 1 insertion(+)

 create mode 100644 file3.txt

```

Explanation: The `git commit` command creates a new commit on the `b1` branch with the message "on b1".


13. `git status`

```

On branch b1

nothing to commit, working tree clean

```

Explanation: The `git status` command confirms that there are no changes to be committed on the `b1` branch.


14. `git log --oneline`

```

0000000 (HEAD -> b1) on b1

```

Explanation: The `git log --oneline` command displays the commit history. In this case, there is a single commit with the message "on b1".


15. `ls`

```

file3.txt

```

Explanation: The `ls` command lists the files in the current directory, which includes `file3.txt`.


16. `git ls-files`

```

file3.txt

```

Explanation: The `git ls-files` command lists the tracked files in the repository, which includes `file3.txt`.


17. `git checkout master`

```

Switched to branch 'master'

```

Explanation: The `git checkout` command switches to the `master` branch.


18. `ls`

```

file3.txt

```

Explanation: The `ls` command lists the files in the current directory. Since the `file3.txt` file was created on the `b1` branch and has not been merged into `master` yet, it is still present.


19. `git  ls-files`

```

file3.txt

```

Explanation: The `git ls-files` command lists the tracked files in the repository, which includes `file3.txt`.


20. `git status`

```

On branch master

nothing to commit, working tree clean

```

Explanation: The `git status` command shows that there are no changes to be committed on the `master` branch.


21. `git log --oneline`

No output (no commits have been made on the `master` branch after the initialization).



GIT merge with out Conflict

1. `git branch`: This command lists all the branches in your Git repository. It shows both local and remote branches, with an asterisk (*) next to the currently checked out branch.


2. `ls`: This command lists the files and directories in the current directory. It is not a Git command but a command used in the command-line interface (CLI) to list files.


3. `git merge b1 master`: This command merges the changes from branch `b1` into the `master` branch. It integrates the commits from `b1` into `master` and creates a new merge commit, if necessary.


4. `ls`: Running `ls` again lists the files and directories in the current directory. This command is used to list files in the CLI and does not have any direct relation to Git.


5. `git log --oneline`: This command displays the commit history in a concise format. It shows the commit hashes and commit messages on a single line.


6. `git log --oneline b1`: Adding the branch name (`b1` in this case) to the `git log --oneline` command filters the commit history to only show the commits specific to the `b1` branch. It displays the abbreviated commit hashes and commit messages for those commits.


Please note that the `git branch`, `git merge`, `git log`, and `ls` commands are executed in the command-line interface (CLI) or terminal, while `ls` is a system command to list files and directories in a directory, not specific to Git.


GIT Merge with Conflicts

=================================================


1. `touch file4.txt`: This command creates a new file named `file4.txt` in the current directory.


2. `echo "a new file created on master" >> file4.txt`: This command appends the text "a new file created on master" to the `file4.txt` file.


3. `git status`: This command shows the status of your Git repository, including any untracked or modified files.


4. `git add .`: This command stages all changes in the repository, including the new `file4.txt` file.


5. `git commit -m "file on master"`: This command creates a new commit on the `master` branch with the message "file on master".


6. `git log --oneline`: This command displays the commit history in a concise format, showing the abbreviated commit hashes and commit messages.


7. `git checkout b1`: This command switches to the `b1` branch.


8. `ls`: Running `ls` lists the files and directories in the current directory (specific to the `b1` branch).


9. `git log --oneline`: This command displays the commit history of the `b1` branch in a concise format.


10. `touch file4.txt`: This command creates a new file named `file4.txt` in the current directory (specific to the `b1` branch).


11. `echo "a new file created on b1" >> file4.txt`: This command appends the text "a new file created on b1" to the `file4.txt` file.


12. `ls`: Running `ls` lists the files and directories in the current directory (specific to the `b1` branch).


13. `git add .`: This command stages all changes in the repository, including the modified `file4.txt` file.


14. `git commit -m "file add on b1"`: This command creates a new commit on the `b1` branch with the message "file add on b1".


15. `git checkout master`: This command switches back to the `master` branch.


16. `git merge b1 master`: This command merges the changes from the `b1` branch into the `master` branch.


17. Resolve conflicts in `file4.txt`: When there are conflicts in the `file4.txt` file, you need to manually modify the file to resolve the conflicts. Once the conflicts are resolved, save the file.


18. `git status`: This command shows the status of the repository, including the resolved conflicts.


19. `git add .`: This command stages the resolved changes.


20. `git commit -a -m "resolved conflicts"`: This command creates a new commit with the resolved conflicts.


21. `cat file4.txt`: This command displays the contents of the `file4.txt` file.


22. `git checkout b1`: This command switches back to the `b1` branch.


23. `cat file4.txt`: This command displays the contents of the `file4.txt` file in the `b1` branch.


Please note that when resolving conflicts, you may need to use a text editor or a merge tool to modify the conflicting file and remove the conflict markers manually.



bottom of page