jinzhe

jinzhe

github
email

Getting Started with Git

Git is a distributed version control system.

[!NOTE]

  • Version control, as the name suggests, can record history and roll back when needed.
  • The concept of distributed means that there is an additional local repository, and each local repository contains all versions. There is no need to submit online during the process (but in the end, the code still needs to be submitted to the central repository).

add#

The purpose of add is to submit the code changes in the working directory to the staging area to monitor changes within the file.

git add <file>

The file status changes from "untracked" to "staged"

Warning

The add command adds changes to the file, not the file itself!

Quick add#

Stage all files.

git add .

commit#

The purpose of commit is to submit the code in the staging area to the local repository.

git commit

Quick commit#

git commit -m <commit message>

push#

Push local changes to the remote repository. Essentially, it submits the current branch and its commits along with it to the remote repository.

git push <hostname> <branch>

Quick push#

Submit all local branches.

 git push

log#

After completing a commit, you have successfully made a submission. Now, let's check the commit you just made. Here, the commit's SHA-1, author, and commit time are recorded.

git log

View detailed log of content changes#

Display detailed file content changes.

git log -p

View simple log#

Only display file change statistics.

git log --stat

show#

View current commit information only#

git show

View information of a specific commit#

git show SHA-1

View files in a specific commit#

git show SHA-1 <file>

pull#

git pull is actually a combination of git fetch and git merge. It fetches all commits from the target branch and merges them with the current commit to generate a new commit.

git pull <hostname> <branch>

Pulling will automatically merge to some extent:

  • If one branch changes file A and another branch changes file B, both files will be changed after merging.
  • If one branch changes the first line of file A and another branch changes the second line of file A, both the first and second lines of file A will be changed after merging.
  • However, if one branch changes the first line of file A and another branch also changes the first line of file A, there will be a conflict that needs to be resolved manually.

After resolving the conflict, you need to re-add and commit. At this time, you will find that git has automatically filled in the log information for us, and you can submit it.

branch#

The main difference between Git and other version control systems is that Git has the concept of branch, which refers to branches that point to commits.

A reference is equivalent to a shortcut for a commit. We can see that each commit has a SHA-1 hash value, which has a very low repetition rate. We can use the first few values to represent it. For example, we can use 7800ba to represent the previous commit. The HEAD -> master and github/master behind also point to this commit.

HEAD: Reference to the current branch#

The reference to the current branch refers to the commit corresponding to the current working directory. When you make a commit, HEAD will automatically point to the current commit. In short, wherever the current commit is, HEAD is there. You can always use HEAD to operate on the current commit.

branch: Branch#

In addition to referring to a commit, HEAD can also refer to a branch. When pointing to a branch, it indirectly points to the corresponding commit. When the current branch is submitted, HEAD moves along with the corresponding branch.

main: Default branch#

The main branch is created by default when the project is cloned.

Note

All branches are equal.

Branch operations#

Create a branch#

git branch <branch name>

Switch to a branch#

git checkout <branch name>

To simplify the process, you can create a branch and switch to it immediately.

git checkout -b <branch name>

Delete a branch#

git branch -d <branch name>

[!NOTE]

  • When deleting a branch, switch to another branch for deletion.
  • Deleting a branch only deletes the reference to the commit, and the commits in the branch are not deleted.
  • Deleting a branch that has not been merged into master will fail (for safety reasons). Use uppercase -D instead.

Workflow: Feature Branching#

The most popular workflow in the market.

  1. Suppose there is a project that requires you to develop a feature, so you create a new branch on the project.
git checkout -b zhang
  1. After development is complete, you can push it to the remote branch:
git add .
git commit -m "Development completed"
git push origin zhang
  1. Then you tell your colleague to review your code. If your colleague agrees, they will:
git pull
git checkout zhang
  1. If your colleague says there is no problem after reviewing, you can merge zhang into master. If your colleague says there is a problem, you can modify the code and repeat step 2.
git checkout master
git pull
git merge zhang

Finally, push to the remote repository and delete the local/remote branch:

git push
git branch -d zhang
git push origin -d zhang
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.