Git and GitHub (Version Control System) (June 12)

Git and GitHub (Version Control System) (June 12)

S.No.GitGitHub
1.Git is software.GitHub is a service.
2.Git is a command-line toolGitHub is a graphical user interface
3.Git is installed locally on the systemGitHub is hosted on the web
4.Git is maintained by Linux.GitHub is maintained by Microsoft.
5.Git is focused on version control and code sharing.GitHub is focused on centralized source code hosting.
6.Git is a version control system to manage source code history.GitHub is a hosting service for Git repositories.
7.Git was first released in 2005.GitHub was launched in 2008.
8.Git has no user management feature.GitHub has a built-in user management feature.
9.Git is open-source licensed.GitHub includes a free tier and a pay-for-use tier.
10.Git has minimal external tool configuration.GitHub has an active marketplace for tool integration.
11.Git provides a Desktop interface named Git Gui.GitHub provides a Desktop interface named GitHub Desktop.
12.Git competes with CVS, Azure DevOps Server, Subversion, Mercurial, etc.GitHub competes with GitLab, Bit Bucket, AWS Code Commit, etc.

Basic Git Commands and Understanding Branches

Introduction: Git is a widely used version control system that helps developers track changes in their code and collaborate effectively. This article will cover some essential Git commands and provide an understanding of branches in Git. By following these instructions, you'll be equipped with the necessary knowledge to manage your code repositories efficiently.

Setting Global Username and Email: Before using Git, it's crucial to set your global username and email. This information will be associated with your commits.

git config --global user.name "<your username>"
git config --global user.email "<your email>"

Initializing an Empty Git Repository: To start using Git in a directory, you need to initialize a new repository.

git init

Cloning an Existing Git Repository: To obtain a copy of an existing Git repository, you can clone it using the repository URL.

git clone <repository_url>

Adding Files to Git: You can add specific files to the staging area using the add command.

git add <filename>

Adding All Files in the Current Directory to Git: If you want to add all the files in the current directory to the staging area, you can use the following command.

git add .

Committing Staged Files: After adding files to the staging area, you need to commit them to create a new snapshot of your code.

git commit -m "<your_commit_message>"

Restoring Files from Being Modified to Tracked: If you want to revert changes made to a file and restore it to the last committed state, you can use either of the following commands.

git restore <filename>
git checkout <filename>

Checking Git Repository Status: To check the status of your Git repository, use the following command.

git status

Viewing Branches: To see a list of branches in your Git repository, execute the following command.

git branch

Creating and Switching to a New Branch: If you want to create a new branch and switch to it simultaneously, you can use the following command.

git checkout -b <branch_name>

Switching to an Existing Branch: To switch to an existing branch, use the following command.

git checkout <branch_name>

Deleting a Branch: To remove a branch from Git, you can use the following command.

git branch -d <branch_name>

Adding a Remote Origin URL: To add a remote origin URL to your Git repository, use the following command.

git remote add origin <your_remote_git_url>

Removing a Remote Origin URL: If you need to remove a remote origin URL, execute the following command.

git remote remove origin

Fetching Remote Branches: To fetch all the remote branches from the remote repository, use the following command.

git fetch

Pushing Local Changes to a Remote Branch: To push your local changes to a remote branch, execute the following command.

git push origin <branch_name>

Pulling Remote Changes to a Local Branch: To pull the latest changes from a remote branch to your local branch, use the following command.

git pull origin <branch_name>

Checking Git Commits and Logs: To view the commit history and logs of your Git repository, execute the following command.

  1. Git Branching:
  • To show all branches: git branch

  • To create a new branch: git branch <branch_name>

  • To switch to a specific branch: git checkout <branch_name>

  • To delete a branch: git branch -d <branch_name>

  • Branch merge: Use the git merge <branch_name> command to merge branches.

  1. Conflict Resolution:
  • Conflicts occur when the same file has different content in different branches.

  • Resolving conflicts involves making adjustments, staging changes, and creating a new commit.

  • Git commands for conflict resolution:

    • git log merge: Lists the commits causing the conflict.

    • git diff: Identifies differences between repositories or files.

    • git checkout: Undoes changes or switches branches.

    • git reset mixed: Undoes changes in the working directory and staging area.

    • git merge --abort: Exits the merge process and returns to the pre-merge state.

    • git reset: Resets conflicted files to their original state.

  1. Cherry Picking:
  • Cherry picking allows you to pick a commit from one branch and apply it to another.

  • Use git cherry-pick <commit> to apply a specific commit to the current branch.

  1. Git Stash:
  • Stashing allows you to store changes temporarily without committing them.

  • Commands for using Git stash:

    • git stash: Stashes changes.

    • git stash list: Lists stashed items.

    • git stash apply stash@{<list_number>}: Applies stashed changes.

    • git stash clear: Clears all stash items.

  1. Git Rebase:
  • Git rebase is used to move or combine commits to a new base commit.

  • Use git rebase <base> to perform a rebase operation.

  1. Git Squash:
  • Squashing combines multiple commits into one, creating a more concise commit history.

  • Squashing can be done using Git's interactive rebase feature or merging branches.

  • Steps to squash commits:

    • Check commit history: git log --oneline

    • Choose commits to squash: git rebase -i HEAD~<number_of_commits>

    • Update commit messages in the text editor.

  1. Git Fork:
  • A fork is a copy of a repository, allowing you to experiment with changes without affecting the original project.

  • Forking is commonly done on platforms like GitHub.