Git Basics
Git Basics
PREPARED BY
Aishwarya Verma
git Basics PAGE 01
What is git
About GIT
Git is a Distributed Version Control System.
Git was created by, Linus Torvalds on 7th April 2005, on Linux Kernel.
Git is a free software for tracking changes in any set of files.
Usually it is used for coordinating work among programmers for
collaboratively developing source code during software development.
2. Centralized VCS
Files tracked are saved in a central server
Pros
- Files can be pulled and changes can be pushed to the server
Cons
- If server is damaged, rollback to previous state is difficult.
What is git
3. Distributed VCS
Files tracked are saved in a central server, with all the history
details.
When files are pulled, all the file tracking details are also saved
in working computer. i.e., full backup is pulled.
Programmers can collaborate effectively.
Feature to ignore files which are not to be tracked.
If the server gets damaged, we still have the full backup with
tracking history of the project, which was pulled from server
previously.
Rollback to previous changes is very easy.
GIT Features
It captures snapshots, not the differences.
.git file, is the one which contains all the project history.
Almost every operation is local (no internet required)
i.e., all operations are performed locally and after that changes can be
pushed to a remote repository (centralized server).
The goals include speed, data integrity, and support for distributed,
non-linear workflows.
Git has integrity i.e., all the changes are tracked and need approval for
merging.
It uses SHA1 checksum to validate the files.
Git generally only adds data.
What is git
GIT Installation and Setup
Official link to download git software -
https://git-scm.com/downloads
Git installation comes with -
- git command line tool
- git bash (a terminal program)
- git GUI (Graphical User Interface), which uses command line tool
in backend.
After git installation, to check if it is installed successfully
- make sure to check the checkbox -> Launch Git Bash
Git commands can be used via windows power-shell also.
But using git commands without installing git, will lead to errors.
When the git bash terminal opens, you can use the below command -
$ git
What is git
Git - Three stage architecture
Un-Tracked
- The files of the which are not added to .git, i.e., files under repo
which are not being tracked.
- To specifically put some files of the repo untracked, we can put
them under .gitignore file.
Un-Modified
- The files of the repo, which are being tracked, and are not yet
modified.
Modified
- The files of the repo, which are being tracked and are modified.
Staged
- The files of the repo, that are modified and staged for next commit.
- Once the staged files are committed, the status of the file changes
to unmodified.
Basic Linux
commands
1. pwd
- Shows the path of present working directory
$ pwd
2. cd
- Change directory path
$ cd Desktop
$ cd /c
3. ls
- lists the files/contents present in current directory
$ ls
4. touch
- It is used to create a file without any content, i.e., it is empty.
- This command can be used when the user doesn’ t have data to
store at the time of file creation.
$ touch error.log
5. rm
- This command deletes the file with the name specified.
- If the file name is ".git" and it is deleted using this command,
the project folder will no more be tracked and so it will no more
be a git repo.
$ rm -rf .git
Git Commands
1. git
- Gives overview of git and about its usage
$ git
2. git config
- use it to change or display the git configuration setup
- below command lists all the configuration parameters
Git Commands
2. git config - alias continued
- use config to add alias in git
--> use "git last" and it works same as "git log -p -1"
3. git status
- It displays the status of the current directory.
- If the current directory/repository(repo) is enabled with git
tracking, it shows the status of the files under the directory.
$ git status
Git Commands
4. git init
- use it to make your current directory a git repo
- this command initializes current directory/folder as git repo and
starts tracking the directory.
$ git init
5. git add
- use it to enable tracking on files in the current directory
- it will move the files to the staging area, and the files become ready
to commit files.
- it is also used to resolve merge conflicts.
6. git commit
- use it to commit the changes (staged files) to the project repo (local)
- to take a snapshot of the changed file.
$ git commit
--> it will open the vim editor, where you can use it to edit the
commit message
Git Commands
6. git commit - continued
--> to commit without the staging, i.e., skipping the staging area
--> i.e., it will commit the changes directly, without adding them
to staging area.
--> Using this, you can skip stage area with only tracked file.
--> You can't skip stage area if your file is not tracked.
--> So, first track the file then directly commit the file with
appropriate message.
Git Commands
7. git log
- use it to get the history of commits performed on the project repo.
$ git log -p
--> this will show all the logs with the differences made via
each commit.
Git Commands
7. git log - continued
8. rm -rf .git
$ rm -rf .git
--> a linux command to remove(delete) file
- this command is used to delete .git file from your git repo.
- use it when you want to stop tracking your project repo.
- as .git stores all the commits and snapshots of tracking, removing
the file also removes all the tracking details of your project repo.
- After executing this command, your project repo will no more be a
git repo. Deadly command 😵, think before use!!
Git Commands
9. git clone
- to clone a repo from a git hosting website. ex- github/gitlab/etc.
$ git diff
--> it compares the files in staging area with the modified files in
working directory and shows the differences between the files.
--> if there are no modified files in the working directory and all
files are staged, then this command displays nothing.
--> This is because, there is no staged file, which is modifed.
Git Commands
11. ".gitignore" file
- to create ".gitignore" file in your project code, use below command-
$ touch .gitignore
- now add the file/folder names into the .gitignore file, which you do
not wish to track under your project repo.
- now you can check git status of your project repo, and then you will
find only .gitignore shows up as untracked, from the unwanted files
list.
- now use the command git add . (or) git add --a , to track your
.gitignore file
- and commit the file using git commit -m "Added .gitignore file"
Git Commands
11. ".gitignore" file -- continued
- if the files specified in the .gitignore file, are modified, then the
status of the modification will be shown in git status.
- but this must not happen as we do not want to track those files.
- so to overcome this, we have to explicitly untrack the particular file,
by using the next command.
$ git rm third.txt
--> this command not only deletes the specified file, but also adds
changes to the staging area (check by git status), which can be
directly committed.
Git Commands
14. git restore
$ git checkout -f
--> this command will rollback to previous commit changes of
your project repo, replacing all current modifications.
Note: the modified content can not be restored, after bringing the
previous changes in the file, if your file(s) is(are) not committed.
Git Commands
15. git checkout -- continued
$ git remote
--> it shows the names of the remote server added.
$ git remote -v
--> shows the name of remote server with website/url details.
Git Commands
17. git branch
$ git branch
--> this command will display the branches in the current git repo.
$ git branch -v
--> this will show the branches and the last commit details of
the branches.
Git Commands
17. git branch -- continued
- use below commands to delete the branch, which is usually done
when the feature/issue is closed.
--> it will give error if the specified branch is not merged to the
main branch
--> it will give Not give any error and delete the specified branch,
even if the specified branch is not merged to the main branch.
--> this command will push the changes done in the local project
git repo (currently working) to the remote repo under the master
branch.
--> it will show error if sufficient right access is not present.
--> "origin" is the target(remote) repo and "master" is the target
branch.
Git Commands
18. git push -- continued
to be continued ... 😎
Notes by Aishwarya Verma