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":
```
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.
2
ls, git ls-files
Here are the commands you can use to perform the additional tasks:
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.
3
git log , git log --oneline, history, git commit -a -m
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".
4
git diff ,git diff --staged
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.
5
git rm, git rm --cached
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.
6
.gitignore
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.
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.
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.
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
```
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.
8
git reset --hard
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.
9
git branch, git checkout
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).
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. `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.
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. `echo "changes on file1" >> file1.txt`: This command appends the text "changes on file1" to the `file1.txt` file.
2. `echo "changes on file2" >> file2.txt`: This command appends the text "changes on file2" to the `file2.txt` file.
3. `git status`: This command shows the status of your Git repository, indicating the changes made to the files.
4. `cat file1.txt file2.txt`: This command displays the contents of the `file1.txt` and `file2.txt` files.
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. `cat file1.txt file2.txt`: After stashing, the changes made to `file1.txt` and `file2.txt` will no longer be visible in the working directory.
7. `git status`: This command shows the status of the repository, indicating that there are no changes in the working directory.
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. `git stash list`: The stash list remains unchanged.
11. `git stash pop stash@{0}`: This command applies the most recent stash (`stash@{0}`) and removes it from the stash list.
12. `cat file1.txt file2.txt`: The changes made to `file1.txt` and `file2.txt` are reapplied to the working directory.
13. `git status`: This command shows the status of the repository, indicating the changes made to the files.
14. `git stash apply stash@{0}`: This command applies the stash (`stash@{0}`) to the working directory without removing it from the stash list.
15. `git stash list`: The stash list remains unchanged.
16. `git status`: The status of the repository shows the applied changes.
17. `git stash clear`: This command removes all stashes from the stash list.
18. `git stash list`: The stash list is now empty.
19. `git status`: The status of the repository shows the changes made to the files.
20. `git stash -p`: This command allows you to interactively select specific changes to stash.
21. `git status`: The status of the repository shows the changes made to the files.
22. `git stash list`: The stash list remains unchanged.
23. `git stash`: This command creates a new stash with the changes in the working directory.
24. `git stash list`: The new stash is added to the stash list.
25. `git show stash@{1}`: This command displays the changes made in the second most recent stash (`stash@{1}`).
26. `git show stash@{0}`: This command displays the changes made in the most recent stash (`stash@{0}`).
27. `cat file1.txt file2.txt`: The contents of `file1.txt` and `file2.txt` are shown.
28. `git drop stash@{0}`: This command removes the most recent stash (`stash@{0}`) from the stash list.
29. `git stash list`: The stash list is updated and shows the remaining stash.
30. `git stash drop stash@{0}`: This command removes the specific stash (`stash@{0}`) from the stash list.
31. `git status`: The status of the repository shows the changes made to the files.
32. `cat file1.txt file2.txt`: The contents of `file1.txt` and `file2.txt`
are shown without the changes made in the stash.
12
git rebase
1. `git status`: This command shows the status of your Git repository, indicating any changes or untracked files.
2. `git log --oneline`: This command displays the commit history in a concise format, showing the abbreviated commit hashes and commit messages.
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. `git log --oneline`: 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. `touch file3.txt`: This command creates a new file named `file3.txt`.
7. `echo "new txt file for master" >> file3.txt`: This command appends the text "new txt file for master" to the `file3.txt` file.
8. `git add .`: This command stages all changes in the repository, including the new `file3.txt` file.
9. `git commit -m "txt file add to master"`: This command creates a new commit on the `master` branch with the message "txt file add to master".
10. `git status`: This command shows the status of the repository, indicating there are no further changes.
11. `git log --oneline`: This command shows the updated commit history, including the commit with the message "txt file add to master".
12. `git checkout b2`: This command switches to the `b2` branch.
13. `git log --oneline`: This command shows the commit history of the `b2` branch.
14. `touch file4.txt`: This command creates a new file named `file4.txt`.
15. `echo "new txt file for b2" >> file4.txt`: This command appends the text "new txt file for b2" to the `file4.txt` file.
16. `git add . file4.txt`: This command stages the new `file4.txt` file.
17. `git commit -m "txt file add to b2"`: This command creates a new commit on the `b2` branch with the message "txt file add to b2".
18. `git status`: This command shows the status of the repository, indicating there are no further changes.
19. `git log --oneline`: This command shows the updated commit history, including the commit with the message "txt file add to b2".
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. `git log --oneline`: This command shows the updated commit history after the rebase, including the commits from `master`.
Please note that the `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.
13
git remote add origin, git remote -v, git push origin, git branch -d, git push origin --delete
1. Create an empty repository named "repo-demo1" on GitHub:
- Go to the GitHub website (https://github.com) and log in to your account.
- Click on the "+" sign in the top-right corner and select "New repository" from the dropdown menu.
- Enter the repository name as "repo-demo1" and provide optional details like description, visibility (public or private), and other options.
- Click on the "Create repository" button to create the empty repository.
2. Copy the URL of the "repo-demo1" repository:
- Once the repository is created, you can copy the URL by navigating to the repository page on GitHub. The URL should be displayed in the address bar of your browser. For example: `https://github.com/your-username/repo-demo1.git`.
- Copy the URL as you will need it in the following steps.
3. Add the remote repository URL as the origin:
- Open your terminal or command prompt and navigate to your local repository's directory using the `cd` command.
- Run the following command to add the remote repository URL as the origin:
```
git remote add origin https://github.com/your-username/repo-demo1.git
```
- Replace `your-username` with your actual GitHub username and the URL with the one you copied.
4. Use GitHub credentials:
- When you perform Git operations that require authentication with GitHub, such as pushing or pulling, Git will prompt you to enter your GitHub credentials. Enter your GitHub username and password when prompted.
5. Verify the remote repository URL:
- To verify that the remote repository URL has been added correctly, run the following command:
```
git remote -v
```
- You should see the remote repository URL listed for the origin.
6. Push a branch named "b1" to the remote repository:
- Create and checkout a new branch named "b1" using the following command:
```
git checkout -b b1
```
- Make the desired changes to your files and commit them locally using `git commit`.
- Push the branch to the remote repository using the following command:
```
git push origin b1
```
7. Delete the local branch "b1":
- Switch back to the `master` branch using the command:
```
git checkout master
```
- Delete the local branch "b1" using the following command:
```
git branch -d b1
```
8. Delete the remote branch "b1":
- Delete the remote branch "b1" using the command:
```
git push origin --delete b1
```
Following these steps will
create an empty repository,
copy the URL,
add the remote repository URL as the origin
use your GitHub credentials, verify the remote repository URL,
push a branch to the remote repository, delete the local branch,
and finally delete the remote branch. Make sure to replace "your-username" with your actual GitHub username and adjust the repository URL accordingly.
14
git clone
To create a remote repository named "demorepo2" with a `README.txt` file and then clone it to a local repository named "local-repo," follow these steps:
1. Create the remote repository on GitHub:
- Go to the GitHub website (https://github.com) and log in to your account.
- Click on the "+" sign in the top-right corner and select "New repository" from the dropdown menu.
- Enter the repository name as "demorepo2" and optionally provide a description.
- Check the "Initialize this repository with a README" option to create a `README.txt` file.
- Click on the "Create repository" button to create the remote repository.
2. Clone the remote repository to a local repository:
- Open your terminal or command prompt and navigate to the desired location where you want to create the local repository ("local-repo").
mkdir local-repo
- Change to the "local-repo" directory using the `cd` command: `cd local-repo`.
- Run the following command to clone the remote repository to the local repository:
```
git clone https://github.com/RajaDevops/demorepo2.git
```
- This command will clone the "demorepo2" repository from GitHub into the "local-repo" directory.
3. Check the contents of the "local-repo" directory:
- Use the `ls` command to list the contents of the current directory:
`ls`.
- You should see the "demorepo2" directory listed among the files and directories.
4. List the contents of the "demorepo2" directory:
- Change to the "demorepo2" directory using the `cd` command: `cd demorepo2`.
- Run the `ls -l` command to list the contents of the "demorepo2" directory with detailed information.
ls -l
5. List all contents (including hidden files) of the "demorepo2" directory:
- Run the `ls -la` command to list all files and directories, including hidden files, in the "demorepo2" directory.
ls -la
6. git remote -v to check the remote repository
Following these steps, you will have created a remote repository named "demorepo2" with a `README.txt` file, cloned it to the local repository "local-repo," and listed the contents of both directories.