Git Basics

Introduction to Git

Git is a distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency. Created by Linus Torvalds in 2005, Git allows multiple developers to work on a p…


This content originally appeared on DEV Community and was authored by Harsh Mishra

Introduction to Git

Git is a distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency. Created by Linus Torvalds in 2005, Git allows multiple developers to work on a project simultaneously without overwriting each other’s work. It tracks changes in the source code and helps in coordinating work among multiple developers.

Key features of Git include:

  • Distributed: Each user has a full copy of the repository on their machine, including its entire history.
  • Fast and Efficient: Git is optimized for performance. Operations like commits, branching, and merging are lightweight.
  • Branching and Merging: Git supports powerful branching models which enable parallel development, feature testing, and bug fixing.
  • Data Integrity: All data in Git is secured with cryptographic hash functions.

Git Lifecycle

The Git lifecycle represents how code moves through different stages. The main stages are:

  1. Untracked/Unmodified: A new file added to the project or an existing file that hasn't changed since the last commit.
  2. Modified: When you make changes to a file, it enters the modified state.
  3. Staged: Once you’ve made changes to a file and you want to include those changes in the next commit, you stage the file.
  4. Committed: A commit captures the current state of your staged changes. After the commit, the files are in the committed state, ready to be pushed to a remote repository.

Git Lifecycle Steps

  1. Working Directory: This is your local filesystem where you make changes (e.g., editing files, adding new ones).
  2. Staging Area (Index): This is a file in your Git directory that stores information about what will go into your next commit.
  3. Git Repository: A directory where Git stores all your commits, configurations, and metadata.

Git Workflow

Git offers a flexible workflow. Here’s a basic example of a standard Git workflow:

  1. Clone a repository: Developers clone a remote repository using git clone.
  2. Create a new branch: Developers create a branch for the feature or fix they are working on using git branch <branch_name> and switch to it using git checkout <branch_name>.
  3. Make changes: Code changes are made locally in the working directory.
  4. Stage changes: Developers stage the changes they want to include in the next commit using git add <file>.
  5. Commit changes: After staging, the changes are committed using git commit -m "message".
  6. Pull latest changes: Before pushing the code, it's good practice to pull the latest changes from the remote repository using git pull origin <branch>.
  7. Push changes: Once the code is committed, it's pushed to the remote repository using git push origin <branch>.
  8. Merge changes: After review, the changes are merged into the main branch (e.g., master or main) using git merge <branch>.

Here’s a structured overview of common Git commands, specifically focusing on the Setup section and including details for initializing and cloning repositories:

Common Git Commands

SETUP

Configuring User Information Used Across All Local Repositories

  • Set User Name:
  git config --global user.name “[firstname lastname]”

Sets a name that is identifiable for credit when reviewing version history.

  • Set User Email:
  git config --global user.email “[valid-email]”

Associates your email address with commits for identification purposes.

SETUP & INIT

Initializing and Cloning Repositories

  • Initialize a Git Repository:
  git init

Initializes an existing directory as a Git repository, creating a new .git subdirectory.

  • Clone a Repository:
  git clone [url]

Creates a copy of the remote repository at the specified URL, including all its history.

STAGE & SNAPSHOT

Working with Snapshots and the Git Staging Area

  • Check Status:
  git status

Displays modified files in the working directory, indicating which files are staged for the next commit.

  • Stage a File:
  git add [file]

Adds a file as it appears now to your next commit (stages it).

  • Unstage a File:
  git reset [file]

Removes a file from the staging area while retaining changes in the working directory.

  • Show Unstaged Changes:
  git diff

Displays differences between the working directory and the staging area for unstaged files.

  • Show Staged Changes:
  git diff --staged

Displays differences between the staging area and the last commit for staged files.

  • Commit Changes:
  git commit -m “[descriptive message]”

Commits your staged content as a new snapshot, with an associated message describing the changes.

BRANCH & MERGE

Isolating Work in Branches and Integrating Changes

  • List Branches:
  git branch

Lists your branches, marking the currently active branch with an asterisk ().*

  • Create a New Branch:
  git branch [branch-name]

Creates a new branch at the current commit.

  • Switch Branches:
  git checkout [branch-name]

Switches to another branch and checks it out into your working directory.

  • Merge Branches:
  git merge [branch]

Merges the specified branch’s history into the current branch.

  • Show Commit History:
  git log

Displays all commits in the current branch’s history.

TRACKING PATH CHANGES

Versioning File Removals and Path Changes

  • Remove a File:
  git rm [file]

Deletes the specified file from the project and stages the removal for commit.

  • Move a File:
  git mv [existing-path] [new-path]

Changes an existing file's path and stages the move for commit.

SHARE & UPDATE

Retrieving Updates and Updating Local Repositories

  • Add Remote Repository:
  git remote add [alias] [url]

Adds a Git URL as an alias for a remote repository.

  • Fetch Updates:
  git fetch [alias]

Fetches all branches from the specified remote repository.

  • Merge Remote Branch:
  git merge [alias]/[branch]

Merges a remote branch into your current branch to bring it up to date.

  • Push Changes:
  git push [alias] [branch]

Transmits local branch commits to the corresponding remote branch.

  • Pull Updates:
  git pull [alias] [branch]

Fetches and merges changes from the specified remote branch into your current branch.

Working with Remote Repositories

Git supports collaboration through remote repositories. These repositories are typically hosted on services like GitHub, GitLab, or Bitbucket.

  1. Remote Add: To add a remote repository, use:
   git remote add origin https://github.com/username/repository.git
  1. Push: Once changes are committed locally, use the git push command to send them to the remote repository.
   git push origin main
  1. Fetch and Pull:
    • Fetch: Download changes from the remote repository, but don’t apply them to your local branch.
    • Pull: Fetch the changes and automatically merge them into your local branch.
   git pull origin main
  1. Fork and Pull Requests: On services like GitHub, you can fork a repository, make changes, and then create a pull request to propose your changes be merged into the original project.

Branching Strategy

A key feature of Git is its powerful support for branching. Here's a common branching strategy:

  1. Main Branch (main or master): This is the stable branch that contains the code in production.
  2. Feature Branches: Developers create branches for new features. When the feature is completed, the branch is merged into the main branch.
  3. Hotfix Branches: In case of critical issues, developers create hotfix branches to address the issue immediately.
  4. Release Branches: Some teams use release branches for final testing and preparing code for deployment.

Version Control Using Git

Git is widely used for version controlling, where each change to the project is tracked, and historical versions can be accessed easily.

  1. Commit History: Every time changes are committed, Git records metadata about the change (like the author, time, and commit message). This allows you to view the full history of the project with git log.

  2. Reverting to a Previous Version: Git enables developers to easily switch between different versions of their codebase. You can checkout previous commits or tags.

   git checkout <commit_hash>
  1. Collaborative Development: Git's distributed nature makes collaboration easy. Multiple developers can work on different branches simultaneously without worrying about overwriting each other's changes. When everyone finishes their changes, the branches can be merged together, usually after resolving any conflicts that arise.

  2. Conflict Resolution: Sometimes, changes made by two different developers conflict with each other. Git will automatically detect these conflicts and prompt developers to resolve them manually.

  3. Versioning Releases: By using Git tags, you can create snapshots of your code at specific points, such as a release. For example:

   git tag v1.0
   git push origin v1.0

Conclusion

Git is a powerful tool for version control, allowing teams to manage and collaborate on software projects effectively. Understanding the Git lifecycle, key commands, and workflow will enable you to track changes, collaborate with other developers, and ensure your project history is well-organized. Additionally, mastering branching, merging, and working with remote repositories will enhance your development process by fostering efficient collaboration and clear version control.


This content originally appeared on DEV Community and was authored by Harsh Mishra


Print Share Comment Cite Upload Translate Updates
APA

Harsh Mishra | Sciencx (2024-09-26T17:34:12+00:00) Git Basics. Retrieved from https://www.scien.cx/2024/09/26/git-basics/

MLA
" » Git Basics." Harsh Mishra | Sciencx - Thursday September 26, 2024, https://www.scien.cx/2024/09/26/git-basics/
HARVARD
Harsh Mishra | Sciencx Thursday September 26, 2024 » Git Basics., viewed ,<https://www.scien.cx/2024/09/26/git-basics/>
VANCOUVER
Harsh Mishra | Sciencx - » Git Basics. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/26/git-basics/
CHICAGO
" » Git Basics." Harsh Mishra | Sciencx - Accessed . https://www.scien.cx/2024/09/26/git-basics/
IEEE
" » Git Basics." Harsh Mishra | Sciencx [Online]. Available: https://www.scien.cx/2024/09/26/git-basics/. [Accessed: ]
rf:citation
» Git Basics | Harsh Mishra | Sciencx | https://www.scien.cx/2024/09/26/git-basics/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.