The use of Git--how to upload local projects to Github (three simple and convenient methods) (2) (detailed explanation)

1. The first method:

1. First of all, you need a github account, so if you don’t have one, register first!

https://github.com/

When we use git, we need to install the git tool first. Here is the download address. After downloading, you can install it all the way (fool-style installation):

https://git-for-windows.github.io/

2. After logging in, enter the Github homepage, click New repository to create a new project

 3. Fill in the corresponding information and click create repository 

Repository name: warehouse name (enter the name, it is best not to use Chinese)

Description (optional): Introduction to the warehouse description

Public, Private : warehouse permissions (public sharing, private or designated collaborators)

Initialize this repository with a README: Add a README.md

gitignore: The type of warehouse that does not require version management, corresponding to the generated file .gitignore

license: certificate type, corresponding to the generated file LICENSE

4. After the creation is successful, the interface is as follows, copy this address for backup.

5. Next, go to the local operation. First, right-click your project. If you have successfully installed git before, two new options will appear on the right-click, namely Git Gui Here and Git Bash Here. Here we choose Git Bash Here and enter In the following interface, Test_Bluetooth is my project name.

 

6. Next, enter the following code (key step) to clone the warehouse on github to the local

git clone https://github.com/CKTim/BlueTooth.git (https://github.com/CKTim/BlueTooth.git is replaced with the address you copied earlier)

 

 7. After this step, there will be an additional folder under your local project folder. The folder name is the project name on your github. As shown in the figure, I have an additional Test folder. Let’s put the local project folder All the files under (except for the new extra folder), the rest are copied to the new extra folder,

 

8. Then continue to enter the command cd Test to enter the Test folder

 

9. Next, enter the following codes in order to complete other remaining operations:

git add * (Note: Don’t forget the following ., this operation is to add all the files under the Test folder)

git commit -m "commit information" (Note: replace "commit information" with what you need, such as "first commit")

git push -u origin master (Note: the purpose of this operation is to push the local warehouse to github, this step requires you to enter your account number and password)

 

 

2. The second method:

Step 1: We need to create a local repository (actually a folder).

You can directly right-click to create a new folder, or you can right-click to open the Git bash command line window to create it by command.

Now I create a new TEST folder on the desktop through the command line (you can also create this folder anywhere else), and enter this folder

 

              

Step 2: Turn this folder into a Git manageable warehouse by command git init  

At this time, you will find that there is an additional .git folder in TEST, which is used by Git to track and manage the repository. If you can't see it because it is a hidden file by default, then you need to set it to make the hidden file visible. 

 Step 3: At this time, you can paste your project into this local Git warehouse (after pasting, you can check your current status through git status), and then add the project to the warehouse through git add (or git add . Add all files in this directory to the warehouse, note that the points are separated by spaces). During this process, you can actually always use git status to view your current status. 

 Here you are reminded that although you have pasted the project, it has not been added to the Git warehouse, and then we will add all the copied projects to the warehouse through git add.

Step 4: Submit the project to the warehouse with git commit.

 

 Inside the quotation marks after -m is the comment content of this submission. This can be left out, but it is better to write it in, otherwise an error will be reported, and you can Google for details. Ok, the work on our local Git warehouse is done, and the next step is to connect to the remote warehouse (that is, to connect to Github)

Since the transmission between the local Git warehouse and the Github warehouse is encrypted by SSH, you need to set it when connecting:

Step 5: Create SSH KEY. First check if there is a .ssh directory in the user directory of your C drive. If so, check if there are two files id_rsa and id_rsa.pub in it. If there are, skip to the next step. If not, create them through the following command.

1. Enter the command below

1

$ ssh-keygen -t rsa -C "[email protected]" 注意ssh-keygen之间没有空格

 2. Then press Enter to ask where to save the key. The default is under the path in brackets. You can modify it or not. As shown below:

3. Do not modify here, press Enter, prompts if it already exists, whether to overwrite. Because this is my second time, I have this prompt as shown in the following question:

4. Enter Y, press Enter, prompt to enter the password, as shown in the following figure:

5. In order to avoid unnecessary trouble, it is better not to set a password, because it is easy to forget, do not enter the password, press Enter, as shown in the following figure:

6. Confirm the password without entering it, press Enter, as shown in the figure:

 The above result appears, indicating that the key is created successfully!

7. At this time, you will find the two files id_rsa and id_rsa.pub in the .ssh directory under the user  

 

Step 6: Log in to Github, find the icon in the upper right corner, open and click Settings inside, then select SSH and GPG KEYS inside, click New SSH key in the upper right corner, then fill in the Title, and then enter the id_rsa.pub just now Copy the contents of the title to the Key content box under the Title, and finally click Add SSH key to complete the encryption of the SSH Key. The specific steps can also be seen below:

     

 

       

Step 7: Create a Git repository on Github.

You can directly click New repository to create it. For example, I created a TEST2 repository (because I already have a test repository in it, so I can no longer create a TEST repository).

Step 8: After creating the Git warehouse on Github, we can associate it with the local warehouse. According to the prompts on the created Git warehouse page, you can enter the command line of the local TEST warehouse:

1

$ git remote add origin https://github.com/guyibang/TEST2.git


       

Note that origin is followed by the address of the warehouse you created on Github.      

 Step 9: After the association is completed, we can push all the contents of the local library to the remote warehouse (that is, Github), through:

$ git push -u origin master
       Since the newly created remote warehouse is empty, the -u parameter should be added. After the remote warehouse has content, the next time you upload content from the local warehouse, you only need to do the following Already:
$ git push origin master
        The process of uploading the project may take a while, and it looks like this after completion:

 

 At this time, if you refresh your Github page and enter the newly created warehouse, you will find that the project has been successfully uploaded:

        So far, the whole process of uploading the local project to Github has been completed.

      In addition, there is a pit here to pay attention to, that is, when creating a remote warehouse in the seventh step above, if you check Initialize this repository with a README (that is, automatically create a README file for you when creating a warehouse), then it is here In the ninth step, when you push the contents of the local warehouse to the remote warehouse, a failed to push some refs to https://github.com/guyibang/TEST2.git error will be reported.

      

      This is because the README file in your newly created warehouse is not in the local warehouse directory. At this time, we can first merge the content with the following command:

$ git pull --rebase origin master

       At this time, you can push again to succeed.

     Summary: In fact, you only need to perform the following steps to upload the local project to Github

     1. Create a repository (folder) locally and turn it into a Git repository through git init;

     2. Copy the project to this folder, and then add the project to the warehouse through git add.

     3. Then submit the project to the warehouse through git commit -m "comment content";

     4. After setting up the SSH key on Github, create a new remote warehouse, and associate the local warehouse with the remote warehouse through git remote add origin https://github.com/guyibang/TEST2.git;

     5. Finally, push the project in the local warehouse to the remote warehouse (that is, Github) through git push -u origin master; (If the README file is automatically created when creating a new remote warehouse, an error will be reported. See the solution above).

3. The third method

The first step: the Git client is installed

Step 2: Register an account with github

Step 3: Create a folder on the local computer, such as: github, enter this folder, and enter cmd in the address bar

Step 4: Click -> to enter the docs command window, enter the git command, and the following figure will appear to indicate that the git installation is successful.

Step 5: Go back to the previous github interface, the following instructions tell you how to upload the code

1

2

3

4

5

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/hongduhong/test.git

git push -u origin master

 

Step 6: In the github file created above, put the code to be uploaded, and then follow the above instructions to start the operation and execute the instructions

1. git init (build a local warehouse)

2. git add * (add the code to the local warehouse, "* is to add all the code, all the code is updated")

3. git commit -m "first commit" (submit to the local buffer, "The quotation marks indicate what was submitted, to put it bluntly, it is a comment")

4. git remote add origin https://github.com/hongduhong/test.git (submit the code of the local warehouse to the remote github warehouse, "The address behind is the remote warehouse address of the previously created github")

5. git push -u origin master (push the code of the remote warehouse to the master branch)

6. The code is successfully uploaded, as shown in the figure below:

Four, Git command

  1 View, add, submit, delete, retrieve, and reset modified files
  2 
  3 
  4 git help <command> # Display the help of command
  5 
  6 git show #Display the content of a submission git show $id
  7 
  8 git co -- <file> # Discard workspace modifications
  9 
 10 git co . # Discard workspace modifications
 11 
 12 git add <file> # Submit the working file modification to the local temporary storage area
 13 
 14 git add . # Submit all modified working files to the temporary storage area
 15 
 16 git rm <file> # delete the file from the repository
 17 
 18 git rm <file> --cached # Delete the file from the repository, but do not delete the file
 19 
 20 git reset <file> # Restore from the temporary storage area to the working file
 21 
 22 git reset -- . # Restore from the temporary storage area to the working file
 23 
 24 git reset --hard # Restore the state of the last submission, that is, abandon all the modifications after the last submission
 25 
 26 git ci <file> git ci . git ci -a # Combine operations such as git add, git rm and git ci together to do git ci -am "some comments"
 27 
 28 git ci --amend # Modify the last commit record
 29 
 30 git revert <$id> # Restore the status of a certain submission, and the restoration action itself also creates a sub-submission object
 31 
 32 git revert HEAD # Restore the state of the last commit
 33 
 34 view file diff
 35 
 36 
 37 git help <command> # Display the help of command
 38 
 39 git show #Display the content of a submission git show $id
 40 
 41 git co -- <file> # Discard workspace modification
 42 
 43 git co . # Discard workspace modifications
 44 
 45 git add <file> # Submit the working file modification to the local temporary storage area
 46 
 47 git add . # Submit all modified working files to the temporary storage area
 48 
 49 git rm <file> # delete the file from the repository
 50 
 51 git rm <file> --cached # Delete the file from the repository, but do not delete the file
 52 
 53 git reset <file> # Restore from the temporary storage area to the working file
 54 
 55 git reset -- . # Restore from the staging area to the working file
 56 
 57 git reset --hard # Restore the state of the last submission, that is, abandon all the modifications after the last submission
 58 
 59 git ci <file> git ci . git ci -a # Combine operations such as git add, git rm and git ci together to do git ci -am "some comments"
 60 
 61 git ci --amend # Modify the last commit record
 62 
 63 git revert <$id> # Restore the status of a certain submission, and the restoration action itself also creates a sub-submission object
 64 
 65 git revert HEAD # Restore the state of the last commit
 66 
 67 View commit history
 68 
 69 git log git log <file> # View each submission record of the file
 70 
 71 git log -p <file> # View the diff of each detailed modification
 72 
 73 git log -p -2 # View the diff of the last two detailed modifications
 74 
 75 git log --stat #View submission statistics
 76 tig
 77 
 78 You can use tig instead of diff and log on Mac, brew install tig
 79 
 80 
 81 Git local branch management
 82 View, switch, create and delete branches
 83 
 84 
 85 git br -r # View remote branches
 86 
 87 git br <new_branch> # Create a new branch
 88 
 89 git br -v # View the last commit information of each branch
 90 
 91 git br --merged # View the branches that have been merged into the current branch
 92 
 93 git br --no-merged # View branches that have not been merged into the current branch
 94 
 95 git co <branch> # switch to a branch
 96 
 97 git co -b <new_branch> # Create a new branch and switch to it
 98 
 99 git co -b <new_branch> <branch> # create new new_branch based on branch
100 
101 git co $id # Check out a historical submission record, but there is no branch information, switching to other branches will automatically delete
102 
103 git co $id -b <new_branch> # Check out a historical commit record and create a branch
104 
105 git br -d <branch> # delete a branch
106 
107 git br -D <branch> # Forcibly delete a certain branch (the unmerged branch needs to be forced when it is deleted)
108 branch merge and reba
109 git merge <branch> # Merge the branch branch into the current branch
110 
111 git merge origin/master --no-ff # Do not merge with Fast-Foward so that merge commits can be generated
112 
113 git rebase master <branch> # rebase master to branch, equivalent to: git co <branch> && git rebase master && git co master && git merge <branch>
114 Git patch management (convenient for development and synchronization on multiple machines)
115 
116 
117 git merge <branch> # Merge the branch branch into the current branch
118 
119 git merge origin/master --no-ff # Do not merge with Fast-Foward so that merge commits can be generated
120 
121 git rebase master <branch> # rebase master to branch, equivalent to: git co <branch> && git rebase master && git co master && git merge <branch>
122 
123 Git temporary storage pipe
124 git stash # Temporary storage
125 
126 git stash list # list all stash
127 
128 git stash apply # restore temporary storage content
129 
130 git stash drop # Delete the temporary storage area
131 
132 Git remote branch management
133 
134 git pull # Grab all branch updates of the remote warehouse and merge them into the local
135 
136 git pull --no-ff # Grab all branch updates of the remote warehouse and merge them into the local, do not fast-forward the merge
137 
138 git fetch origin # fetch remote warehouse updates
139 
140 git merge origin/master # Merge the remote master branch into the local current branch
141 
142 git co --track origin/branch # Track a remote branch and create a corresponding local branch
143 
144 git co -b <local_branch> origin/<remote_branch> # Create a local branch based on the remote branch, the function is the same as above
145 
146 git push # push all branches
147 
148 git push origin master # Push the local master branch to the remote master branch
149 
150 git push -u origin master # Push the local master branch to the remote (if there is no remote master branch, create it to initialize the remote warehouse)
151 
152 git push origin <local_branch> # Create a remote branch, origin is the name of the remote warehouse
153 
154 git push origin <local_branch>:<remote_branch> # create a remote branch
155 
156 git push origin :<remote_branch> #Delete the local branch first (git br -d <branch>), then push to delete the remote branch
157 
158 Git remote warehouse management
159 git remote -v # View remote server address and warehouse name
160 
161 git remote show origin # View remote server warehouse status
162 
163 git remote add origin git@ github:robbin/robbin_site.git # Add remote warehouse address
164 
165 git remote set-url origin [email protected]:robbin/robbin_site.git # Set remote warehouse address (for modifying remote warehouse address) git remote rm <repository> # Delete remote warehouse
166 
167 Create a remote repository
168 
169 git clone --bare robbin_site robbin_site.git # Create a pure version warehouse with a versioned project
170 
171 scp -r my_project.git git@ git.csdn.net:~ # Upload the pure warehouse to the server
172 
173 mkdir robbin_site.git && cd robbin_site.git && git --bare init # Create a pure warehouse on the server
174 
175 git remote add origin [email protected]:robbin/robbin_site.git # Set remote warehouse address
176 
177 git push -u origin master # Client's first submission
178 
179 git push -u origin develop # Submit the local develop branch to the remote develop branch for the first time, and track
180 
181 git remote set-head origin master # Set the HEAD of the remote warehouse to point to the master branch
182 
183 You can also set the command to track remote libraries and local libraries
184 
185 git branch --set-upstream master origin/master
186 
187 git branch --set-upstream develop origin/develop

 5. Problems encountered and solutions

1. If it appears as shown in the figure below, it means that the version of the local git client you installed is too low

solution:

a. Click the right mouse button, click "Git Bash Here", enter as shown in the figure

b. Enter the command git update (git update-git-for-windows) to update the Git client to the latest.

2. When submitting to the remote warehouse, it prompts: fatal: remote origin already exists. Solution: delete the remote warehouse; enter the command: git remote rm origin

3. You need to enter the user name and password for the first operation, just follow the prompts to enter the user name and password

4. See the following prompt in the docs command window:

$git config --global user.name "Jhon" 

$git config --global user.email [email protected]"

solution:

Following the prompt above, enter

>git config --global user.name "here is your github username" 

>git config --global user.email Here is your registered github email address

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131596777