Create a version library

Use the git init command to turn this directory into a git managed repository

1. Right-click the current folder and select Git bash here

2. If you do not see the.git directory, it is hidden by default, you can use ls -ah to see or folder ‘view > hidden project

Add the file to the repository

The first step is to tell Git to add the file to the staging area git add file

File is the name of the file or. Stands for add all files

It is normal to execute the above command without any display

Git commit -m “wrote a readme file”

-m Indicates the description of the submission. It is mandatory

Some operations of the version library

Modify the file, and then commit the changes to the Git repository

git status

Git status allows you to keep track of the current status of your repository. The command output above tells you that changes have been made but are not ready to commit.

git diff

If git status tells you that a file has been modified, use git diff to check the changes.

After making the modifications, The first step is git add file. The second step is git commit -m “wrote a readme file”. The working tree is clean


Version back

The git log command displays commit logs from the most recent to the most distant

Git log –pretty=oneline git log –pretty=oneline In Git, HEAD represents the current version

git reset --hard HEAD^

Git reset — go back to the current version, meaning go back to the last commit version

1. The previous version was HEAD^ and the previous version was HEAD^^. It’s easy to count 100 ^, so write HEAD~100

2.HEAD^ can be changed to the first seven digits of the version number

If you want to go back to the previous version, as long as the command line window is not closed, you can find the append GPL commit ID is 1094adb… You can then specify a future version

Git reflog Git reflog Git reflog

The git reset command can roll back both the version and the changes made in the staging area to the workspace


Management of change

How does Git keep track of changes, and if you don’t add Git to the staging area, you won’t be added to the commit


Undo modify

Git checkout — ‘file’ means to undo all changes to the file file in the workspace

There are two scenarios

1. One is that the file has not been put into the staging area since the modification. Now, undoing the modification will return to the same state as the repository

2. Readme. TXT has been added to the staging area and has been modified

The result is to return the file to the state it was at the time of the last Git commit or Git add.

summary

Scenario 1: When you tamper with the contents of a file in your workspace and want to discard the workspace changes directly, use git checkout — file.

Scenario 2: If you change the contents of a file in your workspace and add it to the staging area, you want to discard the changes. In the first step, run git reset HEAD to go back to scenario 1. In the second step, follow scenario 1

Scenario 3: An inappropriate change has been committed to the repository and you want to undo the commit, refer to the version Rollback section, but only if it has not been pushed to the remote repository


Delete the file

In most cases, delete unnecessary files directly in the file manager, or use the rm command to delete rm files

summary

The rm command is used to delete a file. If a file has already been committed to the repository, then you never have to worry about deleting it by mistake, but be careful, you can only restore the file to the latest version, and you will lose what you changed since the last commit.

Git checkout — file git checkout — file

Git Checkout replaces the version in your workspace with the version in your repository.

Note: files that are deleted without ever being added to the repository are not recoverable!


Remote warehouse

Local Git connects to github

Git config –global user.name “your user name”

Step 2, generate SSh

Ssh-keygen -t rsa -c "register email"

Enter all the way

In SSH, there are two files, usually the user in drive C. id_rsa private key 2. Id_rsa. pub Public key

Step 3: Add SSH to Github

Fourth, test whether the connection is successful

Open the Git command panel

		ssh -T [email protected]
Copy the code

Note: Github allows multiple keys to be added. If there are several computers, as long as the key of each computer is added to Github, you can push to Github from each computer


Adding remote Libraries

After creating a Git repository locally, you want to create a Git repository on GitHub and synchronize the two repositories remotely, so that the repository on GitHub can both be used as a backup and allow others to collaborate with the repository.

First, log in to GitHub, and then go to the “Create a new repo” button in the upper right corner, Create a new Repository, enter the Repository name, keep the default Settings, and click the “Create Repository” button. You have successfully created a new Git repository

Now let’s follow GitHub’s instructions and associate the local repository with the remote repository by running a command in the local repository:

$ git remote add origin [email protected]:GoodMeng/10-13.git/

The name of the remote repository will be Origin, which is Git’s default name and can be changed to something else, but origin is clearly the name of the remote repository

Next, you can push all the contents of the local library to the remote library:

$ git push -u origin master

Git push git push git push git push git push git push git push git push git push git

Git will not only push the contents of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch. This can be simplified to remove -u in future pushes or pulls.

From now on, whenever a local commit is made, you can use the command

$ git push origin master

Push the latest changes from your local master branch to GitHub, and you now have a truly distributed repository


Clone from a remote repository

To clone a repository, you must first know its location and then use the git clone command to clone it

$ git clone [email protected]:*****/****.git

Clone to a local file


Branch management

Create and merge branches

Git branch name

Git checkout name or Git switch name

Git checkout -b name or git switch -c name

Git merge name: git merge name

Delete a branch: git branch -d name

Resolve the conflict

Modify the file

Commit on the dev branch

Switch to the Master branch

Submit the file on the master branch

Now, the master branch and the Dev branch each have new commits that look like this

Git uses <<<<<<<, =======, and >>>>>>> to mark the contents of different branches to resolve the conflict. This is to manually edit the files that Git failed to merge into the desired contents and then commit.

Use git log –graph to see the branch merge graph


Branch Management Strategy

First of all, the Master branch should be very stable, that is, it should only be used to release new versions, and not work on it.

All the work is on the dev branch, that is, the dev branch is not stable, and at some point, for example, in the 1.0 release, the dev branch will be merged into the master branch, and everyone will have their own branch in the master 1.0 release, and from time to time the dev branch will be merged into the master branch

When merging branches, add –no-ff parameter to merge branches in normal mode, merge history has branches, you can see that the merge, and fast forward does not see that the merge.

git merge --no-ff -m "merge with no-ff" dev

Look at branch history after merge

git log --graph --pretty=oneline --abbrev-commit

Just like the following

$ git log --graph --pretty=oneline --abbrev-commit
*   e1e9c68 (HEAD -> master) merge with no-ff
|\  
| * f52c633 (dev) add merge
|/  
*   cf810e4 conflict fixed
Copy the code

Bug branch

When fixing bugs, we fix them by creating new bug branches, then merging them, and finally deleting them;

When the work in hand is not finished, first put a git stash on the work site, then go to fix the bug, and then go back to the work site with a Git stash pop.

Git cherry-pick can be used to “copy” bugs fixed on the master branch to the current dev branch to avoid duplication of effort.


Feature branch

To develop a new feature, create a branch

If you want to drop a branch that has not been merged, you can forcibly delete it using git branch -d


collaboration

  1. Git remote -v
  2. Branches created locally are not visible to others unless they are pushed remotely.
  3. If the push fails, use Git pull to grab the new commit from the remote branch.
  4. To create a branch corresponding to the remote branch, run the git checkout -b branch-name origin/branch-name command. The name of the local branch and the remote branch must be the same.
  5. Git branch –set-upstream branch-name origin/branch-name
  6. Grab branches remotely, using Git pull, and if there are conflicts, handle them first.

Some common mistakes

1. The following errors often occur when you add Git on Windows

fatal: LF would be replaced by CRLF in —

Cause: In Windows, the default git newline character is CRLF. When git add is submitted, an alarm is generated if the LF newline character (in Linux) is found in the text. The solution is simply to have Git ignore the check

Git config –global core. Autocrat lf false

2. Using git log, only the cursor blinks and you cannot enter the command