Use Git to push code to GitHub or a local remote warehouse

Beginning on August 13, 2021, GitHub no longer supports the use of username and password for authentication when using Git to operate remote warehouses, and a personal access token is required.

Git commits code to the repository using a personal access token

The following tutorial uses the username and password to authenticate the identity, and the password needs to enter the token personal token instead of using the user password for authentication.

1. First log in to the GitHub official website to create a warehouse.

Created a repository called test

2. Create a project folder test locally (with any name)

2.1. Enter the test folder to initialize the local warehouse.

git init

2.2. Create a java file under the test folder

2.3. Execute git add . to add files into the cache area, . means all files in the current folder

git add .

2.4. Execute git commit -m "description", you can not add any description, and the double quotes are empty, there must be double quotes

If the execution is unsuccessful, you need to set the user name and mailbox, which can be set globally or in a single local warehouse

2.5. Change the config file in the .git folder under the test folder.

[user]
name=你的用户名
email=你的邮箱

Or modify git config user.name "username" and git config user.email "mailbox" by command, the global settings need to add --global

git config user.name "xx"
git config user.email "[email protected]"

2.6. Re-execute git commit -m "first commit"

2.7. Add remote warehouse source git remote add origin warehouse address, which can be executed after initializing the local warehouse.

git remote add origin https://github.com/xxxx/test.git //你的仓库地址

Check the config file to automatically add the warehouse source

2.8. Execute git push origin master to push the local warehouse code to the remote warehouse.

git push origin master

After the command is executed, you need to enter your GitHub account and password.

2.9. The code has been successfully pushed to the remote GitHub repository.

3. Simulate multi-person collaboration to submit code to the GitHub warehouse.

3.1. Create a project folder test1 again, enter and execute the git init command to initialize the local warehouse

3.2. Create a bb.java file, execute git add . and git commit -m "second submission"

You also need to set the username and email address for the local warehouse

3.3, Execute git remote add origin warehouse address, set the remote warehouse address

3.4. Execute git push origin master to push the code to the remote warehouse.

The general meaning is: the update was rejected because other warehouses pushed the updated code to the remote warehouse, resulting in inconsistencies between the remote warehouse and the local warehouse files. You need to pull the code to the local before pushing the code.

3.5. Execute git pull origin master.

refusing to merge unrelated histories: Refusing to merge unrelated histories.

Force a merge: git pull origin master --allow-unrelated-histories

git pull origin master --allow-unrelated-histories

Before execution:

After execution:

3.6. Execute git push origin master again

git push origin master

4. Modify the file and push the update again.

4.1. Add code to aa.java file

public class aa{
	public static void main(String args[]){
		System.out.println();
	}
}

4.2. Add the code to the cache, submit and push the update.

The previously empty file aa.java has been updated

5. Create a remote warehouse locally, and the shared LAN can be accessed.

5.1. Create a folder local in virtual machine 1, and initialize the warehouse through git init --bare

git init --bare

5.2. Set the local folder as a share, and the permission is Everyone.

Right-click the folder--Properties--Sharing--Advanced Sharing--check Share this folder--Permissions--Everyone--Check Full Control

5.2.1. Sharing settings

5.2.2

 5.2.3

5.2.4, security settings

 If you do not add the Everyone user and its permissions in Security in this step, you can access the shared folder through IP after setting 5.2.5 and 5.2.6, but when you open the folder, it prompts that there is no access permission, as shown in the figure below:

 5.2.5. Family or workgroup settings

 

 5.2.6, public network settings

 

5.3. Accessible through \\ip\shared folder name

This time the shared folder is set to share under win7 in virtual machine 1, the IP address is: 192.168.5.103, and the file name is: local

The IP address of the physical machine is: 192.168.5.100

 

 Access the shared folder in the virtual machine through \\192.168.5.103\local in the physical machine

 Share success.

5.4. Create a folder local-1 on the physical machine as a local warehouse

5.4.1, git init initializes the local warehouse

git init

5.4.2, git remote add [name] [url] add remote warehouse address, name can be named at will

git remote add origin //192.168.5.103/local

5.4.3、

git config user.name set username

git config user.email set mailbox

git config user.name "jj"
git config user.email "[email protected]"

5.4.4. Go to the local-1 folder to create a file a.java, and push it to the remote warehouse.

5.4.5. git add under the local-1 folder. Add all changes under this folder to the cache area.

git add .

5.4.6, git commit -m "description information" under the local-1 folder

git commit -m "提交到本地仓库"

5.4.7, git push origin master under the local-1 folder

git push origin master

5.5. Create a local-2 folder in virtual machine 2 to simulate the local warehouse of collaborators.

5.5.1, git init under the local-2 folder initializes the local warehouse

git init

5.5.2, git remote add [name] [url] under the local-2 folder to add the source address of the remote warehouse

git remote add  origin //192.168.5.103/local

5.5.3. Under the local-2 folder, git pull origin master pulls the code from the master branch of the remote warehouse to the local

git pull origin master

 

 At this time, the pulled file is an empty file. After adding a piece of code to the file, push it to the remote warehouse again.

5.5.4. Add code to a.java

public class a{
	public static void main(String args[]){
		System.out.println("hello a!");
	}
}

5.5.5. Update the code to the remote warehouse

git config user.name

git config user.email

git add .

git commit -m "description"

git push origin master

5.5.6. Under local-1 on the physical machine, pull the code from the remote warehouse local to the local again.

git pull origin master

 

 At this time, the file size of the physical machine pulled from the remote warehouse to the physical machine has changed, and the remote code warehouse has been built locally.

Guess you like

Origin blog.csdn.net/weixin_44341110/article/details/118252150