What is Git?
Git is a free and open-source version control system, created by Linus Torvalds in 2005. It allows multiple people to work on a project simultaneously without overwriting each other’s changes, maintaining a history of revisions.
For a straightforward guide, check out this no-sh*t guide.
How Git Works
Here’s a basic overview of how Git operates:
- Create a repository: Initialize a project repository with a Git hosting tool (like GitHub or Bitbucket).
- Clone the repository: Copy the repository to your local machine.
- Add and commit changes: Make changes to your local files and commit these changes to the local repository.
- Push changes: Upload your changes to the main branch on the remote repository.
- Pull changes: Download changes made by others from the remote repository to your local machine.
- Create a branch: Create a separate version of your project to work on new features.
- Open a pull request: Propose your changes to be merged into the main branch.
- Merge branches: Combine your changes with the main branch after approval.
Basic Git Commands
Creating a Repository
To initialize a new repository, navigate to your project directory and run:
git init
Adding and Committing Changes
Add specific files to the staging area:
git add <filename>
Or add all changes:
git add *
Commit your changes with a message:
git commit -m "Commit message"
Pushing to a Remote Repository
Add a remote repository URL (only needed once):
git remote add origin <server>
Push your changes to the master branch:
git push origin master
Pulling Changes
Download changes from the remote repository to your local machine:
git pull origin master
Creating and Managing Branches
Create a new branch:
git checkout -b <name-of-the-branch>
Switch to an existing branch:
git checkout <name-of-the-branch-you-want-to-go-to>
Push a new branch to the remote repository:
git push -u origin <name-of-the-branch-that-is-not-yet-in-the-server>
Understanding these basic commands and workflows will help you get started with Git and improve your collaboration and project management skills. Happy coding!