Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Workflow

Git Workflow

In this chapter, we will introduce Git's workflow.

The following figure shows Git's workflow:

We can imagine Git's workflow as the process ofdoing homework and handing it in to the teacher. Combining the figure above, we can divide this process into four main parts:

1. Working Directory — your desk

This is where youactually modify files. Here you write code, delete and make changes.

2. Staging Area — your basket ready to mail

When you feel the homework is almost done, you put it in a basket, ready to be packaged.

3. Local Repository — your personal safe

You package the things in the basket, label them (commit message), and lock them in your own safe.

4. Remote — the teacher's inbox (such as GitHub/GitLab)

Finally, you send the code in the safe to the remote server over the network, so others can view or collaborate.

Knowledge point explanation

Simple summary:
Modify code → add (put in the basket) → commit (store in the box) → push (send to the distance).


Command description

1. Clone repository

If you want to participate in an existing project, you first need to clone the remote repository locally:



git clone https://github.com/username/repo.git

cd repo

2. Create a new branch

To avoid developing directly on the main or master branch, a new branch is usually created:



git checkout -b new-feature

3. Working directory

Edit code, add new files, or delete unneeded files in the working directory.

4. Stage files

Add modified files to the staging area for the next commit operation:



git add filename

# Or add all modified files

git add .

5. Commit changes

Commit the changes in the staging area to the local repository, and add a commit message:



git commit -m "Add new feature"

6. Pull latest changes

Before pushing local changes, it's best to pull the latest changes from the remote repository to avoid conflicts:



git pull origin main

# Or if working on a new branch

git pull origin new-feature

7. Push changes

Push local commits to the remote repository:



git push origin new-feature

8. Create a Pull Request (PR)

Create a Pull Request on GitHub or another hosting platform, inviting team members to review the code. After the PR is merged, your changes will be merged into the main branch.

9. Merge changes

After the PR is approved and merged, you can merge the remote repository's main branch into your local branch:



git checkout main

git pull origin main

git merge new-feature

10. Delete branch

If the new feature branch is no longer needed, you can delete it:



git branch -d new-feature

Or delete the branch from the remote repository:



git push origin --delete new-feature