Git Tutorial

·

12 min read

Git Tutorial

What is a Version Control System?

  1. A VCS is a software tool that allows developers to manage changes to the source code of a software project. It enables developers to track and manage different versions of code files, collaborate with others, and revert to earlier versions if necessary.

  2. Version control enables the current team to analyze the deletion, editing, and creation of datasets made since the original copy. It brings clarity to the development of the software.

What are the benefits of using version control?

● All team members are free to work on any file at any time with the Version Control System (VCS). Later on, VCS will allow the team to integrate all of the modifications into a single version.

● The VCS asks to provide a brief summary of what was changed every time we save a new version of the project. We also get to examine exactly what was modified in the content of the file. As a result, we will be able to see who made what changes to the project.

● Inside the VCS, all the previous variants and versions are properly stored. We will be able to request any version at any moment, and we will be able to retrieve a snapshot of the entire project at our fingertips.

● A VCS that is distributed, such as Git, lets all the team members retrieve a complete history of the project. This allows developers or other stakeholders to use the local Git repositories of any of the teammates even if the main server goes down at any point in time.

Indeed, here are the main advantages of using a Version Control System (VCS) in a bold format:

  1. Collaboration

  2. Version Tracking

  3. Revert to Previous Versions

  4. Branching

  5. Conflict Resolution

  6. Backup

  7. Code Reviews

  8. Traceability

  9. Efficient Collaboration

  10. Open Source

The following are the types of VCS :

 Centralized version control system (CVCS).

 Distributed/Decentralized version control system (DVCS).

The drawback of CVCS:

➢ It’s not locally available, meaning you always need to be connected to a network to perform any action.

➢ Since everything is centralized, if the central repository or server fails or crashes, you will lose entire data.

What are centralized version control systems (CVCS)?

It is a version control where there is a single central repository hosted on a server. This server is expected to have the latest code and expects all its clients to contribute by being connected to the server always.

What are distributed version control systems (DVCS)?

Distributed revision control synchronizes repositories by transferring patches from peer to peer. There is no single central version of the codebase instead, each user can download a working copy and the full change history. Hence not required to be connected to the server all the time.

Example: GIT

What is Source Code Management?

It is a process through which we can store and manage any code. Developers write code, Testers write test cases and DevOps engineers write scripts. We can store and manage this code in Source Code Management. Different teams can store code simultaneously. It saves all changes separately. We can retrieve this code at any point in time. Ex: Bitbucket, Git

What is Git and why is it used?

GIT is a distributed version control system and source code management (SCM) system with an emphasis to handle small and large projects with speed and efficiency.

It is most suitable when multiple people are working on projects as a team and is used for tracking the project changes and efficiently supports the collaboration of the development process.

With the help of the versioning system, the developer can identify who has made what changes and then run tests and fix bugs if any, and then do the necessary feature implementation

Advantage of git

  1. One of the biggest advantages of Git is its branching capabilities

  2. Pull Requests

  3. Open Source and Community Support

  4. Offline Access: You can work with Git offline, making commits and managing branches without needing a network connection.

  5. Faster Release Cycle

GitHub is a web-based Git repository hosting service

GitHub Alternatives

  • BitBucket

  • GitBucket

Git branch manages branches within a Git repositoryA Git repository is a container that stores code and history.

What is the difference between Git and GitHub?

GIT is a distributed version control system installed on local machines that allows developers to keep track of commit histories and support collaborative work.

Git files are stored on both the server and client, allowing users to work independently, make changes offline, and collaborate with others by merging changes

GIT is a softwareGitHub is a service
Git can manage source code HistoryGitHub is a cloud-based hosting service that lets you manage Git repositories
Git is installed locally on the systemGitHub is maintained by Microsoft
Git is focused on version control and code sharingGitHub is hosted on the web
Git is a command-line toolGitHub is focused on centralized source code hosting
Git has no user management featureGitHub is a graphical user interface
SVN and Mercurial are the competitorsGitHub has a built-in user management feature

Some Important Questions!

What are the Git commands that you use to commit changes to your remote repository?

  1. Git Add

  2. Git Commit

  3. Git Push

What is the difference between git fetch and git push?

Git fetch retrieves changes from a remote repository to your local repository without merging.

Git push sends your local commits to a remote repository, updating its branches. Both are essential for collaboration and keeping local and remote repositories in sync.

What is the difference between git pull and git fetch?

Git fetch will download new commits from a remote repository to a local repository git pull does the same as git fetch and also merges the same into your local working files.

SVN vs GIT

SVN (Subversion): Centralized version control system. Single central repository. Slower, complex branching and merging, tracks changes per file.

Git: Distributed version control system. Each developer has a local repository. Faster, easy branching and merging, tracks changes per-commit. Adaptable workflows, extensive community, and tool support. Ideal for distributed and collaborative development.

What are .git and .gitignore?

.git Directory: The heart of a Git repository, containing all version history and configuration. Never manually modify it.

.gitignore File: A text file that specifies which files and directories Git should ignore when tracking changes. Helps keep irrelevant or sensitive files out of version control.

What are pre-commit hooks and post-commit hooks?

Pre-commit Hooks: These are scripts or actions that run before a Git commit is completed. They are used to enforce coding standards, perform code checks, or ensure that commit messages follow a specific format.

Post-commit Hooks: These are scripts or actions that run after a Git commit has been successfully made. They can be used for tasks like sending notifications, triggering automated builds, or updating issue trackers with commit information.

What are git webhooks?

When planning to cascade/notify new activities on the git server to a different tool like Jenkins, git webhook is used. the webhook contains information about the activity (ex: push on the server)

What is a ‘conflict’ in git?

A "conflict" in Git occurs when two different branches or commits have made conflicting changes to the same part of a file, making it unclear which change should be accepted. Git cannot automatically resolve such conflicts and requires manual intervention from the user to decide which version of the changes to keep. Conflicts typically happen during the process of merging or rebasing branches.

OR

A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.

How can conflict in git be resolved?

To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running “git add” After that to commit the repaired merge, run “git commit”.Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.

OR

  • Identify the Conflict: Git will highlight conflicts in the affected files.

  • Open the File: Edit the conflicted file(s) in a text editor.

  • Manually Edit: Locate and remove conflict markers (e.g., <<<<<<<, =======, >>>>>>>) and keep desired changes.

  • Stage the File: Use git add to stage the resolved file.

  • Finish Merge/Rebase: Complete the merge or rebase (e.g., git commit, git rebase --continue).

  • Test and Commit: Verify code functionality and commit if needed.

  • Regenerate

What is git stash and talk about its use cases.

Git stash is a feature in Git that allows you to temporarily save your changes without committing them. It's useful when you need to switch branches or perform some other operation that requires a clean working directory.

Difference between git clone and git fork?

Git clone is a command used to create a copy of an existing Git repository on your local machine. It copies the entire repository, including all branches and commit history, allowing you to work on the project and contribute changes. Typically used for collaborating on an existing project as a contributor. git fork:

Git fork is not a Git command but a feature provided by Git hosting platforms like GitHub and GitLab. It allows you to create a personal copy (fork) of a repository hosted on these platforms.

what is amend in git?

In Git, "amend" refers to the action of modifying the most recent commit. It allows you to make changes to the last commit message or include additional changes without creating a new commit. This is done using the git commit --amend command. It's useful for making small corrections or adding missed changes to the last commit in your version history.

How to amend a commit in git?

  1. Make your changes to the files.

  2. Stage the changes with git add.

  3. Use git commit --amend to update the last commit with your changes.

What is cherry-pick in git?

Cherry-pick allows you to pick a particular commit from a branch and insert into another branch. This is different from git merge in that, git merge will bring in all commits from branches while cherry-pick picks a specific commit only

How to create or initialize a Git Repository?

Git init

What language is used in GIT?

Git is primarily implemented in the C programming language, while its user interface and extensions can use various scripting languages.

Why is GIT better than Subversion (SVN)?

Git is an open-source version control system; it will allow you to run a ‘version' of a project. Multiple developers can check out, and upload changes and each change can then be attributed to a specific developer.

How do you undo the last commit?

git revert <commit_id>

How do I change the URL for a remote Git repository?

git remote set-url origin git://this.is.new.URL

What is the difference between git merge and git rebase?

  1. git merge applies all unique commits from branch A into branch B in one commit with the final result.

  2. git rebase gets all unique commits from both branches and applies them one by one.

  3. git merge doesn’t rewrite commit history, just adds one new commit

  4. git rebase rewrites commit history but doesn’t create extra commits for merging

    What is another option for merging in Gi?

“Rebasing” is an alternative to merging in git.

What is the difference between ‘git remote’ and ‘git clone’?

Git remote add’ just creates an entry in your git config that specifies a name for a particular URL. While ‘git clone’ creates a new git repository by copying an existing one located at the URI.

Master or the main branch what is the other branch that is quite commonly used?

it's called a bug fix Branch or hotfix Branch

From which branch do you usually perform release?

Release branch

What is the branch that always is updated and up to date?

it's called your master trunk or main branch

Keywords

git --versionIt shows the version of Git installed on your machine
git initIt is used to initialize a new Git repository in a directory
git clone [repository URL]This command is used to obtain a repository from an existing GitHub repo.
git add .This command is used to add all the files to the staging area
git add [file name]It will add all your files to the git staging area
git pushIt will push all the local changes that you've made to the remote GitHub repository
git pullIt will pull ( fetch ) all the updated code from the remote branch and merge it with your local branch
git merge [branch name]This is a command used to combine changes from one branch into another branch in a Git repository
git statusIn simple terms, it will show you exactly which files/folders have been modified
git diffThis command in Git is used to display differences between different states of your Git repository. It can be used in various ways to compare changes between commits, branches, and working directories.
git commit -mto set commit message
git logIt will list down the entire commit history i.e, all the commits that you've made till now
git branch branch nameThis command is used to create a new branch in your local git repository
git branchIt will list down all the local branches that you've created
git branch -aIt will list down all the branches i.e., local branches + remote branches that are available for checkout
git branch -d branch nameto delete a branch
git branch -m <new name>Used to rename the current working branch
git checkout branch nameto navigate a branch
git checkout .to see the last commit
git checkout -idto see that commit
git checkout masterto navigate the current position
git checkout -b <branch-name>Creates a new branch and switches to the new one.
git stashIt's used to temporarily save and remove the changes that you've made on the working tree
git remoteIt will give the name of the remote repository e.g, " origin " or " upstream "
git remote -vIt will give the name as well as the URL of the remote repository
git grepcommand is used to search in the checkout branch and local files
git resetUndo the changes to the local files, and restore the last commit.