Thursday, May 28, 2020

Git & Git Commands

What is Git?

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Git Workflow Diagram

In above diagram Local is your local Git client and Remote is your online Github, Gitlab or Bitbucket

Git Commands

  • Initialize Git

    • git init

  • Configure the Git user’s email address and username

  • Create Git Branch

    • git branch feature (feature is the name of the branch)

    • git checkout -b demo (demo is the name of the branch)

  • List available Git Branch

    • git branch

    • git branch -a (list all branch)

    • git branch --all (list both remote-tracking and local branches)

  • Switch to available Git Branch

    • git checkout demo (demo is the available branch and by running this command you will switch to demo branch)

  • Delete Git Branch

    • git branch -D demo (demo is the name of the branch)

  • Rename or move Git branch

    • git branch -m old branch name new branch name

    • git branch --move old branch name new branch name

  • Adding file to Git staging area

    • git add filename (mentioning file name will add that specific file only)

    • git add . (Using this command will add all files in Git Stating area)

  • Committing a Git file

    • git commit -m “type some message to give reference” filename

  • Check the status of Git file, repo & branch

    • git status

    • git log filename (will display commit id, Author, date, HEAD & commit message)

  • Cloning Remote Git repo on your local git client

    • git clone repo url

  • Pulling remote repo

    • git pull repo url

  • Compare Git branches

    • git diff first branch name second branch name

  • Push to a Remote Repository (Consider Bitbucket here)

    • git push <remote_server> <branch_name> (This command specifies you're pushing to)

      • remote_server — the name of the remote server. In most cases, origin indicates that you're pushing to Bitbucket.

      • branch_name — the repository branch where you made your changes and want to push them. A branch allows you do work on a set of code for your repository separate from the main codebase.

      • If prompted for authentication, enter your Bitbucket username and password.
        In below example, origin is the remote server and master is the branch where you're pushing:

        • example command - git push origin master