You are on page 1of 39

GIT LAB EXERCISE

1.git init, git config --global user.email, git config --global user.name, git
status, git add, git commit
1. Create a directory named myrepo

2. Now we need to navigate to the myrepo directory

3. We need to initialize the Git in the directory by using git init command

4. In the next step we need to configure our global user email and name by the following
commands:

Replace the email with your github email and user name with github username.

5. We need to create a file named “file1.txt”

6. Now check the git status


7. Add “file1.txt” to git through the following command:

8. Again check the git status

Now Git should show that "file1.txt" has been added to the staging area.
9. Commit "file1.txt" to Git with the message "new file"

10. Again check the git status


2 ls, git ls-files
1. To display the list of files in the current directory we need to use the command ls

2. To list the files tracked by the git we need to use the following command

3. Create a file named “file2.txt” and write "hello world" to it:

4. Now need to know how many files are present use the following command

5. Now we need to know the list of files in git

6. Now that we have created the file2.txt we need to verify the status of git

7. Add "file2.txt" to Git

8. Again check the git status


9. Commit "file2.txt" to Git with the message "second file"

10. List the files in the current directory

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

11. List the files tracked by Git

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.
3 git log , git log --oneline, history, git commit -a –m

1. To view the git commit history we use the following command

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

2. To view the git commit history in a concise format we use the following command

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

3. To view the command history we use the following command


4. Append the text "new line 1st time" to "file1.txt"

This command appends the line in the text file file1.txt

5. Check the git status

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


6. Now we need to commit all the changes including the modifications in the “file1.txt”
to Git
The `-a` flag tells Git to automatically stage any modified files.

7. Again check the git status

8. List the files in the current directory using the ls command

9. To view the content of file1.txt we need to use the cat command.

This will display the contents of "file1.txt", including the newly added line.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".
4. git diff ,git diff –staged
1. Create a file named "file3.txt".

2. Add the first line to "file3.txt".

3. Check the Git status

4. Add "file3.txt" to Git

5. Check the Git status again

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"


7. Again Check the Git status

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

8. Now we need to add the second line to our file.

9. Check the Git status

10. To View the differences in "file3.txt" that are not yet staged

11. To view the differences in "file3.txt" that are staged (already added)
12. Add the changes in "file3.txt" to Git

13. Again checking the Git status

14. View the differences in "file3.txt"

15. View the differences in "file3.txt" that are staged (already added)

16. Add the third line to "file3.txt"

17. View the differences in "file3.txt"

18. View the differences in "file3.txt" that are staged


19. Check the Git status

20. Commit the changes in "file3.txt" to Git with the message "updated file3 with new
lines".

21. Check the Git status

22. View the differences in "file3.txt"


23. View the differences in "file3.txt" that are staged (already added)

We have created, modified, and committed changes to "file3.txt" in Git, and checked the
Git status and differences at various stages.
5 git rm, git rm –cached

1. To remove "file3.txt" from Git and delete the file

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

2. Check the 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"

4. Check the 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
This command removes "file2.txt" from the Git repository but keeps it in the file system.

6. Check the Git status

7. List the files in the current directory with detailed information

8. List the files tracked by Git

9. Check the Git status

10. Commit the deletion of "file2.txt" from the Git repository only, with the message
"deleted from only local repo"
11. Check the Git status

12. List the files in the current directory with detailed information

13. List the files tracked by Git

We 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.
6.gitignore

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.

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).
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.

1. Check the Git status

2. Create the `.gitignore` file

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

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

4. Check the Git status


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

5. Add the `.gitignore` file to Git

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

6. Commit the addition of the `.gitignore` file with the message "ignore file added"

7. Check the 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.
7. git revert
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

1. Run `git log --oneline` to view the commit history

`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.
8 git reset –hard

1. Check the Git status

2. View the commit history in a condensed format

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

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


5. Reset the repository to a specific commit

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

7. Check the Git status

This will show the current status of your Git repository after the reset operations.
We have checked the Git status, viewed the commit history, performed `reset --hard`
operations, and checked the Git status again at different stages.
9 git branch, git checkout

1. git branch: 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 . This indicates that the branch b1 is created based on the master
branch.

3. Now we need to again execute the command git branch .Here we need to observe that the
output will show 2 branches

4. Now we need to change the git branch from master to b1 , to do that we need to execute
the following command.

5. Again we need to execute the git branch command

6. Now we need to check the status of git

7. Execute the following command to now the git commit history


8. Now create a text file file3.txt using the following command

9. We are including a new line in file3.txt using the following command

10. Check the git status

11. Now add the file3.txt

12. Now commit the changes made in file3.txt

13. Check the git status

14. To know the commit history once again execute the following command
15. To know about the list of files in the current working directory execute the following
command

16. To know about the list of files tracked by git use the following command

17. Till now we are in the b1 branch to change the branch to master we use the following
command.

18. Now use the ls command to know about the files in the current working directory i.e
master branch

19. To know about the list of files tracked by git we use the following command.

20. Check the git status

21. Now execute the following command to know about the commit history
10 git merge

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. We need to create a new file file4.txt

2. We need to append the text to file4.txt

3. Check the git status

4. This command stages all changes in the repository, including the new `file4.txt` file
5. This command creates a new commit on the `master` branch with the message "file on
master".

6. To know the commit history we need to execute the following command

7. Now switch the branch to b1

8. To know about the files in current working directory use the following command.

9. To know the commit history of b1 use the following command

10. We have already created a text file (file4.txt) in master branch , we also need to create the
same text file in b1 branch . To create the file we use the following command.

11. To append the text in file4.txt we need to use the following command.
12. Run the ls command to know about the files in the current working directory.

13. To stage all changes in the repository, including the modified `file4.txt` file we need to
use the following command.

14. The following command creates a new commit on the `b1` branch with the message "file
add on b1".

15. To switch back again to the master branch use the following command.

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

Here there is a conflict arising . The conflict needs to be modified manually . All we need to
do is we need to go to myrepo and go to file4.txt . So open the file4.txt and remove all the
lines or combine the two lines by removing the unnecessary symbols. Then save the file.

17. Check the git status

18. The following command stages the resolved changes


19. The following command creates a new commit with the resolved conflicts

20. To display the contents of the `file4.txt` file, we use the following command.

21. To switch back to the b1 branch use the following command.

22. To display the contents of file4.txt when we are in b1 branch we use the following
command.

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.
11 git stash, git stash list, git show stash , git stash pop stash, git stash apply
stash, git stash clear, git stash -p, git drop stash

1. To make changes for file1.txt i.e here we are appending a new line to file1.txt that we had
already created in the previous steps. To append the line we need to use the following
command.

2. Similarly we apply the same command to append a new line to file2.txt

3. Check the git status

4. To display the contents of file1.txt and file2.txt we use the following command

5. git stash: This command saves the changes in a new stash and reverts the working
directory to the state of the last commit.

6. After stashing, the changes made to `file1.txt` and `file2.txt` will no longer be visible in the
working directory.
To display the contents use the following command.
7. Check the git status

8. git stash list: This command lists all the stashes that have been created.

9. git show stash@{0}: This command displays the changes made in the most recent stash
(`stash@{0}`).

10. Again execute git stash list command

11. git stash pop stash@{0}: This command applies the most recent stash (`stash@{0}`) and
removes it from the stash list.
12. The changes made to `file1.txt` and `file2.txt` are reapplied to the working directory. To
see the changes applied execute the following command.

13. Check the git status

14. Now apply the following command

15. Apply the following command.

16. Check the git status


17. git stash clear: This command removes all stashes from the stash list.

18. Now execute the following command

19. Check the git status

20. git stash -p: This command allows you to interactively select specific changes to stash.
21. Check the git status

22. Execute the git stash list command.

23. To create the new stash execute the following command.

24. Now execute the git stash list command to check if the new stash is added to the list

25. git show stash@{1}: This command displays the changes made in the second most recent
stash (`stash@{1}`).
26. Now execute the following command

27. Display the contents of file1.txt&file2.txt

28. The following command removes the most recent stash (`stash@{0}`) from the stash list.

29. Execute the git stash list command

30. Check the git status

31. The contents of `file1.txt` and `file2.txt` are shown without the changes made in the stash.
12 git rebase

1. Check the git status

2. To view the git commit history use the following command

3. git reset --hard: This command resets the current branch and working directory to the
state of a specific commit. With `--hard`, it discards any changes and moves the branch
pointer to the specified commit.

4. After the reset, this command shows the updated commit history, excluding the discarded
commits.

5. git branch b2 master: This command creates a new branch named `b2` starting from the
`master` branch.

6. Create a new file file3.txt


7. Append the text for the created file using the following command.

8. The following command stages all changes in the repository, including the new `file3.txt`
file.

9. The below command creates a new commit on the `master` branch with the message "txt
file add to master".

10. Check the git status

11. View the commit history

12. Switch to branch b2 through the following command

13. To view the commit history of b2 branch use the following command.
14. Create a new file file4.txt

15. Append the text using the following command

16. This command stages the new `file4.txt` file.

17. This command creates a new commit on the `b2` branch with the message "txt file add to
b2".

18. Check the git status

19. View the commit history


20. git rebase master: This command applies the commits from the `master` branch on top
of the `b2` branch. It integrates the changes from `master` into `b2` and replays the commits
on top of the latest commit in `b2`.

21. Again view the commit history

git rebase command may cause conflicts if there are conflicting changes between the
branches. In such cases, you will need to resolve the conflicts manually.

You might also like