GIT Flashcards

1
Q

What is a version control system (VCS)?

A

A VCS

  • keeps track of the contributions of the developers working as a team on the projects.
  • maintains the history of code changes done and with project evolution, it gives an upper hand to the developers to introduce new code, fixes bugs, and run tests with confidence that their previously working copy could be restored at any moment in case things go wrong.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a git repository?

A

A repository is a file structure where git stores all the project-based files. Git can either stores the files on the local or the remote repository.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Steps to install git

A
  • git - will tell you whether it is installed already
  • download from the web
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does git clone do?

A

The command creates a copy (or clone) of an existing git repository. Generally, it is used to get a copy of the remote repository to the local repository.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you see a history of the git commits ?

A

git log

**git log -2 **(show just the last two commits)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you get rid of ‘unstaged changes’ ?
in other words, the file is being tracked by git, you have made uncommited/unstaged changes to it which you want to discard

A

git checkout – filename
or git checkout – . (for all the files)
or git restore filename

git checkout also used to switch branches.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you unstage a change ?

A

***git restore –staged <file>***</file>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what if you dont want to include certain files in your git repo ?

A

use .gitignore file
- should check in the .gitignore file so it stays with the repo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you delete a file ?

A

the delete is the same as a change. a new commit is made to reflect the deletion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create an issue ?

A

you can create an issue in Git by following these steps:

On GitHub.com, navigate to the main page of the repository.
Under your repository name, click Issues.
Click New issue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a pull request ?

A

A pull request is a feature in Git that allows developers to propose changes to a codebase hosted on GitHub. It is essentially a request to merge one branch into another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is fork ?

A

A fork is a Git feature that allows you to create a copy of an existing repository. Forking a repository creates a new repository in your account that is identical to the original repository. You can then make changes to the forked repository without affecting the original repository.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is rebase ?

A

A rebase is a Git command that allows you to modify the history of a branch. It works by moving the entire branch to begin on the tip of another branch. i.e. move the base of the branch to begin at the tip of another branch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the command git config do?

A

The git config command is a convenient way to set configuration options for defining the behavior of the repository, user information and preferences, git installation-based configurations, and many such things.

For example:
To set up your name and email address before using git commands, we can run the below commands:

  • git config –global
    user.name
    “«your_name»”
  • git config –global user.email “«your_email»”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you explain head in terms of git and also tell the number of heads that can be present in a repository?

A
  • A head is nothing but a reference to the last commit object of a branch.
  • For every repository, there will always be a default head referred to as “master” or now “main” (as per GitHub) but there is no restriction to the count of heads available. In other words, it can have any number of heads.
    Usages:
  • To go or checkout to 1 commit before the latest commit, we use git checkout HEAD~1
  • To uncommit the last 3 commits without losing the changes, we first run git reset HEAD~3. Then we can see the changes made in the last 3 commits and then update it manually and commit it finally.
  • In order to uncommit the last 3 commits and also remove the changes, we can run the command: git reset –hard HEAD~3. This command will completely remove all the changes.
  • To look into the changes made in the last 3 commits, we can run git diff HEAD~3
  • To make a new commit by reverting the last 3 commits, we can run the command: git revert –no-commit HEAD~3…HEAD
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a conflict?

A
  • Git usually handles feature merges automatically but sometimes while working in a team environment, there might be cases of conflicts such as:
  1. When two separate branches have changes to the same line in a file
  2. A file is deleted in one branch but has been modified in the other.
  • These conflicts have to be solved manually after discussion with the team as git will not be able to predict what and whose changes have to be given precedence
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the functionality of git ls-tree?

A

This command returns a tree object representation of the current repository along with the mode and the name of each item and the SHA-1 value of the blob.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does git status command do?

A

git status command is used for showing the difference between the working directory and the index which is helpful for understanding git in-depth and also keep track of the tracked and non-tracked changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Define “Index”.

A

Before making commits to the changes done, the developer is given provision to format and review the files and make innovations to them. All these are done in the common area which is known as ‘Index’ or ‘Staging Area’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does git add command do?

A
  • This command adds files and changes to the index of the existing directory.
  • You can add all changes at once using **git add . **command.
  • You can add files one by one specifically using ***git add <filename>*** command.</filename>
  • You can add contents of a particular folder by using ***git add /<foldername>/ ***command</foldername>

these files go to Staging - they still need to be Commited

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Why is it considered to be easy to work on Git?

A

With the help of git, developers have gained many advantages in terms of performing the development process faster and in a more efficient manner. Some of the main features of git which has made it easier to work are:

  • Branching Capabilities:
  • Due to its sophisticated branching capabilities, developers can easily work on multiple branches for the different features of the project.
  • It also has an easier merge option along with an efficient work-flow feature diagram for tracking it.
  • Distributed manner of development:
  • Git is a distributed system and due to this nature, it became easier to trace and locate data if it’s lost from the main server.
  • In this system, the developer gets a repository file that is present on the server. Along with this file, a copy of this is also stored in the developer’s system which is called a local repository.
  • Due to this, the scalability of the project gets drastically improved.
  • Pull requests feature:
  • This feature helps in easier interaction amongst the developers of a team to coordinate merge-operations.
  • It keeps a proper track of the changes done by developers to the code.
  • Effective release cycle:
  • Due to the presence of a wide variety of features, git helps to increase the speed of the release cycle and helps to improve the project workflow in an efficient manner.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How will you create a git repository?

A
  • Have git installed in your system.
  • Then in order to create a git repository, create a folder for the project and then run git init.
  • Doing this will create a .git file in the project folder which indicates that the repository has been created.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Tell me something about git stash?

A

Git stash can be used in cases where we need to switch in between branches and at the same time not wanting to lose edits in the current branch. Running the git stash command basically pushes the current working directory state and index to the stack for future use and thereby providing a clean working directory for other tasks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the command to create a branch ?

A

***git branch <branch-name>***</branch-name>

25
Q

command to create a branch and change to it right away

A

***git branch -b <branchname>***</branchname>

26
Q

What is the command used to delete a branch?

A
  • To delete a branch we can simply use the command git branch –d [head].
  • To delete a branch locally, we can simply run the command: ***git branch -d <localbranchname>***</localbranchname>
  • To delete a branch remotely, run the command: ***git push origin –delete <remotebranchname>***</remotebranchname>
  • Deleting a branching scenario occurs for multiple reasons. One such reason is to get rid of the feature branches once it has been merged into the development branch.
27
Q

What differentiates between the commands git remote and git clone?

A

git remote command creates an entry in git config that specifies a name for a particular URL. Whereas git clone creates a new git repository by copying an existing one located at the URL

28
Q

What does git stash apply command do?

A
  • git stash apply command is used for bringing the works back to the working directory from the stack where the changes were stashed using git stash command.
  • This helps the developers to resume their work where they had last left their work before switching to other branches
29
Q

Differentiate between git pull and git fetch.

A
  • git pull - This command pulls new changes from the currently working branch located in the remote central repository.
  • git fetch - This command is also used for a similar purpose but it follows a two step process:
    1. Pulls all commits and changes from desired branch and stores them in a new branch of the local repository.
    current
    2. For changes to be reflected in the current / target branch, git fetch should be followed by git merge command

git pull = git fetch + git merge

30
Q

Can you give differences between “pull request” and “branch”?

A
  • pull request - This process is done when there is a need to put a developer’s change into another person’s code branch
  • branch - A branch is nothing but a separate version of the code.
31
Q

Why do we not call git “pull request” as “push request”?

A
  • “Push request” is termed so because it is done when the target repository requests us to push our changes to it.
  • “Pull request” is named as such due to the fact that the repo requests the target repository to grab (or pull) the changes from it
32
Q

Can you tell the difference between Git and GitHub?

A
  • Git - This is a distributed version control system installed on local machines which allow developers to keep track of commit histories and supports collaborative work.This is maintained by “The Linux Foundation”.
  • GitHub -This is a cloud-based source code repository developed by using git. This was acquired by “Microsoft”
33
Q

What do the git diff and git status commands do?

A
  • git diff - This shows the changes between commits, working trees, etc.
  • git status - This shows the difference between the working directory and index that is essential in understanding git in depth.

git diff works in a similar fashion to git status with the o

34
Q

What has to be run to squash multiple commits (last N) into a single commit?

A

Squashing multiple commits to a single one overwrites the history which is why it is recommended to be done using full caution. This step can be done by running the command: git rebase -i HEAD~{{N}} where {{N}} represents the number of commits needed to be squashed

35
Q

How would you recover a branch that has already pushed changes in the central repository but has been accidentally deleted from every team member’s local machines?

A

We can recover this by checking out the latest commit of this branch in the reflog and then checking it out as a new branch

36
Q

Can you tell something about git reflog?

A

This command tracks every single change made in the repository references (that can be branches or tags) and also maintains the branches/tags log history that was either created locally or checked out. Reference logs such as the commit snapshot of when the branch was created or cloned, checked-out, renamed, or any commits made on the branch are maintained by Git and listed by the ‘reflog’ command.

This recovery of the branch is only possible when the branch was either created locally or checked-out from a remote repository in your local repository for Git to store its reference history logs.
This command should be executed in the repository that had the lost branch

37
Q

What consists of a commit object?

A

A commit object consists of the following components:

  • A set of files that represents the state of a project at a given point in time.
  • Reference pointer to parent commit objects.
  • A 40 character string termed as SHA-1 name uniquely identifies the commit object
38
Q

Explain the levels in git config and how can you configure values using them?

A

In order to make git work, it uses a set of configurations that are pre-defined by default by means of configuration files (or config files). We can change the default behavior of git by just modifying these files which are basically text files. In order to do this, it is important to understand how git identifies these files. It does so by following the below steps:

  • Firstly, git searches for the config values in the system-wide gitconfig file stored in «installationpath»/etc/gitconfig file that has settings defined and applied to every user of the system and all their repos.
    • In case you want git to search from this particular file and read/write on it, we can pass the option –system to git config command.
  • Next, git searches for the ~/.gitconfig file or* ~/.config/git/config* that has the scope specific to the user.
    • Git can be made to read/ write from this file specifically bypassing –global to the git config command.
  • Lastly, git searches for the config values in the git directory of the local repository that we are currently working on.
    • These config values are specific to that particular repository alone and can be accessed by passing –local to the git config command.This is the default config file that gets accessed and modified upon in case we do not specify any levels
39
Q

What is a detached HEAD and what causes this and how to avoid this?

A

Detached HEAD indicates that the currently checked-out repository is not a local branch. This can be caused by the following scenarios:

  • When a branch is a read-only branch and we try to create a commit to that branch, then the commits can be termed as “free-floating” commits not connected to any branch. They would be in a detached state.
  • When we checkout a tag or a specific commit and then we try to perform a new commit, then again the commits would not be connected to any branch. When we now try to checkout a branch, these new commits would be automatically placed at the top.

In order to ensure that detached state doesn’t happen, =instead of checking out commit/tag, we can create a branch emanating from that commit and then we can switch to that newly created branch by using the command: git checkout -b «newbranchname». This ensures that a new branch is checkout out and not a commit/tag thereby ensuring that a detached state wouldn’t happen

40
Q

What is the difference between git stash apply vs git stash pop command?

A
  • git stash pop command throws away the specified stash (topmost stash by default) after applying it.
  • git stash apply command leaves the stash in the stash list for future reuse. In case we wanted to remove it from the list, we can use the git stash drop command.

git stash pop = git stash apply + git stash drop

41
Q

How will you resolve conflict in Git?

A
  • Conflicts occur whenever there are multiple people working on the same file across multiple branches. In such cases, git won’t be able to resolve it automatically as it is not capable of deciding what changes has to get the precedence.
  • Following are the steps are done in order to resolve git conflicts:
    1. Identify the files that have conflicts.
    2. Discuss with members who have worked on the file and ensure that the required changes are done in the file.
    3. Add these files to the staged section by using the git add command.
    4. Commit these changes using the git commit command.
    5. Finally, push the changes to the branch using the git
42
Q

How to revert a bad commit which is already pushed?

A

Fix the bad changes of the files and create a new commit and push to the remote repository. This step is the simplest and most recommended approach to fix bad changes. You can use the command: ***git commit -m “<message>"***</message>

43
Q

Explain steps involved in removing a file from git index without removing from the local file system?

A

Sometimes we end up having certain files that are not needed in the git index when we are not being careful while using the git add command. Using the command git rm will remove the file from both the index and the local working tree which is not always desirable.

44
Q

What is a git rebase ?

A
45
Q

What does git init do ?

A

Initialises a git repository. Adds a .git folder

46
Q

How do you remove a file from staging ?

A

git rm –cached filename

47
Q

What does git restore filename do ?

A

if you have made changes to filename, which have not yet been staged, you can discard them with the restore command
or
git checkout – filename

48
Q

What does “git add . “ command do ?

A

this will add all of the files to the staging area

49
Q

When committing , what flag do you use for adding a message ?

A

use the -m flag. if you don’t use this, git will open an editor allowing you type a more substantial message

50
Q

How do you see the source code changes in GIT ? known as diff

A

git log -p (p stands for patch)
there are gui for this

51
Q

What is the difference between git restore and git checkout ?

A

can always use git restore it superceeds git checkout

52
Q

How do you switch between branches ?

A

git checkout branchname

53
Q

What does SCM stand for ?

A

Source Code Mangement system

54
Q

Alternatives to GitHub ?

A

GitLab
Atlassian’s Bitbucket
AWS CodeCommit

55
Q

Create a local git repository
Push it to GitHub

A
56
Q

what is a git tag ?

A

A Git tag is a label that you can apply to a specific commit in your Git repository. Tags are often used to mark important milestones in your project’s history, such as releases or major changes. They can also be used to mark specific commits that you want to reference later .

57
Q

what is git log –branch

A

provides a semi-graphically representation of the branches and associated commits to make it easire to visualise what has been going on

58
Q

how do you move the HEAD pointer to a specific earlier commit ?

A

git checkout the comm hash

59
Q

differences between git switch and git checkout ?

A

should really use git switch . more intuituve syntax