Skip to content

saikiranchalla1/learning-git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Version Control Systems

Version control is a system that helps manage changes to files and projects over time. It tracks modifications, maintains a history of changes, and allows collaboration among team members. Version control is crucial in software development and other fields where managing multiple versions and tracking changes is necessary.

  1. Why Version Control is Needed:
  • Collaboration: Version control enables multiple developers to work on the same project simultaneously and merge their changes efficiently.
  • History Tracking: Version control systems (VCS) maintain a detailed history of changes, making it easy to revert to previous versions, analyze modifications, and track who made specific changes.
  • Code Integrity: VCS ensures that changes made by different team members do not conflict, and the codebase remains stable and functional.
  • Backup and Recovery: Version control provides a reliable backup of project files, preventing data loss and facilitating recovery.
  1. Types of Version Control Systems: There are two main types of version control systems:
  • Centralized Version Control Systems (CVCS): In a CVCS, a central server stores the entire project's history, and team members check out the latest version to work on. Examples include CVS and Subversion (SVN).
  • Distributed Version Control Systems (DVCS): DVCS allows each user to have a complete copy of the repository, including the entire history. Users can work offline, commit changes locally, and synchronize with others later. Git and Mercurial are examples of DVCS.
  1. Why Git is Better: Git has gained immense popularity and is widely considered the leading VCS. Here are some reasons why Git is superior to other version control systems:
  • Distributed Architecture: Git's distributed nature allows for fast and efficient operations, even with large codebases and remote repositories. Each user has a complete copy of the repository, enabling offline work and easy collaboration.
  • Performance: Git's design optimizes performance, making operations such as commits, branching, and merging fast and lightweight.
  • Branching and Merging: Git provides powerful and flexible branching and merging capabilities, enabling efficient parallel development, feature isolation, and easy integration of changes.
  • Integrity and Security: Git ensures the integrity of project data through a content-addressable storage model, where each file and commit is identified by a unique hash. Additionally, Git supports cryptographic methods for secure communication and authentication.
  • Rich Command Set: Git offers a comprehensive set of commands for version control, allowing fine-grained control over project history and facilitating advanced workflows.
  • Ecosystem and Community: Git has a vast ecosystem of tools, extensions, and integrations, along with a large and active community that contributes to its development and provides support.

Basic Git Concepts

Installing Git on macOS

  • Open a web browser and go to the official Git website: https://git-scm.com.
  • On the homepage, click on the "Download" button.
  • Once the download is complete, locate the downloaded file (typically a .dmg file) and double-click on it.

Follow the prompts in the installation wizard to install Git:

  • Review the license agreement and click "Continue" to proceed.
  • Choose the installation location for Git and click "Continue".
  • Select the components to install (leave the default options selected) and click "Continue".
  • Choose the desired terminal emulator for Git (e.g., "Use Git from the Command Line and also from 3rd-party software") and click "Continue".
  • Review the installation summary and click "Install" to begin the installation process.
  • Enter your macOS user password if prompted and click "Install Software".
  • Wait for the installation to complete.
  • Once the installation is finished, click "Close" to exit the installation wizard.
  • To verify that Git is installed correctly, open a terminal and run the command: git --version.

You should see the Git version number displayed in the terminal, indicating a successful installation.

Installing Git on Windows

  • Open a web browser and go to the official Git website: https://git-scm.com.
  • On the homepage, click on the "Download" button.
  • Once the download is complete, locate the downloaded file (typically an .exe file) and double-click on it.

Follow the prompts in the installation wizard to install Git:

  • Review the license agreement and click "Next" to proceed.
  • Choose the installation location for Git and click "Next".
  • Select the components to install (leave the default options selected) and click "Next".
  • Choose the desired editor for Git (e.g., "Use Vim (the ubiquitous text editor) as Git's default editor") and click "Next".
  • Select the desired option for adjusting your PATH environment (e.g., "Use Git from the Windows Command Prompt") and click "Next".
  • Choose the desired terminal emulator for Git (e.g., "Use the OpenSSL library") and click "Next".
  • Choose the line-ending conversions (e.g., "Checkout Windows-style, commit Unix-style line endings") and click "Next".
  • Choose the terminal emulator used by Git Bash (e.g., "Use MinTTY") and click "Next".
  • Review the installation summary and click "Install" to begin the installation process.
  • Wait for the installation to complete.
  • Once the installation is finished, click "Finish" to exit the installation wizard.
  • To verify that Git is installed correctly, open a command prompt or Git Bash and run the command: git --version.

You should see the Git version number displayed in the terminal, indicating a successful installation.

Configuring Git

After installing Git, it is essential to configure it with your personal information before you start using it. This configuration ensures that the correct user information is associated with your commits. Here are the steps to configure Git in your local environment:

  • Open a terminal or command prompt.
  • Set your username by entering the following command, replacing with your desired username:
git config --global user.name "<your-username>"

  • Set your email address by entering the following command, replacing with your email address:
git config --global user.email "<your-email>"

  • (Optional) Set your preferred text editor for Git commit messages. By default, Git uses the system's default editor. You can set a specific editor by entering the following command, replacing with the command to launch your desired text editor (e.g., "vim" or "nano"):
git config --global core.editor "<your-editor>"
  • (Optional) Enable Git to automatically convert line endings. This setting is helpful when collaborating with users on different operating systems. To enable automatic line ending conversion, enter the following command:
git config --global core.autocrlf true
  • (Optional) View your Git configuration to verify the settings you've applied:
git config --list

Initializing a Git Repository

  • Open a terminal.
  • Navigate to the project folder using cd git-demo.
  • Run git init to initialize a new Git repository: git init.

Staging and Committing Changes

  • Check the current status of the repository: git status.

Stage changes for commit:

  • To stage a specific file, use: git add (e.g., git add Program.cs).
  • To stage all changes, use: git add ..
  • Commit the staged changes: git commit -m "Commit message" (e.g., git commit -m "Initial commit").

Branching and Merging

  • List all branches: git branch.
  • Create a new branch: git branch (e.g., git branch feature).
  • Switch to the new branch: git checkout (e.g., git checkout feature).
  • Make changes and commit them in the new branch.
  • Switch back to the main branch: git checkout main.
  • Merge the new branch into the main branch: git merge (e.g., git merge feature).

Viewing History

  • View the commit history: git log.
  • To see a simplified, one-line version of the log: git log --oneline.
  • View a specific commit and its changes: git show .

Working with Remote Repositories

Intermediate Git Concepts

Git Worktree

  • Create a new worktree: git worktree add (e.g., git worktree add ../worktree-feature feature).
  • Switch to the new worktree directory: cd .
  • Make changes and commit them in the new worktree.
  • Switch back to the original working tree: cd .
  • View the list of linked worktrees: git worktree list.
  • Remove a worktree: git worktree remove .

Fetch and Download

  • Download the latest changes from the remote repository: git fetch.
  • See the remote branches: git branch -r.
  • Merge the fetched changes into your local branch: git merge origin/.

Git LFS (Large File Storage)

  • Install Git LFS following the official documentation for Mac.
  • Initialize LFS in your repository: git lfs install.
  • Track large files: git lfs track "" (e.g., git lfs track "*.psd").
  • Stage and commit changes as usual.

Rebase and Squash

  • Rebase your current branch onto another branch: git rebase (e.g., git rebase main).
  • Squash commits during the rebase process:
  • Interactively: git rebase -i HEAD~ (e.g., git rebase -i HEAD~3).
  • Replace "pick" with "squash" or "s" in the editor for the commits you want to squash.
  • Save and exit the editor.
  • Adjust commit messages if needed.
  • Continue the rebase: git rebase --continue.

Git Stash

The git stash command allows you to temporarily save changes without committing them.

  • Make some changes to your working tree.
  • Run git stash to save the changes: git stash.
  • Switch branches or perform other operations.
  • Apply the stashed changes:
  • To apply the most recent stash: git stash apply.
  • To apply a specific stash: git stash apply stash@{} (e.g., git stash apply stash@{0}).
  • Optionally, remove the applied stash: git stash drop.
  • To list all stashes: git stash list.

Git Hooks

Git hooks are scripts that run at certain points during Git's execution.

  • Navigate to the Git repository's root folder.
  • Enter the .git/hooks directory: cd .git/hooks.
  • Create or modify the desired hook file (e.g., pre-commit, post-commit, etc.) using your preferred text editor.
  • Write the necessary code or commands within the hook file.
  • Make the hook executable: chmod +x (e.g., chmod +x pre-commit).
  • Test the hook by performing the corresponding action (e.g., making a commit).
  • Git will execute the hook script accordingly.

Git Submodule

Git submodules allow you to include other Git repositories within your main repository.

  • Create a new Git repository for the submodule.
  • Initialize the submodule within your main repository:
  • Add the submodule: git submodule add (e.g., git submodule add https://github.com/user/repo.git submodules/repo).
  • Stage and commit the changes.
  • To clone a repository with submodules, use git clone --recurse-submodules .
  • To update submodules within your repository:
  • Initialize the submodule (if not done already): git submodule init.
  • Update the submodule to the latest commit: git submodule update.

Git Branching Strategies

In Git, branching is a powerful feature that allows for parallel development, isolating new features or bug fixes, and managing project workflows. There are several branching strategies commonly used in Git. Here are some of the most popular strategies along with their advantages and disadvantages:

Git Flow:

Advantages:

  • Well-defined and structured branching model.
  • Clear separation of features, releases, and hotfixes.
  • Supports long-term maintenance and parallel development.

Disadvantages:

  • Can be complex and may not be suitable for smaller projects or rapid iterations.
  • Requires discipline and adherence to the workflow.
  • Can result in a large number of branches over time.

Feature Branching:

Advantages:

  • Each feature or task is developed in its own branch, providing isolation and easy tracking.
  • Enables parallel development by allowing multiple team members to work on separate features simultaneously.
  • Easy to integrate completed features back into the main branch.

Disadvantages:

  • Can result in a large number of branches, requiring proper naming and management.
  • Potential conflicts when merging feature branches with the main branch.
  • May require coordination to ensure proper integration and testing.

Trunk-Based Development:

Advantages:

  • Promotes simplicity and a fast-paced development workflow.
  • Encourages small, frequent commits and continuous integration.
  • Minimizes the number of long-lived branches, reducing complexity.

Disadvantages:

  • Limited isolation of features, which may require careful coordination and testing.
  • Risk of conflicts due to simultaneous development in the same branch.
  • May not be suitable for complex projects or teams with a large number of contributors.

GitLab Flow:

Advantages:

  • Simple and lightweight branching model.
  • Encourages collaboration and continuous integration.
  • Supports rapid iteration and deployment cycles.

Disadvantages:

  • Less suitable for long-term maintenance and parallel development.
  • May require additional tooling or automation for managing deployments and environments.
  • Limited isolation of features, which may require coordination and testing.

GitHub Flow:

Advantages:

  • Simple and lightweight branching model.
  • Streamlined workflow for small to medium-sized projects.
  • Emphasizes continuous integration and frequent deployments.

Disadvantages:

  • May not be suitable for complex projects with longer release cycles.
  • Limited support for parallel development and long-term maintenance.
  • Requires proper communication and coordination among team members.

It's important to note that these strategies are not mutually exclusive, and you can adapt and combine them to fit the specific needs of your project and team. Choose a branching strategy that aligns with your development processes, team size, and project requirements. Additionally, regularly review and refine your branching strategy as your project evolves and new requirements arise.

Additional Tutorials

Branching

Creating a Branch:

  • Open a terminal or Git Bash.
  • Navigate to the repository's directory using the cd command:
cd /path/to/repository
  • Create a new branch using the git branch command:

git branch <branch-name>

  • Switch to the newly created branch using the git checkout command:
git checkout <branch-name>

Alternatively, you can create and switch to a new branch in a single step by using the -b flag with the git checkout command:

git checkout -b <branch-name>

Renaming a Branch:

Ensure you are on a different branch than the one you want to rename (switch branches if necessary).

Rename the branch using the git branch -m command:

git branch -m <new-branch-name>

Switching Branches:

List all available branches and identify the branch you want to switch to:

git branch --list

Switch to the desired branch using the git checkout command:

git checkout <branch-name>

Checking Remote Branches:

List all remote branches using the git branch -r command:

git branch -r

Deleting a Local Branch:

Ensure you are on a different branch than the one you want to delete (switch branches if necessary).

Delete the local branch using the git branch -d command:

git branch -d <branch-name>

If the branch has not been merged and you want to force the deletion, use the -D flag instead:

git branch -D <branch-name>

Deleting a Remote Branch:

Delete a remote branch using the git push command with the --delete flag:

git push origin --delete <branch-name>

Viewing Git Branches:

List all branches (both local and remote) using the git branch -a command:

git branch -a

Merging a Git Branch:

Ensure you are on the branch you want to merge changes into (typically the main branch).

Merge the specified branch into the current branch using the git merge command:

git merge <branch-name>

Getting an Upstream Branch and Pulling Remote Changes:

Add a remote repository as an upstream reference to your local repository using the git remote add command:

git remote add upstream <remote-repository-url>

Fetch the upstream repository's branches and commits to update your local repository:

git fetch upstream

Switch to the branch you want to update with the latest changes from the upstream repository:

git checkout <branch-name>

Merge the changes from the upstream branch into your local branch:

git merge upstream/<branch-name>

If conflicts occur during the merge, resolve them manually and commit the changes.

Push the updated branch to your remote repository, if desired:

git push origin <branch-name>

Git Rebase vs Merge

Suppose you are working on a project with a main branch (main) and a feature branch (feature). Here's how git rebase and git merge can be used in different situations:

Scenario 1: Incorporating Changes from main into feature

Start by switching to the feature branch:

git checkout feature

Fetch the latest changes from the remote repository:

git fetch origin

If there are new commits in the main branch, rebase the feature branch on top of main using git rebase:

git rebase origin/main

This command applies the changes from main onto the feature branch, making it up to date with the latest commits in main. It rewrites the commit history of the feature branch.

Note: Use git pull origin main as an alternative to git fetch and git rebase in one step.

If there are conflicts during the rebase, resolve them by editing the conflicting files, then stage the changes using git add and continue the rebase using git rebase --continue.

Alternatively, if you encounter difficulties during the rebase and want to abort it, you can use git rebase --abort to return to the original state of the branch.

Once the rebase is complete, the feature branch will include the changes from main. You can push the updated branch to the remote repository:

git push origin feature

This ensures that your branch includes the latest changes from main and can be easily merged in the future.

Scenario 2: Merging feature into main using git merge

Switch to the main branch:

git checkout main

Ensure the main branch is up to date with the latest changes from the remote repository:

git fetch origin

Merge the feature branch into main using git merge:

git merge feature

This command merges the changes from feature into main, creating a new merge commit.

If there are conflicts during the merge, resolve them by editing the conflicting files, then stage the changes using git add and continue the merge using git merge --continue.

Alternatively, if you encounter difficulties during the merge and want to abort it, you can use git merge --abort to return to the original state of the branch.

Once the merge is complete, the changes from feature are incorporated into main. You can push the updated main branch to the remote repository:

git push origin main

This ensures that the changes are available to others working on the project.

Choosing Between git rebase and git merge:

  • Use git rebase when you want to incorporate the latest changes from one branch into another, maintaining a linear commit history. It is useful for keeping feature branches up to date with the latest changes in the main branch.
  • Use git merge when you want to combine the changes from one branch into another, creating a new merge commit. It is commonly used when integrating feature branches into the main branch.
  • git rebase can result in a cleaner, linear commit history, but it should not be used if the branch has been pushed to a remote repository and shared with others.
  • git merge preserves the original commit history but creates a merge commit, which can be helpful for tracking merge points and maintaining an accurate project history.

Remember to consider the collaboration workflow, branch status, and commit history when deciding between git rebase and git merge. Both commands have their strengths and appropriate use cases in different scenarios.

Git Tags

What are Git Tags?

Git tags are references to specific points in Git history, commonly used to mark important milestones, releases, or versions of a project. Unlike branches, tags are typically used for marking specific commits as significant and are not meant to be updated or changed over time.

Creating a Git Tag:

Open a terminal or Git Bash.

Navigate to the repository's directory using the cd command:

cd /path/to/repository

Check the available branches and make sure you are on the desired commit:

git log --oneline

Create an annotated tag using the git tag command. An annotated tag includes additional information such as the tagger's name, email, date, and a message describing the tag:

git tag -a <tag-name> -m "<tag-message>"

For example:

git tag -a v1.0 -m "Release version 1.0"

An alternative is to create a lightweight tag without the additional information:

git tag <tag-name>

Viewing Git Tags:

List all tags in your repository using the git tag command:

git tag

View the details of a specific tag, including the associated commit and tag message, using the git show command:

git show <tag-name>

Pushing Tags to a Remote Repository:

Push a single tag to the remote repository using the git push command:

git push origin <tag-name>

Push all tags to the remote repository using the git push command with the --tags flag:

git push origin --tags

This pushes all local tags to the remote repository.

Deleting a Git Tag:

Delete a local tag using the git tag command with the -d option:

git tag -d <tag-name>

Delete a remote tag using the git push command with the --delete flag:

git push origin --delete <tag-name>

Checking Out a Specific Tag:

Switch to a specific tag by creating a new branch at that tag:

git checkout -b <branch-name> <tag-name>

This creates a new branch from the specified tag.

You can now make changes on the newly created branch or view the code as it existed at the tagged commit.

Tagging Releases and Versions:

  • It is common to use tags to mark releases or versions of a project. You can create tags like v1.0, v2.0, or release-1.2, depending on your preferred naming convention.
  • Consider tagging important milestones, significant commits, or stable versions of your project to provide a clear history and reference points.
  • Annotated tags with informative messages are especially useful for documenting release notes or important details about the tag.

Git tags are a powerful way to mark specific points in your Git history and provide meaningful references to important milestones or releases. They help create a clear history and aid in navigating through the project's development.

Git Revert:

git revert is used to create a new commit that undoes the changes made in a previous commit. It allows you to safely undo changes without modifying the commit history.

  • Open a terminal or Git Bash.
  • Navigate to the repository's directory using the cd command:
cd /path/to/repository

Identify the commit you want to revert. You can use the git log command to view the commit history and find the commit hash.

Execute the git revert command followed by the commit hash:

git revert <commit-hash>

This creates a new commit that undoes the changes made in the specified commit.

Save and exit the commit message in the text editor that opens. It describes the revert operation.

The revert commit is now created, effectively undoing the changes made in the specified commit. You can use git log to verify the commit history.

Git Reset:

git reset allows you to reset the current branch to a specific commit, modifying the commit history. It has different modes that affect how the reset operation behaves.

  • Open a terminal or Git Bash.
  • Navigate to the repository's directory using the cd command:
cd /path/to/repository

Determine the commit you want to reset to. You can use the git log command to find the commit hash or other references.

Choose the appropriate mode for the reset operation:

  • Soft Reset:
git reset --soft <commit-hash>

This moves the branch pointer to the specified commit while keeping the changes in the staging area and working directory. The changes from the specified commit are "uncommitted" and can be modified before creating a new commit.

  • Mixed Reset (default):
git reset --mixed <commit-hash>

This moves the branch pointer to the specified commit and resets the staging area, but keeps the changes in the working directory. The changes from the specified commit are "unstaged" and can be modified before staging and committing.

  • Hard Reset:
git reset --hard <commit-hash>

This moves the branch pointer to the specified commit and discards all changes in the staging area and working directory. The changes from the specified commit are permanently removed.

Confirm the reset operation when prompted. Make sure you have a backup of any important changes before performing a hard reset.

The branch is now reset to the specified commit, modifying the commit history accordingly.

.gitignore:

.gitignore is a file that specifies patterns to exclude files and directories from being tracked by Git. It is used to prevent sensitive or unnecessary files from being committed.

Open a text editor.

Create a new file named .gitignore in the root directory of your repository.

Specify the files, directories, or patterns that you want Git to ignore, each on a new line.

For example:

# Ignore build output
bin/
obj/

# Ignore files with specific extensions
*.log
*.tmp

# Ignore a specific file
config.ini

The # character is used for comments.

Save the .gitignore file.

Git will now ignore the specified files and directories when staging and committing changes.

Note: If you want to ignore a file or directory that is already tracked by Git, you may need to remove it from the repository using git rm --cached before adding it to the .gitignore file.

Using git revert, git reset, and .gitignore provides powerful functionality in managing your Git repository:

git revert allows you to safely undo changes made in a specific commit by creating a new commit that undoes those changes.

git reset enables you to reset the branch to a specific commit, modifying the commit history. It provides different modes for different purposes, such as preserving changes or discarding them.

.gitignore helps in specifying patterns to exclude files and directories from being tracked by Git, ensuring they are not accidentally committed.

By understanding and utilizing these features, you can effectively manage and control the state and history of your Git repository.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published