Git is a distributed version control system used for tracking changes in source code. Below are the fundamental Git commands to help you get started.
Set up your Git environment:
# Set username
git config --global user.name "Your Name"
# Set email
git config --global user.email "[email protected]"
# Check configuration
git config --list
Create a new Git repository:
git init
Clone an existing repository:
git clone <repository_url>
git status
git add <file_name> # Add a specific file
git add . # Add all files
git commit -m "Your commit message"
git push origin <branch_name>
git branch <branch_name>
git checkout <branch_name>
OR
git switch <branch_name>
git checkout -b <branch_name>
OR
git switch -c <branch_name>
git branch
git branch -d <branch_name>
git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them manually. After resolving conflicts:
git add .
git commit -m "Resolved merge conflict"
Fetch and merge changes from a remote repository:
git pull origin <branch_name>
Once a feature is merged, delete the branch to keep the repository clean:
git branch -d <branch_name>
If the branch has been pushed to the remote repository, delete it remotely as well:
git push origin --delete <branch_name>
These are the fundamental Git commands that will help you work efficiently with version control. Always make sure to follow best practices like using meaningful commit messages and keeping your branches organized.