YouTip LogoYouTip

Git Workflow

Git Workflow | Rookie Tutorial

This section will introduce you to Git's workflow.

The diagram below illustrates Git's workflow:

Image 1

We can imagine Git's workflow as the process of writing homework and submitting it to your teacher. Combined with the diagram above, we can divide this workflow into four main parts:

1. Working Directory β€” Your Desk

This is where you actually modify files. You write code here and make changes freely.

  • Action: You implement a new feature or fix a bug here.

2. Staging Area β€” Your "To-Be-Mailed" Basket

When you feel your homework is nearly done, you put it into a basket, ready to be packaged.

  • Key command: git add
  • Meaning: Tell Git, "I've confirmed these changes should be committedβ€”please remember them for now."

3. Local Repository β€” Your Personal Safe

You package the contents from your basket, label it (with a commit message), and lock it into your personal safe.

  • Key command: git commit
  • Meaning: The changes officially become part of your project history. Even if you mess things up later, you can always restore from here.

4. Remote Repository β€” Your Teacher's Inbox (e.g., GitHub/GitLab)

Finally, you send the code from your safe to a remote server over the network, making it easy for others to view or collaborate.

  • Key command: git push
  • Meaning: Back up your code and share progress with your team.

Key Concepts Explained

  • git stash: You're halfway through your homework when suddenly you need to switch to another urgent task, but you don't want to commit unfinished work. In this case, you can temporarily stash your changes into a drawer (stash), and retrieve them later when you're ready to continue (pop).
  • git pull: Check whether your teacher (the remote repository) has received new homework from others, and directly sync it to your desk.
  • git fetch & merge: First check what updates exist remotely (fetch), then merge them (merge) into your code after confirming everything is fine.

Simple Summary:

Modify code β†’ add (put into basket) β†’ commit (store in safe) β†’ push (send afar).


Command Reference

1. Clone a Repository

If you want to contribute to an existing project, first clone the remote repository to your local machine:

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, you usually create a new branch:

git checkout -b new-feature

3. Working Directory

Edit code, add new files, or delete unnecessary files in your working directory.

4. Stage Files

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

git add filename
# or stage all modified files
git add .

5. Commit Changes

Commit staged changes to your local repository with a commit message:

git commit -m "Add new feature"

6. Pull Latest Changes

Before pushing your local changes, it's best to pull the latest updates 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 your 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 to invite team members for code review. Once the PR is approved and merged, your changes will be integrated into the main branch.

9. Merge Changes

After the PR is reviewed, approved, and merged, merge the remote main branch into your local branch:

git checkout main
git pull origin main
git merge new-feature

10. Delete Branch

If you no longer need the feature branch, delete it locally:

git branch -d new-feature

Or delete it from the remote repository:

git push origin --delete new-feature
← Git BranchGit Install Setup β†’