Git is an essential tool for version control that helps developers manage and track code changes effectively. This blog will guide you through the initial setup of Git, how to create a new repository, push changes, merge branches, and use Git tags. Let’s get started!
1. First-Time Setup on Git
To configure Git for the first time, you need to set your user name and email address. These details will be associated with your commits. Follow these commands:
git config --global user.name "Kishan"
git config --global user.email "kishanradadiya@gmail.com"
To verify your configuration, run:
git config --list
2. Creating and Setting Up a New Repository
Step 1: Initialize a New Repository
Navigate to your project folder and run:
git init
Step 2: Add Remote Origin
To connect your local repository to a remote Git repository, use:
git remote add origin <remote-branch-url>
Step 3: Fetch and Checkout Branches
To fetch updates from the remote repository and switch to a specific branch:
git fetch
git checkout <branch-name>
To check the current branch, use:
git branch
3. Create a New Repository on Git Lab
To create a new repository:
- Clone the repository from GitLab:
git clone https://github.com/Kishan41290/Image-Tagging.git
2. Navigate to the project directory:
cd test
3. Create a README file, add it, and commit:
touch README.md
git add README.md
git commit -m "add README"
4. Push the changes to the master branch:
git push -u origin master
4. Push an Existing Folder to Git
Step 1: Initialize Git
Navigate to your existing project folder:
cd existing_folder
git init
Step 2: Add Remote Origin and Push Changes
git remote add origin https://github.com/Kishan41290/Image-Tagging.git
git add .
git commit -m "Initial commit"
git push -u origin master
5. Push an Existing Git Repository to a New Remote
- Rename the current origin:
git remote rename origin old-origin
2. Add a new origin:
git remote add origin https://github.com/Kishan41290/Image-Tagging.git
3. Push all branches and tags:
git push -u origin --all
git push -u origin --tags
6. Resolving “git pull not merge or not push/pull” Error
If you encounter an error while pulling changes, use:
git pull --allow-unrelated-histories
7. Pushing Changes to Any Branch
To push your changes to a branch:
- Add your changes:
git add .
2. Commit the changes:
git commit -m "Initial"
3. Push to the remote branch:
git push
8. Checkout and Fetch Branches
To switch to another branch:
git checkout
To fetch a new branch and switch:
git fetch
git checkout <branch-name>
git branch
9. Merging Two Branches
To merge two branches:
- Switch to the target branch:
git checkout test
2. Pull the latest changes:
git pull
3. Merge the desired branch:
git merge WEB-1
4. Push the merged changes:
git push
10. Pulling Changes from a Specific Branch
To pull updates from a particular branch:
git pull origin master
11. Git Tagging
Tags help mark specific points in the repository’s history, such as release points.
Create Lightweight Tag
git checkout <branch-name>
git tag v1.0
List All Tags
git tag
Create an Annotated Tag
git tag -a v1.1 -m "Release version 1.1"
Show Changes of a Tag
git show v1.0
List Tags Starting with a Specific Pattern
git tag -l "v1.*"
Push Tags to Remote
Push a specific tag:
git push origin v1.0
Push all tags:
git push --tags
Delete Tags
To delete tags locally:
git tag -d v1.0
To delete multiple tags:
git tag -d v1.0 v1.1
Delete tags from the remote:
git push origin -d v1.0 v1.1
Tag a Past Commit
git tag v1.0 <commit-ref>
Conclusion
This blog covered the essential commands for setting up Git, managing repositories, pushing changes, merging branches, and using tags. With these basics, you should be well-equipped to manage your projects effectively using Git.