How to work with Git for beginners?

If you are a Developer, QA Tester, Agile team member or DevOps Engineer you may be interested to learn git faster. So in this article we are going to discuss some concepts and commands related to git.

What is Git?

Git is a distributed version control system designed to manage and track changes to files in a project over time. It allows multiple users to collaborate on a project by providing a centralized repository where they can share and synchronize their work. Git is widely used in software development to track code changes, but it can also be used for any type of file-based project.

With Git, each user has a local copy of the entire project, including the complete history of changes. This allows them to work offline and independently, making Git a distributed version control system. Users can create branches to work on new features or experiments without affecting the main project. Once their changes are ready, they can merge them back into the main branch.

Git stores data in a series of snapshots, where each snapshot represents the complete state of the project at a specific point in time. It uses efficient algorithms to track changes at a file level, reducing the storage requirements and making operations such as commits and branching fast and lightweight.

Key concepts in Git include:

  1. Repository: A repository is a collection of files and their complete history of changes.
  2. Commit: A commit is a snapshot of the project at a specific point in time. It records changes made to the files and includes a commit message to describe the changes.
  3. Branch: A branch is a separate line of development that allows users to work on new features or fixes without affecting the main project. Branches can be created, switched, merged, and deleted.
  4. Merge: Merging combines changes from different branches into a single branch, incorporating the changes made in each branch.
  5. Remote: A remote is a reference to a repository on a different machine or server, allowing users to collaborate and share their changes with others.

Git provides a powerful set of commands and features to manage the version control process efficiently. It is widely adopted in the software development industry due to its flexibility, speed, and robustness.

Installing Git in your local system.

To install Git on Windows, you can follow these steps:

  1. Visit the official Git website: https://git-scm.com/
  2. On the website’s homepage, locate the “Downloads” section and click on the “Windows” link.
  3. This will redirect you to the Git for Windows download page. The page should automatically detect the appropriate version for your system. Click on the download link to start the download.
  4. Once the download is complete, locate the downloaded installer file (e.g., Git-x.x.x.x-64-bit.exe) and double-click on it to launch the installer.
  5. The installer will guide you through the installation process. You can leave the default settings as they are unless you have specific preferences.
  6. On the “Select Components” screen, you can choose the components to install. It is recommended to keep the default selections, which include the Git Bash, Git GUI, and Git Credential Manager.
  7. On the “Adjusting your PATH environment” screen, select the second option, “Use Git from Git Bash only,” which enables Git Bash as the command-line interface for Git. This option ensures that Git commands are available in the Git Bash and other terminals.
  8. Choose the line ending conversion option based on your preference. The default option, “Checkout as-is, commit as-is,” is generally recommended.
  9. Choose the terminal emulator used by Git Bash. The default selection, “Use Windows’ default console window,” is suitable for most users.
  10. Choose the default behavior for line ending conversions when working with text files. The default option, “Checkout Windows-style, commit Unix-style line endings,” is recommended.
  11. Optionally, choose the default behavior for handling symbolic links. The default option, “As is,” is suitable for most cases.
  12. Choose the default Git branch name. The default selection, “Let Git decide,” is commonly used.
  13. Click “Install” to begin the installation process.
  14. Once the installation is complete, you can click “Finish” to exit the installer.
  15. To verify the installation, open the Git Bash application by searching for “Git Bash” in the Start menu. This will open a terminal window where you can type git --version and press Enter. You should see the Git version number displayed, indicating that Git has been successfully installed.

To install Git on Ubuntu, you can follow these steps

  1. Open a terminal window on your Ubuntu system. You can do this by pressing Ctrl + Alt + T or searching for “Terminal” in the applications menu.
  2. Update the package list by running the following command:
    sudo apt update
  3. Once the package list is updated, you can install Git by running the following command:
    sudo apt install git
  4. Ubuntu will prompt you to confirm the installation. Type Y and press Enter to proceed.
  5. The package manager will download and install Git on your system.
  6. After the installation is complete, you can verify the installation by checking the Git version. Run the following command:
    git --version

    The terminal should display the installed Git version, confirming that Git has been successfully installed.

Git Cheat Sheet

  1. Configuration:

  • git config –global user.name “Your Name” – Set your name for Git commits.
  • git config –global user.email “yourname@example.com” – Set your email for Git commits.
  • git config –global core.editor “editor” – Set your preferred text editor for commit messages.
  • git config –list – List your Git configuration settings.
  1. Repository Initialization:

  • git init – Initialize a new Git repository in the current directory.
  • git clone <repository> – Clone a remote repository to your local machine.
  1. Snapshotting and Staging:

  • git status – Show the current status of the repository, including tracked/untracked files and changes.
  • git add <file> – Add a file or changes to the staging area for the next commit.
  • git diff – Show the differences between the working directory and the staging area.
  • git diff –staged – Show the differences between the staging area and the last commit.
  • git commit -m “commit message” – Commit the staged changes with a descriptive message.
  • git rm <file> – Remove a file from the repository and the working directory.
  1. Branching and Merging:

  • git branch – List all branches in the repository.
  • git branch <branch-name> – Create a new branch with the specified name.
  • git checkout <branch> – Switch to a different branch.
  • git checkout -b <branch-name> – Create a new branch and switch to it.
  • git merge <branch> – Merge changes from one branch into the current branch.
  • git branch -d <branch-name> – Delete a branch.
  1. Remote Repositories:

  • git remote add <name> <url> – Add a remote repository with the given name and URL.
  • git remote -v – List all remote repositories and their URLs.
  • git fetch <remote> – Fetch changes from a remote repository.
  • git pull <remote> <branch> – Fetch and merge changes from a remote repository.
  • git push <remote> <branch> – Push changes to a remote repository.
  • git remote remove <name> – Remove a remote repository.
  1. Inspecting History:

  • git log – Display the commit history, showing author, date, and commit message.
  • git log –oneline – Show the commit history in a condensed format.
  • git show <commit> – Show detailed information about a specific commit.
  • git blame <file> – Show who last modified each line of a file.
  • git diff <commit> <commit> – Show the differences between two commits.
  1. Miscellaneous:

  • git stash – Temporarily save changes that are not ready to be committed.
  • git stash list – List all stashed changes.
  • git stash apply – Apply the most recent stash and keep it in the stash list.
  • git tag <tag-name> – Create a new tag for a specific commit.
  • git reset <commit> – Reset the current branch to a specific commit.
  • git clean -n – List untracked files in the repository.
  • git ignore – Specify files and patterns to ignore.

Learning git is a fundamental step towards software development, QA Testing or DevOps engineer job role. If you are a fresher you must learn git and git commands basics to have a better understanding how version management in a repository works.  

Leave a Comment

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

error: Content is protected !!
Scroll to Top