Git is a distributed version control system that allows multiple developers to work on a project simultaneously without interfering with each other's changes. It tracks changes in source code during software development, enabling collaboration and maintaining a history of changes. Git is widely used in open-source and commercial projects, and it integrates well with platforms like GitHub, GitLab, and Bitbucket.
In this guide, we will cover the basic commands, branching strategies, commit best practices, pull requests, resolving merge conflicts, and advanced Git operations. Git is a powerful tool, and understanding its workflow is essential for effective collaboration in software development.
For more information on Git, visit the official documentation at git-scm.com
These are the fundamental commands you'll use in everyday Git operations.
Branching allows parallel development without affecting the main codebase.
Writing good commit messages is crucial for understanding project history.
Fixes #123
).feat: Add user authentication endpoint
Implement the /api/auth/login route using JWT for session management.
This endpoint accepts username and password, validates credentials
against the database, and returns a JWT token upon success.
Addresses issue #45.
PRs are used to propose changes, discuss them, and integrate them into the main codebase.
git push origin [branch-name]
.main
or develop
).git branch -d [branch-name]
) and remotely (usually an option when merging).Conflicts occur when Git cannot automatically merge changes from different branches.
git merge
or git pull
if conflicts exist. git status
will show conflicted files.<<<<<<< HEAD
Your changes (current branch)
=======
Incoming changes (branch being merged)
>>>>>>> [other-branch-name]
<<<<<<<
, =======
, >>>>>>>
) and keep the desired code.git add [resolved-file-name]
for each file you fixed.git commit
. Git usually provides a default commit message; you can keep it or modify it. If you were rebasing, use git rebase --continue
instead.git pull origin main
while on your feature branch).More sophisticated Git commands for experienced users.
Git hooks are scripts that run automatically at certain points in the Git workflow (e.g., before committing, after merging).
.git/hooks
directory of your repository. Sample hooks are provided with .sample
extensions..sample
extension to activate a hook. Make sure the script is executable (chmod +x .git/hooks/[hook-name]
).pre-commit
: Runs before a commit message is entered. Used for linting, running tests, checking code style. If it exits non-zero, the commit is aborted.prepare-commit-msg
: Runs before the commit message editor is fired up but after the default message is created.commit-msg
: Runs after the commit message is entered. Used to validate the message format.post-commit
: Runs after a commit is completed. Used for notifications.pre-push
: Runs during git push
, after remote refs have been updated but before any objects have been transferred. Used for running tests before pushing.git clone
. Tools like Husky can help manage shareable hooks within a project.