Git is a version control system used to manage changes to files, typically in code or design projects.
What is Version Control?
Version control refers to managing changes made to files over time. It allows us to:
- Track modifications,
- Revert to previous versions,
- Collaborate effectively.
Why do we use Git?
Git helps us:
- Preserve changes,
- Track modifications across one or multiple files,
- Save progress in projects, whether code or design.
While Git can be used for personal notes or academic purposes, it's mainly used for team collaboration. Simply put, Git is "a tool that helps me organize with my team and track the work we do."
Remember, Git operates within a directory, so even if you want to track a single file, Git must be initialized in the directory containing that file.
Useful Commands for Team Collaboration
# Initialize a Git repository in a directory
git init
# Update all remote branches
git fetch --all
# List local branches
git branch
# List both local and remote branches
git branch -a
# Add a remote repository
git remote add origin https://gitlab.com/hijuliancode/demo-git-tutorial
# Add a Heroku remote repository
git remote add heroku https://git.heroku.com/demo-heroku-repo.git
# Remove a remote repository
git remote remove heroku
# View all configured remote repositories
git remote -v
# Fetch and merge changes from the master branch of the remote repository
git pull origin master
# Push changes from the local master branch to the remote repository
git push origin master
# Display the state of your working directory and staging area
git status
# Stage a file for commit
git add filename.html
# Stage all changes in the current directory for commit
git add .
# Remove a file from the staging area
git reset filename.html
# Remove all changes from the staging area
git reset .
# Rename a file
git mv oldname.js newname.js
# Rename a file within a directory
git mv folder/oldname.js folder/newname.js
# Temporarily save changes and revert to the last commit
git stash
# Restore changes from the stash
git stash apply
# Create a commit with a descriptive message
git commit -m "Message"
Example Workflow
Imagine we’re building a house as a project. Here’s how a workflow might look with three team members: Person A, Person B, and Person C.
-
Person A initializes the repository:
git init git remote add origin git@gitlab.com:hijuliancode/demo-git-tutorial.git
-
Person A creates an initial file and pushes it to the repository:
echo "## demo-git-tutorial" >> README.md git add README.md git commit -m "initial commit" git branch -M master git push origin master
-
Person B and Person C clone the repository:
git clone git@gitlab.com:hijuliancode/demo-git-tutorial.git cd demo-git-tutorial
-
Person A creates the
develop
branch and pushes it to the remote:
git checkout -b develop git push origin develop
-
Person B and Person C update their local repositories to include the
develop
branch:
git fetch --all git checkout develop
Now everyone has the repository and can start contributing.
-
Person A creates a feature branch to work on the house plans:
git checkout -b feature/house-plans touch house-plans.js git add house-plans.js git commit -m "Add house plans" git push origin feature/house-plans
-
Person B and Person C update their local repositories to access the new branch:
git fetch --all git branch -a git checkout feature/house-plans
-
Person B wants to suggest improvements to the house plans. They can do so by creating a Pull Request (or Merge Request):
- Navigate to the repository's GitHub or GitLab page,
- Go to the Pull Request or Merge Request section,
- Assign a reviewer (like a team lead or peer).
To apply the suggested changes:
git add house-plans.js
git commit -m "Improve house plans"
git push origin feature/house-plans
-
Once the feature branch is reviewed and merged, it can be deleted:
git checkout develop git pull origin develop git branch -D feature/house-plans
Repeat this process for new features or changes!
Top comments (0)