This guide is designed to help you practice and understand the various commands and practices involved in version control using Git.
To start using Git in your project, initialize a new Git repository with the following command:
git init
Use the Git CLI to stage changes before committing them:
git add <path>
See what changes are staged using:
git status
Commit your changes with a descriptive message:
git commit -m "<message>"
Explore your repository's history with:
git log
Create and manage branches with Git:
- Create a new branch:
git branch <branch-name>
- Check out a branch:
git checkout <branch-name>
- Create and check out a new branch:
git checkout -b <branch-name>
Merge changes from one branch to another:
git merge <branch-name>
Keep your branch up-to-date with the main branch:
# Switch to the main branch
git checkout main
# Pull changes from the remote repository
git pull
# Switch back to your branch
git checkout <branch-name>
# Merge changes from main to your branch
git merge main
Go back to a previous version of your project using:
# View commit history
git log
# Checkout a specific commit
git checkout <commit-hash>
Temporarily save and retrieve uncommitted changes with:
# Stash changes
git stash
# Retrieve stashed changes
git stash pop
Manage remote repositories with Git:
# Display existing remote URLs
git remote -v
# Add a new remote repository URL
git remote add <shortname> <remote-url>
# Update a remote repository URL
git remote set-url <existing-shortname> <new-remote-url>
# Rename a remote repository URL
git remote rename <old-shortname> <new-shortname>
# Remove a remote repository URL
git remote rm <existing-shortname>
Use a .gitignore
file to specify files you don't want included in your Git
history.
Enhance your Git experience in VSCode:
- Utilize the Git Graph extension to visualize your repo's commit history.
- Leverage the Git Lens extension to investigate your repository's commit history.
- Understand how the file tree in VSCode highlights files with uncommitted changes.
Save your development progress using small commits with clear and helpful messages.
Organize your development process by creating feature branches. Each branch can correspond to a specific part of your project, and changes can be merged into the main branch when completed.