Reading list Switch to dark mode

    Git: Basics usage and techniques

    Updated 20 September 2023

    In the previous Git blog, we learned how to install git and configure, we will learn git basic usage and techniques to use git commands and explore these techniques.

    • Clone and Initializing the repository
    • Pull
    • Push
    • Status ( to check the commit changes )
    • Commit
    • Commit history
    • Compare commits

    What is a clone and how does Initializing a repository?

    A clone is a term commonly used in version control systems, such as Git, to refer to the process of creating a local copy of a remote repository.

    When you clone a repository, you create a copy of the entire repository’s history, including all branches, commits, and files. This local copy allows you to work on the project, make changes, and synchronize with the remote repository.

    Git repository initialize

    git init

    Exploring “Git Pull”: Understanding its functionality and operation.

    Find the Best CS-Cart
    Development Company
    Find out More

    In Git, “pull” is a command used to fetch the latest changes from a remote repository and incorporate them into the current local branch. It combines two operations: fetching and merging.

    git pull

    Git performs the following actions when you execute the “pull” command:

    • It fetches the latest commits and changes from the remote repository.
    • It identifies the changes that are not present in the local branch.
    • It merges those changes into the current branch, creating a new merge commit if necessary.

    If you want to pull changes from a different branch.

    git pull origin <branch>

    NOTE: “pull” command should be used with caution, especially if you have uncommitted changes in your local branch. It’s recommended to commit or stash your changes before pulling to avoid conflicts or potential loss of work.

    Push command and How does it work?

    In Git, “push” is a command used to upload your local commits and changes to a remote repository. It allows you to share your work with others or update a central repository with your local changes.

    git push

    NOTE: you should ensure that you have pulled the latest changes from the remote repository using the “pull” command. This helps avoid conflicts and ensures that you have the most up-to-date version of the branch before pushing your changes.

    If you want to push to a different location or branch than the default upstream

    git push origin <branch>
    push

    Difference between Commit & Commit history?

    Commits are snapshots of your entire repository at specific points in time. It is recommended to make new commits frequently, focusing on logical units of change. Over time, the commits should narrate the history of your repository and explain how it evolved to its current state.

    In addition to the contents and message, commits also include valuable metadata such as the author, timestamp, and more.

    git commit -m "update the README.md with link to contributing guide"
    commit

    Commit history refers to the chronological sequence of commits in a version control system, such as Git. It provides a comprehensive record of all the changes made to a project over time, allowing developers to track and understand the project’s evolution.

    git log

    This command displays the commit history in reverse chronological order. It shows the commit hash, author, date, and commit message for each commit.

    git log

    If you want to list logs from a different branch.

    git log <branch>
    git log branch
    git show <commit-hash>
    git show

    This command shows the details of a specific commit, including the changes made in that commit.

    Git Status and How does it work?

    In Git, git status is a command used to display the current state of the repository and the changes made to files. It provides information about modified, staged, and untracked files, as well as the branch being worked on.

    git change log update
    git status
    git status command

    Using the “git status” command you can quickly get an overview of the state of your repository and the changes you’ve made. It helps you track which files are modified, what changes are staged, and which files are not being tracked.

    Compare commits and How does it work?

    Git allows you to view and analyze the differences between two commits. It helps you understand the changes made, identify introduced bugs, track progress, and review modifications in the codebase.

    • Specifying Commits
    • Viewing Differences
    • Unified Diff Format
    • Detailed Change Analysis
    • Context and Patching
    • Visualizing Differences

    In the above command, replace commit1 and commit2 with the actual commit hashes, branch names, or other references that represent the commits you want to compare.

    For example, to compare the differences between the current commit and the previous commit, you can use:

    git diff commit1 commit2
    git diff

    This command compares the changes between the current commit (HEAD) and its parent commit (HEAD^).

    git diff HEAD HEAD^

    This command compares the changes between the specified commits on their respective branches.

    git diff staging/abc123 master/def456

    By using the git diff command with appropriate commit references, you can analyze and understand the differences between commits in your Git repository.

    branch-diff

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home