[Git Notes] - TheTestingAcademy (Pramod Sir) (1)
[Git Notes] - TheTestingAcademy (Pramod Sir) (1)
Source - https://sdet.live
y
What is GIT?
m
Why Use Git?
GIT vs Github vs Gitlab
Git Architecture
de
Configuring Git
.git Directory
Commands
ca
Creating a new repository
Checking the status
Commiting
gA
Blame
Remote repositories
Connecting to a remote repository
tin
Cloning a repository
Getting changes from a server
Branches
es
✅
50+ Git Interview QnA:
✅
To initialize a repository or create new git repository in your local system -
✅
What are the new changes and files updated in your code?
✅
How to add files to the staging area?
✅
How to commit code to local git repo? (local repository not yet pushed to remote)
✅
How to push code to a remote repository?
✅
Pushing the code to remote repository
✅
How to review the current branch?
✅
How to create and update a working branch?
How to update remote repository URL?
✅ How to get status and updates made in remote branches?
✅ How to update the local repository?
✅ How to delete a branch on local (after commit)?
✅ How to force delete a branch?
✅ How to remove files from the staging area?
✅ How to create a git-ignore file?
✅ How to get a new repository in your local system?
✅ How to search for text in the current working repo/directory?
✅ How to create and move branch working directory (local)?
✅ How to fetch the latest changes from remote to local?
✅ How to save local changes made without committing or moving them to remote
y
✅ How to use stashed changes in git local repo so as to commit to remote repository?
repository?
m
✅ GIT Workflow:
Command Used: git stash apply
de
ca
Install Git into the System
1. Linux - Simply open up a new terminal and install Git via your distribution's package
gA
manager. For Ubuntu, the command is: sudo apt-get install git
2. Windows - we recommend git for windows as it offers both a GUI client and a BASH
command line emulator.
3. OS X - The easiest way is to install Homebrew, and then just run brew install git
tin
What is GIT?
eT
● Git is a version control system that is used for tracking changes to files.
● It does this through a series of snapshots of your project. It works with those
snapshots to version and manage your source code, and it does this in a simple way.
Th
y
m
de
ca
Git Architecture
gA
tin
es
eT
de
$ git config --global user.name "My Name"
$ git config --global user.email [email protected]
ca
git config --global --list
.git Directory
gA
The .git directory contains all the configurations, logs, branches, HEAD, and more
Commands
tin
git init
eT
Staging
● Git has the concept of a "staging area".
● You can think of this like a blank canvas, which holds the changes which you would
like to commit.
● It starts out empty, but you can add files to it (or even single lines and parts of files)
with the git add command, and finally commit everything (create a snapshot) with git
commit
git add
git add hello.txt
git add . // Add everything
git status
Commiting
A commit represents the state of our repository at a given point in time.
It's like a snapshot, which we can go back to and see how thing were when we took it.
Blame
y
Examine specific parts of the code’s history and find out who was the last author to modify
m
that line “`bash.
Show what revision and author last modified each line of a file
https://git-scm.com/docs/git-blame
de
Remote repositories
ca
git remote add
gA
Connecting to a remote repository
1. In order to upload something to a remote repo.
2. Create a Repo at Github.com and Add Remote to it.
tin
3. The Git command to do this is git push and takes two parameters.
the name of the remote repo (we called ours origin) and the branch to push to
es
git add .
git commit -m “blah blah”
Th
Cloning a repository
● Download locally and have a fully working copy of your project
git clone https://github.com/PramodDutta/Restfulbooker.git
Branches
y
m
de
ca
gA
tin
es
Switching branches
Th
Merging branches
TEMPORARY COMMITS
y
git stash
//Save modified and staged changes
m
git stash list
//list stack-order of stashed file changes
de
git stash pop
//write working from top of stash stack
git stash drop
ca
//discard the changes from top of stash stack
gA
Little Advanced
1. Setting up .gitignore
tin
a. log files
b. task runner builds
c. the node_modules folder in node.js projects
es
7. Resolve conflict
a. git merge tim_branch
<<<<<<< HEAD
// Use a for loop to console.log contents.
for(var i=0; i<arr.length; i++) {
console.log(arr[i]);
}
=======
y
// Use forEach to console.log contents.
m
arr.forEach(function(item) {
console.log(item);
});
de
>>>>>>> Tim's commit.
$ git add -A
ca
$ git commit -m "Array printing conflict resolved."
gA
Git Bisect
● Git bisect is a command in the Git version control system that helps in identifying the
tin
● https://education.github.com/git-cheat-sheet-education.pdf
● https://tutorialzine.com/2016/06/learn-git-in-30-minutes
● https://www.geeksforgeeks.org/git-difference-between-merging-and-rebasing/
Th
● https://learnxinyminutes.com/docs/git/
What is GIT?
GIT is a version control and source code management (SCM) system that is designed to
handle both small and large projects quickly and efficiently.
What is GIT version control?
● GIT version control enables you to monitor the evolution of a group of files (code
files).
● It allows for the creation of several versions of a file collection. Each version saves a
snapshot of the files at a specific point in time, and the snapshot can be used to
revert the collection of files. (The code can be developed in different versions of
Java, and can be merged in Git)
● VCS permits switching between these versions. These versions are often maintained
in a location known as a repository. (You can move between Java versions during the
development process.)
y
m
What is a Distributed Control System?
Later, the code is sent from our local workstation to the central repository (GitHub). Working
does not require a connection to a centralized repository.
de
ca
How to configure GitHub repository locally?
gA
# git config --global alias.lo "log --oneline" -----> To create an Alias to Command
# git config --global --unset alias.lo -----> To Remove an Alias
# git config --global --unset user.name -----> to remove username
tin
y
m
de
ca
gA
How to edit an incorrect commit message in Git? Or How can you fix a broken
commit?
git commit --amend -m "This is your new Git Message"
tin
y
m
de
ca
What is Git and how does it work?
Git is a version control system that tracks changes to files and directories. It allows multiple
gA
people to work on the same project simultaneously and keeps track of all changes made to
the files.
tin
command prompt. This will initialize a new repository in the current directory.
y
repository to the remote repository.
m
How do I create a new branch in Git?
To create a new branch in Git, you can use the command "git branch [branch name]" in the
de
terminal or command prompt. This will create a new branch in the repository that can be
used to make changes without affecting the master branch.
ca
How do I switch between branches in Git?
gA
To switch between branches in Git, you can use the command "git checkout [branch name]"
in the terminal or command prompt. This will switch to the specified branch and make it the
active branch.
tin
command prompt. This will open a tool that allows you to compare and resolve conflicts
between different versions of a file.
Th
y
command prompt. This will create a new tag that can be used to mark a specific commit in
m
the repository.
de
To push a tag to a remote repository, you can use the command "git push [remote name] [tag
name]" in the terminal or command prompt. This will send the specified tag to the remote
repository.
ca
How do I view the status of a Git repository?
gA
To view the status of a Git repository, you can use the command "git status" in the terminal or
command prompt. This will show the current state of the repository, including changes that
have been made but not yet committed.
tin
To remove a remote repository in Git, you can use the command "git remote remove [remote
name]" in the terminal or command prompt. This will remove the specified repository as a
remote that can be accessed and pushed to.
y
How do I view the commit history in Git?
m
To view the commit history in Git, you can use the command "git log" in the terminal or
command prompt. This will display a list of all commits made to the repository, along with the
commit message, author, and date.
de
How do I view the difference between two commits in Git?
To view the difference between two commits in Git, you can use the command "git diff
ca
[commit hash 1] [commit hash 2]" in the terminal or command prompt. This will display the
changes made between the two specified commits.
gA
How do I cherry-pick a commit in Git?
To cherry-pick a commit in Git, you can use the command "git cherry-pick [commit hash]" in
the terminal or command prompt. This will apply the changes made in the specified commit
to the current branch.
tin
To merge a branch in Git, you can use the command "git merge [branch name]" in the
terminal or command prompt. This will combine the changes made on the specified branch
with the current branch.
Th
y
How do I clone a repository in Git?
m
To clone a repository in Git, you can use the command "git clone [repository URL]" in the
terminal or command prompt. This will create a copy of the specified repository on your local
machine.
de
ca
GIT Notes(with Interview QnA) - for
Automation Testers / SDET
gA
tin
Important GIT Command Useds which can be used day to day life for managing
code on remote repositories.
local system -
eT
✅ What are the new changes and files updated in your code?
Command Used: git status
y
Command Used: git remote add origin <remote_url>
m
✅ Pushing the code to remote repository
de
Command Used: git push origin <branch_name>
ca
✅ How to review the current branch?
gA
Command Used: git branch (it reflects all existing branches and current working branch)
tin
y
✅ How to create a git-ignore file?
m
de
Command Used: touch .gitignore (works in git-bash)
Mac Terminal: git add .gitignore
Note: Used when you want to save local changes temporarily that are not yet committed. Its
primary use is when you want to switch a branch or work on different task.
✅ How to use stashed changes in git local repo so as to commit to
remote repository?
y
m
✅ GIT Workflow:
de
ca
gA
tin
es
eT
Th
Th
eT
es
tin
gA
ca
de
m
y