Git Q&A

·

6 min read

Similar to Git

Distributed Version Control System

  • Mercurial

  • Fossil

  • Bazaar

  • Darcs

Centralized Version Control System

  • Subversion (SVN)

  • Perforce (Helix Core)

They are known for their ease of use and simplicity

Like Git, it supports branching, merging, and distributed workflows.


CVCS & DVCS

Centralized Version Control System (CVCS)Distributed Version Control System (DVCS)
In a CVCS, there is a central repository that stores the entire version history of the project. All team members connect to this central repository to retrieve and submit changes.In a DVCS, each team member has a complete copy (clone) of the entire repository, including its history, on their local machine.
Requires constant network access to the central repository for most operations.Most operations (e.g., commit, log, branch) can be performed locally without constant network access.
Version history and branching are managed centrally, and each commit affects the entire project.Branching and merging are typically easier and more flexible in DVCS, as each clone is a full-fledged repository.
SVN (Subversion) is a popular centralized version control system.Git is the most widely used distributed version control system.

Git log

Purpose:git log is used to display the commit history of a repository.

Usage: Running git log in the command line will show a chronological list of commits, starting with the most recent commit.

Information: It provides details such as the commit hash, author, date, and commit message for each commit. It's useful for understanding the overall history and timeline of a repository.

Git blame

  • Purpose:git blame is used to show what revision and author last modified each line of a file.

  • Usage: Running git blame <filename> will display the specified file with annotations next to each line, showing the commit hash, author, and date for the last modification to that line.

  • Information: It's useful for tracing the origin of changes in a file, which can be helpful for understanding why specific lines were added or modified.

In summary, git log provides an overview of the commit history for the entire repository, while git blame is focused on showing the history of changes for individual lines within a specific file.

Credential helper

In Git, a credential helper is a utility that helps Git securely store and retrieve authentication credentials, such as usernames and passwords. Git uses credential helpers to manage and cache authentication information for accessing remote repositories.

Index stage in git

In Git, the "index" is sometimes referred to as the "staging area" or "cache." The index is a crucial component in Git's workflow and plays a key role in preparing changes for a commit

When you make changes to your project, Git allows you to stage these changes before committing them. This means you can selectively choose which modifications you want to include in the next commit.

git tag

In Git, a tag is a reference to a specific commit in the Git history. It's like a snapshot of a specific version of your code at a particular point in time. Tags are often used to mark significant points, such as release points or version numbers.

Here are some common tasks related to Git tags:

1. Creating a Tag:

  • To create a lightweight tag (just a name pointing to a specific commit), use:

      git tag <tag_name>
    
  • To create an annotated tag (includes a message and additional information), use:

      git tag -a <tag_name> -m "Tag message"
    

2. Listing Tags:

  • To list all tags in your repository, use:

      git tag
    

3. Displaying Tag Information:

  • To display information about a specific tag, use:

      git show <tag_name>
    

4. Pushing Tags to a Remote Repository:

  • Tags are not automatically pushed to remote repositories when you push changes. To push tags, use:

      git push origin <tag_name>
    

    or to push all tags:

      git push --tags
    

5. Deleting a Tag:

  • To delete a local tag, use:

      git tag -d <tag_name>
    
  • To delete a remote tag, use:

      git push --delete origin <tag_name>
    

6. Checking Out a Specific Tag:

  • To check out the code at a specific tag, use:

      git checkout <tag_name>
    

7. Tagging a Specific Commit:

To tag a specific commit (using the commit hash), use:

git tag -a <tag_name> <commit_hash>

git merge

Merging combines the changes from one branch into another and creates a new "merge commit" to represent the integration point.

The commit history remains more linear, and the branch being merged in is preserved in the history.

Use merge when you want to combine changes from one branch into another while preserving the commit history of both branches.

git rebase

Rebasing moves or combines a sequence of commits to a new base commit, usually changing the commit history.

The commit history appears linear, and it might be more challenging to see where branches were integrated.

git revert

git revert is used to create a new commit that undoes the changes made by a previous commit.

It does not modify existing commits but instead creates a new commit that undoes the changes introduced by a specific commit.

git restore

git restore is used to restore the working directory or individual files to a specific state.

git reset

git reset is used to reset the current branch to a specified commit.

git fetch

when you want to see what changes are on the remote repository without merging them immediately. You can then decide to merge or inspect the changes before merging.

git pull

when you want to fetch changes from the remote repository and automatically merge them into the current branch.

git clone

when you want to create a local copy of a remote repository for the first time. This is typically done when you start working on a project.

git diff

compare changes between files or branches in a Git repository

1. File Comparison:

a. Compare the Working Directory with the Last Commit:

git diff <filename>

This command shows the differences between the changes in the specified file in the working directory and the last committed version.

b. Compare Between Two Commits:

git diff <commit1> <commit2> -- <filename>

2. Branch Comparison:

a. Compare the Working Directory with Another Branch:

git diff <branch_name> -- <filename>

This command shows the differences between the changes in the specified file in the working directory and the version in another branch.

b. Compare Between Two Branches:

git diff <branch1> <branch2> -- <filename>

This command compares the specified file between two different branches. Replace <branch1> and <branch2> with the actual branch names.

c. Compare All Changes Between Two Branches:

git diff <branch1>..<branch2> -- <filename>

This command compares all changes between two branches for the specified file. The .. syntax is used to indicate the range between two branches.