Overview of how to clone, modify, add, and delete files in Git
In the first article in this series when we started using Git, we created a simple Git repository and connected it to our computer and added a file to it. In this article, we will learn some other things about Git, how to clone (download), modify, add, and delete files in the Git repository.
Let's clone it
Suppose you already have a Git repository on GitHub and want to get your files from it - maybe you have lost a local copy on your computer, or you are working on another computer but want to access the repository Documents, what should you do? Download your file from GitHub? That's right! In Git terminology we call it "clone clone". (You can also download the repository as a ZIP file, but we will discuss cloning in this article.)
Let's clone the repository named Demo created in the previous article. (If you haven't created the Demo repository, skip back to that article and perform those steps before proceeding.) To clone a file, simply open a browser and navigate to https://github.com/
As you can see above, the "Clone with HTTPS using HTTPS" option is turned on. Copy your warehouse address from this drop down box (https://github.com/
Git clonehttps://github.com/
Then, to see the list of files in the Demo directory, enter the following command:
Ls Demo/
The terminal should look like this:
Modify the file
Now that we have cloned the repository, let's modify the files and update them on GitHub. First, enter the following command one by one, change the directory to Demo/, check the contents of README.md, add new (additional) content to README.md, and then use git status to check the status:
Cd Demo/
Ls
Cat README.md
Echo"Added another line to REAMD.md" >> README.md
Cat README.md
Git status
If you run these commands one by one, the terminal will look like this:
Let's take a look at the output of git status and see what it means. Don't worry about such statements:
On branch master
Your branch isup-to-date with 'origin/master'.".
Because we haven't learned this yet. (LCTT Annotation: learn it, you know) The next line says: Changes not staged for commit; this is to tell you that the files listed below are not marked for preparation ("stage planning") to submit. If you run git add, Git will mark these files as Ready for commit; in other words, Changes staged for commit. Before we do this, let's use the git diff command to check what we added to Git and then run git add.
Here is the terminal output:
Let's analyze it:
Diff --git a/README.md b/README.md is the content of the Git comparison (README.md in this example).
--- a/README.md will show anything removed from the file.
+++ b/README.md will show anything added from the file.
Anything added to the file is printed in green text with a + sign at the beginning of the line.
If we delete anything, it will print in red text and add a - at the beginning of the line.
Now git status displays Changes to be committed: and lists the file name (ie, README.md) and what happened to the file (ie it has been modified and is ready to commit).
Tip: If you have already run git add, now you want to see what the file is different, usually git diff will not output anything because you have already added the file. Instead, you must use git diff --cached. It will tell you the difference between the current version of Git and the previous version of the file. Your terminal output looks like this:
Upload files to your repository
We modified the README.md file with some new content and it is time to upload it to GitHub.
Let's submit the changes and push them to GitHub. run:
Git commit -m"Updated Readme file"
This tells Git that you are "submitting" changes that have been "added". As you may remember, from the first part of this series, it is very important to add a message explaining what you did in the submission so that you can After reviewing the Git logs, understand the purpose at the time. (We'll pay more attention to this topic in the next article.) The Update Readme file is the submission message - if you think this does not reasonably explain what you did, write down your submission as needed.
Run git push -u origin master, which will prompt you for a username and password, and upload the file to your GitHub repository. Refresh your GitHub page and you should see the changes you just made to README.md.
The lower right corner of the terminal shows that I submitted the change, checked the Git status, and pushed the change to GitHub. Git status shows:
Your branch isahead of'origin/master'by1commit
(use "git push" topublish your local commits)
The first line indicates that there is a commit in the local repository but not in origin/master (that is, on GitHub). The next line instructs us to push these changes to origin/master, which is what we did. (In this example, please refer to the first article in this series to awaken your memory of the origin. I will explain the meaning of master when I discuss the branch in the next article.)
Add new files to Git
Now that we have modified a file and updated it on GitHub, let's create a new file, add it to Git, and upload it to GitHub. run:
Echo"This is a new file" >> file.txt
This will create a new file named file.txt.
If you use cat to view it:
Cat file.txt
You will see the contents of the file. Now continue to run:
Git status
Git reports that there is an untracked file (named file.txt) in your repository. This is Git telling you that there is a new file in the repository directory on your computer, but you didn't tell Git that Git did not track any changes you made.
We need to tell Git to track this file so that we can submit and upload the file to our repository. The following are the commands to perform this operation:
Git add file.txt
Git status
The terminal output is as follows:
Git status tells you that file.txt has been modified. For Git it is a new file, which Git didn't know before. Now that we have added file.txt to Git, we can submit changes and push it to origin/master.
Git now uploads this new file to GitHub; if you refresh the GitHub page, you should see the new file file.txt in the repository on GitHub.
With these steps, you can create as many files as possible, add them to Git, and submit and push them to GitHub.
Delete files from Git
What if we find that we made a mistake and need to delete file.txt from our repository? One way is to delete the file from the local copy using the following command:
Rm file.txt
If you do git status now, Git will say there is a file not staged for commit, and it has been removed from the local copy of the repository. If we run now:
Git add file.txt
Git status
I know we are deleting this file, but we still run git add because we need to tell Git what we are doing. git add can be used to add new files, modify the contents of an existing file, or delete files from the repository. Time. In fact, git add takes all changes into account and submits those plans to these changes. If in doubt, check the output of each command in the screen capture below.
Git will tell us that the deleted file is being submitted. As soon as you submit this change and push it to GitHub, the file will also be removed from GitHub's repository. Run the following command:
Git commit -m"Delete file.txt"
Git push -uorigin master
Now your terminal looks like this:
Your GitHub looks like this:
Now you know how to clone, add, modify, and delete Git files from your repository. The next article in this series will examine the Git branch.
Iwatch Screen Protector,Tempered Glass Package,40Mm Tempered Glass,44Mm Tempered Glass
Shenzhen TUOLI Electronic Technology Co., Ltd. , https://www.hydrogelprotector.com