First, install Git on your local machine. You can download the latest version suitable for your operating system from the official Git website. After installation, run the following command in the terminal or command prompt to check if Git is successfully installed:
git --version
To put your PHP project into a Git repository, you need to initialize it. Run the following command in your project directory:
git init
This will create a hidden directory called .git, which contains all the configuration and data for the Git repository.
To add your code to the repository, use the git add command. For example, to add the index.php file:
git add index.php
Next, use the git commit command to commit your changes and add an appropriate commit message describing the changes made:
git commit -m "Added index page"
In Git, branches are independent copies of code that allow developers to make changes without affecting the main branch. To create a new branch, run the following command:
git branch new-branch-name
To switch to a different branch, run the following command:
git checkout branch-name
Once you’ve completed work on a branch, you can merge its changes back into the main branch. To merge two branches, run the following command:
git merge branch-name
To pull changes from a remote repository, run the following command:
git pull origin master
To push your local changes to a remote repository, use the following command:
git push origin master
If conflicts occur while merging branches, you’ll need to resolve them manually. To do this, use the following command:
git mergetool
This will open a window in your text editor where you can review and make necessary changes to resolve the conflicts. After that, commit your merge to complete the merge process.
By mastering Git, you can effectively manage version control and collaboration in your PHP projects. Through initializing repositories, creating branches, committing changes, and resolving conflicts, you can keep your codebase organized and collaborate seamlessly with your team. By following best practices and leveraging Git's powerful tools, you’ll significantly enhance your development workflow.