Chapter 1 introduction to git
Chapter 1 introduction to git
App for :
1. Sleep, exercise
2. Manage the activities well
Adding feature :
1. Count calories and help them to manage their activities accordingly
If, at any point of time in the project development process, you want to go back to the previous version of
your project or want to revisit an older version, the version control tool can do that for you.
- The local version control system is simple and fast because no central repository needs
to be accessed to save the revisions.
- Advantages :
1. All the people working on the project can easily identify who all are working on the project
currently.
2. The administrators have full control over the access a user can have to a project and decide
who all can alter the project.
1. The system depends a lot on the central servers. If the server goes down, the project
suffers because no activity can take place.
2. If the hard disk on which the central server is located is corrupted and there is no backup,
you lose all the data.
4. The commits are made remotely and are, consequently, very slow
- In a distributed version control system, instead of copying the latest snapshot of the project files,
the entire project is copied so that if the server fails, each client has his/her own copy of the
project and any client’s project can be copied to restore the server.
- This type of version control is faster than the central version control system because it does not
need to access a remote server but the local hard drive itself.
- Advantages :
1. You need to be online only when sharing changes. You can otherwise make changes to
your local repository working offline.
2. Each working copy is in itself a backup because the entire project is copied to the local
system.
3. There can be no unwarranted changes because each change request is merged by the
project owner.
- Many projects use Git for version control of their projects, which includes both open-source and
commercial projects.
- Git has become a favorite version control system because of the following features:
1. Work Offline:
- Git gives you the freedom to decide where and when you want to work. You do not need
to worry about Internet connectivity to work on the project.
all on your local machine. You just need Internet connectivity when you want to
collaborate with other developers on Git.
2. Security :
- Git stores everything in its database using a secure hashing algorithm called SHA-1.
- This prevents accidental and malicious changes in the repositories and ensures that the
history of project files is traceable.
- This ensures that you have an authentic history of your source code.
3. Flexibility :
- Git is a very flexible tool to use. It supports both large projects as well as small projects.
- Git gives you the freedom to experiment with your project and try new ideas. Git is
compatible with many existing systems and protocols and lets you to work with them
4. Speed
- Git is one of the fastest version control systems in the market. Fetching data from the local
system’s hard drive is a lot faster than fetching it from a remote server.
- This makes Git fast to use. Recording changes, viewing history, and comparing files are all
super easy and fast with Git.
CONFIGURING GIT :
- https://git-scm.com/download
- The platforms supported by Git are
1. Linux,
2. Windows,
3. MAC OS X,
4. Solaris
Configuring :
1. Set up email adress
git config -global user.email "your email"
2. Set up username
git config -global user.name "your name"
CREATING REPOSITORY
- A repository is like a folder where Git stores all the versions and metadata of the project.
- Git creates a hidden folder named .git in the root directory of the project. The root directory is the
directory at the highest hierarchical level. For example, for Windows, it is C:\ drive.
- To create an empty Git repository or to make an existing repository into a Git repository, the following
command is used
git init
- When you run the git init command, Git creates a new subdirectory named .git. The .git subdirectory in
the root folder of the project is where all the metadata of the project is stored.
- By typing the git init command, you convert a folder into a Git repository.
- This is the first command that is used before we begin working on a new project because other Git
commands are not available outside the Git repository.
- This can be done at any time and no Internet connection is required for the same. The .git directory is a
hidden folder.
Awal :
Setelah command tersebut (membuat folder teest5 dan invisible folder .git :
Kalau repository atau directorynya merupakan directory yang memang langsung ingin digunakan sebagai git
directory, maka git init saja
- These may include files that are automatically created such as the
1. temporary files,
2. log files,
3. cache files, and
4. files that contain personal configuration and passwords.
Git allows you to create a file called “.gitignore” in the root folder of the project. The .gitignore file
specifies all the untracked files that you want Git to ignore. For example, if you want Git to ignore all
the .log and .tmp files, you need to create a .gitignore file in the Git repository with the following lines:
*.log
*.tmp
- Each line in a .gitignore file specifies a pattern. The list of files to ignore should be defined at the very
beginning,
- before the first commit is made; otherwise, if the commit is already made, you will have to first untrack
the files and then add them to the .gitignore file.
THE WORKFLOW
- A typical Git workflow is that in which
1. you make changes in your working directory
2. and then push the changes in the staging area, which acts as a checkpoint before the changes can
be pushed to the local repository
3. by committing the changes.
- After the changes have been committed, the repository can be cloned and copied by other remote
users. You can then start working on the repository. The Git workflow can be depicted as shown in the
following figure
- Staging Area:
It is a temporary area where the changes stay before they can be committed to the local repository.
- Local Repository:
The .git repository inside the working directory is the local repository.
- Remote Repository:
It is a version of your repository that is hosted on a server from where users can access it.
- Untracked files
are the rest of the files which were not in the last screenshots. To save the files and track them you
need to stage and then commit the changes made in the file.
STAGING (INDEXING)
Staging (Indexing) In most version control tools, there are only two areas where the changes made by
you are stored,
- the working area
- the database.
However, in Git, there is an intermediate area where the changes are stored before they can be
pushed from the working area to the database.
This area is called the staging or index area for all the changes before they can be committed and
pushed to the database.
- It lets you to work efficiently with large changes by splitting them into smaller, more manageable
changes.
- Before you can commit a change, Git provides you the opportunity to store the changes in the
staging or index area, where you can review them before committing them.
- It lets you control the files you want to commit and those that you want to exclude.
- The staging area helps wrap changes that belong together in a single commit. This helps in the
easy identification and organization of the changes made.
- The git add command is used to add a file from the working directory to the staging area
before a commit can be made. The git add command does not make any significant changes to
the repository; it just pushes the file to the staging area.
COMMITING
Changes are not recorded in the local repository or the database unless they are not committed. The git
commit command commits snapshots of the version history. To commit the changes, you need to run the
following command:
git commit
INSPECTING A REPOSITORY
git status
git status -s
- Melihat commit id
git log --oneline
(HEAD -> master), artinya adalah commit terakhir atau paling baru dilakukan
BRANCHING IN GIT
- The git branch command is used to not only create but also to
• list,
• delete, and
• rename branches.
3. Checkout branch
- git checkout <branch name>
- git checkout -b <branch name> (membuat branch baru dan langsung masuk ke dalamnya)
4. Merging Branch
- git merge command. The target branch is merged with the current branch, called the HEAD
branch, And no changes appear in the target branch.
- It is good practice to remove an obsolete target branch after it has been merged with the
current working branch because it keeps the main branch clean and tidy.
Conflict
▪ If you try to merge two branches that have altered the same files, Git won’t be able to
figure out which branch version to consider.
▪ In such scenarios, where conflicts occur, Git stops and asks you to resolve the conflicts
manually
- You can resolve a merge conflict by using the simple Git workflow commands status, add, and
commit.
1. By running the git status command, you can figure out the files where the conflicts
have occurred.
3. After you have fixed the conflict, you can run the git add command to tell Git that the
issue has been resolved.
4. You can then run the git commit command to generate the merge commit.
- Antar branches
git diff branch_a branch_b
6. Delete branches
git branch -d <nama branch>
Git branch -d <nama branch> // untuk menhapus secara paksa
UNDOING CHANGES
You may make commits that you want to undo. Git gives you the option to undo the changes made by you in
your project. The following commands can be used to make an undo in Git:
- Git checkout
- Git reset
- Git revert
Git checkout
- git checkout While working on a project, you may want to go back to a previous version of your
project. Git allows you to do so. To visit a previous version or to move to a previous commit, you
need to run the following command:
git checkout
- When you check out a commit, the HEAD, instead of pointing to the most recent commit, points
to the commit specified by you.
- This state when the HEAD is not pointing to the latest commit is called a detached HEAD state.
You can add, delete, and edit files but nothing will reflect the most-recent state of the repository.
MENGAMBIL VERSI FILE PADA VERSI COMMIT SEBELUMNYA SAJA, CURRENT DIRECTORYNYA
SAMA, HANYA FILE TERPILIH SAJA YANG KEMBALI KE MASA LALU ATAU KE PAST COMMIT YANG
KITA PILIH :
Unlike checking out only the commit, checking out a file affects the current state of the
repository. The older version appears as a modified file. We can revert to the previous version by
committing the file
GIT REVERT
- Undo commit, The git revert command is used to undo the changes made. Instead of deleting the
latest version with the commit, it makes a new commit with the same state as the version you want.
git reset
Running the above command resets the staging area to match the recent commit. It does not make any
changes to the working directory.
To reset the staging as well as the working directory to match them to the most recent commit, the following
command is used:
The --hard flag tells Git to overwrite the working directory as well.
EXTRAS =
1. SEMUA BRANCHES BISA BACA STAGING AREA, TETAPI SEMUA COMMIT BRANCHES HANYA BISA DI
BACA SESUAI DENGAN COMMIT YANG TELAH TERJADI PADA BRANCH TERSEBUT, ATAU KETIKA
SUATU BRANCH DITARIK LAGI UNTUK MEMBUAT BRANCH BARU
2. KETIKA BERADA DI SUATU BRANCH DAN MEMBUAT BRANCH BARU, MAKA OTOMATIS BRANCH
BARU AKAN TERLAHIR DARI CURRENT BRANCH
3. JIKA ADA MODIFIED FILE SEDANGKAN FILE TERSEBUT BERADA DI STAGED AREA (TELAH DI ADD)
MAKA NANTINYA JIKA FILE DI COMMIT TANPA ADD ULANG, MAKA PERUBAHAN TERSEBUT TIDAK
AKAN BISA DI TRACK