Git is a distributed version control system that allows multiple developers to collaborate on a project efficiently. It helps track changes, manage different versions of code, and revert to previous versions when needed. It is widely used in software development and essential for teamwork.
- Version Control: Keeps track of all changes in a project.
- Collaboration: Enables multiple developers to work on the same project.
- Backup and Recovery: Prevents loss of work by saving versions.
- Branching and Merging: Allows parallel development without affecting the main project.
- Efficiency: Handles large projects with ease.
Download and install Git from git-scm.com. After installation, verify by running:
git --versionBefore using Git, set up your user information:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"To check your configuration:
git config --global --listTo set configurations for a specific repository only (instead of globally):
git config user.name "Your Name"
git config user.email "your_email@example.com"To start using Git in a project, navigate to the project folder and initialize Git:
git initTo check if Git is initialized:
git statusAfter modifying files, add them to the staging area:
git add . # Adds all files
# OR
git add filename # Adds a specific fileTo check which files are staged:
git statusSave a snapshot of your code with a meaningful message:
git commit -m "Your commit message"To view commit history:
git logTo store your code online, link your local repository to a remote repository (e.g., GitHub, GitLab, Bitbucket):
git remote add origin your_repo_urlTo verify the remote repository:
git remote -vUpload your code to the remote repository:
git push -u origin mainTo push subsequent updates:
git pushBranches allow multiple features to be developed simultaneously without affecting the main code.
git branch feature-branchgit checkout feature-branchgit checkout -b feature-branchgit branchgit checkout main
git merge feature-branchgit branch -d feature-branchTo force delete a branch:
git branch -D feature-branchgit reset --soft HEAD~1git reset --hard HEAD~1git reset filenamegit checkout -- filenameWhen collaborating, it's important to keep your local repository updated.
git fetchgit pull origin main- Write Clear Commit Messages: Describe changes clearly.
- Use Branches: Keep
mainclean and stable. - Pull Before Pushing: Avoid merge conflicts by updating before pushing.
- Commit Often: Save small, meaningful changes frequently.
- Use
.gitignoreFiles: Exclude unnecessary files (e.g.,node_modules,envfiles).
Example .gitignore file for a Node.js project:
node_modules/
.env
dist/
.DS_Store
git statusgit log --oneline --graph --allgit clone repo_urlgit stashgit stash popgit diffgit show commit_hashGit is a powerful tool that enhances development workflow and collaboration. By mastering the commands above, you can efficiently manage your projects, track progress, and contribute to open-source or team-based projects seamlessly. Happy coding!