What is Git?
Git is a distributed version control system that allows multiple developers to work on the same project simultaneously. It keeps track of changes made to the codebase, enables collaboration and provides a way to revert changes if needed.
Installing Git
To use Git, you need to first install it on your local machine. You can download the installer from the Git website (https://git-scm.com/downloads) and follow the installation instructions.
Basic Git commands
Here are some basic Git commands that you will use frequently:
• git init
initializes a new Git repository
• git add
adds files to the staging area
• git commit
saves changes to the repository
• git push
pushes changes to a remote repository
• git pull
fetches changes from a remote repository and merges them into the local repository
Creating a new repository
To create a new Git repository, you can use the following steps:
• Create a new directory on your local machine: mkdir myproject
• Change into the directory: cd myproject
• Initialize a new Git repository: git init
• Add your files to the staging area: git add
.
• Commit your changes: git commit -m "Initial commit"
Cloning an existing repository
To clone an existing Git repository, you can use the following command:
git clone <repository-url>
For example, to clone the Git repository for the Bootstrap framework, you can use the following command:
git clone https://github.com/twbs/bootstrap.git
Branching and merging
Git allows you to create branches, which are separate copies of the codebase that can be developed independently.
To create a new branch, you can use the following command:
git branch <branch-name>
To switch to a different branch, you can use the following command:
git checkout <branch-name>
Once you have made changes to a branch, you can merge it back into the main branch using the following command:
git merge <branch-name>
Resolving merge conflicts Sometimes, when you merge two branches, Git may detect conflicts between the changes made to the codebase. To resolve these conflicts, you need to manually edit the affected files and then commit the changes.
Here's an example of how to resolve a merge conflict:
• Merge the branch into the main branch: git merge <branch-name>
• Git will detect a conflict and ask you to resolve it manually.
• Edit the affected files to resolve the conflict.
• Add the resolved files to the staging area: git add
.
• Commit the changes: git commit -m "Resolved merge conflict"
Working with remote repositories Git allows you to work with remote repositories, which are repositories hosted on a remote server (such as GitHub).
To push changes to a remote repository, you can use the following command:
git push <remote-name> <branch-name>
For example, to push changes to the main branch of a remote repository named "origin", you can use the following command:
git push origin main
To fetch changes from a remote repository, you can use the following command:
git fetch <remote-name>
To merge the fetched changes into your local repository, you can use the following command:
git merge <remote-name>/<branch-name>
For example, to fetch changes from a remote repository named "origin" and merge them into the main branch of your local repository, you can use the following command:
git fetch origin
git merge origin/main
Top comments (0)