Git is a distributed version control system that helps developers manage and track changes in their codebase efficiently.
GitHub is a web-based hosting platform for version control using Git. It provides a collaborative environment for software development and versioning.
To create a new Git repository, use the following command:
bash
git init
Before using Git, you need to set up your user details for version tracking.
bash
git config --global user.name "John"
git config --global user.email "example@gmail.com"
To verify your configuration, use:
bash
git config --list
To view the current status of your repository, including changes and tracked files:
bash
git status
bash
git add <filename>
bash
git add .
To save changes with a descriptive message:
bash
git commit -m "Your commit message"
To link your local repository to a remote GitHub repository:
bash
git remote add origin https://github.com/your-username/your-repo.git
To upload your commits to the remote repository:
bash
git push -u origin main
bash
git log
bash
git log --oneline
To switch to the main branch of your repository:
bash
git switch main
Alternatively, you can use:
bash
git checkout main
To create a new branch:
bash
git branch <branch-name>
To switch to a specific branch:
bash
git switch <branch-name>
Or use:
bash
git checkout <branch-name>
To list all the branches in your repository:
bash
git branch
Merging combines the changes from one branch into another.
To merge a branch into the currently active branch:
bash
git merge <branch-name>
Switch to the branch you want to merge changes into (e.g., main):
bash
git switch main
Or:
bash
git checkout main
Merge the branch (e.g., feature-branch) into the current branch:
bash
git merge feature-branch
If there are conflicts during the merge, Git will notify you. To resolve conflicts:
Open the conflicted files in a text editor.
Manually resolve the conflicts.
Mark the conflicts as resolved by staging the files:
bash
git add <conflicted-file>
Complete the merge with a commit:
bash
git commit
1. Initialize a repository: `git init`
2. Check repository status: `git status`
3. Stage changes: `git add` or `git add .`
4. Commit changes: `git commit -m "message"`
5. Add remote repository: `git remote add origin <URL>`
6. Push changes: `git push -u origin main`
7. Create and switch branches: `git branch <branch-name>` and `git switch <branch-name>`
8. Merge branches: `git merge <branch-name>`
---