[TIL] GIT Review

03/25/23

·

7 min read

[TIL] GIT Review

Commit, Remote Repo, and .gitignore

Git & GitHub Structure

Git vs GitHub

  • Git is a version control system that lets you manage and keep track of your source code history.

  • GitHub is a cloud-based hosting service that lets you manage Git repositories.

Three Sections Managed by Git

  1. Working Directory - the directory that you are currently working on

  2. Staging Area - the temporary storage of the files that will be committed, or the space of files that are subject to track

  3. Local Repository - the storage managed by each user's computer => all committed contents from the Working Directory will be saved as snapshots

Why Should We Use Git?

  1. Version Control

    : can view history and control the versions of a single file

  2. Dividing the Work Units

    : can easily keep track of history to find which section occurred errors by saving the work units

  3. Collaboration with Other Developers

    : can work separately and easily combine them (we are able to find who, when, and what part is changed)

Why Should We Use GitHub?

  1. Space to save Git projects with convenient tools to manage Git

  2. Tools for collaboration

  3. A high community

  4. Contribution to new projects or existing projects

What is SourceTree?

  • CLI: using Git by entering commands on Terminal

  • SourceTree: a tool to use Git easily

Commit

Version Control: Save three pieces of information: who committed, when committed, the status of the current project

Shows all committed history and which part is changed

Getting Started with Git

Initialize -> Add(staging) -> Commit

  • Initialization: set the project (folder) as it will be managed by Git

  • Add or Staging: select files that are subject to commit

  • Commit: it is important to write the proper commit message for viewing the commit history later

Remote Repo

  • Tracking (branch tracking): local repo is connected to the remote repo

  • Only the local repo stores the info on which remote repo is connected to it

  • Remote repo does save commits manually

    Git Push - javatpoint

  • Push: reflecting the commits of the local repo to the remote repo

  • Pull: reflecting the commits of the remote repo to the local repo

Git Clone - How To Use Git Clone | W3Docs Git Online Tutorial

  • Clone: downloading the remote repo on my local computer

Order to Manage Project

Pull -> Local repo commit -> Push

  • Conflict: when the local repo and remote repo change the same file

    • How to avoid Conflicts?

      1. PULL: Set the status of the local repo and remote repo the same, which means 'pull' from the remote repo to the local repo

      2. COMMIT: Save the local repo

      3. PUSH: Reflect the local repo to the remote repo

.gitignore

: excludes specific files to be committed

  • Specific files can be

    • files typically made up of automatically generated files on the OS

    • files that should not be publicly exposed for security reasons

    • unnecessary files in collaborative work

  • You should be careful not to commit files that contain sensitive information such as ssh keys, IAM pems, various account information, or environment variable setup files.

  • .gitignore generator for each framework


Collaboration, Merge, and Branch

How to Collaborate with Other Developers

  1. Issue: decide who will carry out each task

  2. Branch: work on what each person is responsible for

  3. Merge: merge individual works into the project

  4. Sometimes, review the work before finally integrating it into the project - Merge after Pull Request

Issue

: issues that have to be resolved in the project

Types of Issue

  1. Bug Report

    • "There seems to be a bug in the sign-up feature. I'll register an issue for it."
  2. Feature Add or Project Enhancement

    • "It would be great if we could make this button more visible. I'll register an issue for it."
  3. Work Unit to Resolve Problems

    • "I'll take care of issue number 6. Please assign it to me!"

  • Include the "issue number" in the commit message

    feature changed #1

Project Board

Under Projects in each repo, we can manage issues (just like Jira).

  • Issues: upload and stack issues

  • To Do: drag and drop issues that are going to begin working on

  • In Progress: drag and drop issues that are currently in progress

  • Done: drag and drop issues that are done -> Click the "Close Issue" button

Milestone

To manage Sprint: set a due date and show the percentage of complete, open, and closed issues

  1. Create Milestone

  2. Add issues (either newly created issues or existing issues)

  3. Close when Milestone Due Date is passed

  • Other Issue Tracking Platforms: Jira, Trello, etc

Branch

: work on what each person is responsible for

Git Branches: List, Create, Switch to, Merge, Push, & Delete

  • Branch Name Convention

    • feature/issueNum_name: feature folder is used for the feature development

Creating Branch

  1. Create a branch following the branch name convention

  2. Checkout: selecting the current working branch

  • main: main branch of the local repo

  • feature/2_jjigae: feature/2_jjigae branch of the local repo

  • origin/main: main branch of origin (connected remote repo)

  • origin/HEAD: recent commit of currently working commit. origin (remote repo)

Deleting Branch

  • The branch that will be deleted should not be the current branch (not checked-out)

Merge

: merge specific branches into the one branch

  • The default setting will include all commits under branches that are trying to merge

  • Each branch should change different files, if not, it will occur Merge Conflict.

Merge Conflict

: it is more important to fix bugs than not occur errors

  • When multiple branches made changes on the same file and try to merge into one branch, it occurs merge conflict.

  • Git shows where conflict occurred: <<<<<<< conflicted part >>>>>>>

      <<<<<<< HEAD
      {main branch (merged branch) content}
      =======
      {conflicting branch content}
      >>>>>>> conflicting branch name or commit id
    

How to fix Merge Conflict

  1. Fix file

  2. Delete <<<<<<< HEAD, =======, >>>>>>>conflicting branch name or commit id

  3. Commit fixed file

Git Flow

  • Flow: Managing method (commit ~ branch)

  • github-flow, gitlab-flow, git-flow

  • Web Development Project: github-flow

Remote Repo and Branch

  • Tracking: connecting the branch of the local repo (main) and a specific branch of the remote repo (origin/main)

Git Command

  • Register User

git config --global user.name "SIWON KIM"
git config --global user.email"siwonkim1108@gmail.com"
  • Initialize the local repo

git init

# you can view .git folder under the project directory
# .git stores the meta data regarding the repo

  • View the repo status

git status

  • Track the file

git add <file name>
# track <file name>

git add -A
# track the whole project

  • Untrack the file

git rm --cached <file name>

  • Reflect the source to the local repo

git commit -m "<message>"
# <message> is required
# with -m option, you can write message and commit simutaneously

git commit -a -m "<message>"
# with -a option, you can commit without doing 'add' again on tracked file
# i.e. skip add
# Warning: only worked for the file that is already on Staging Area

  • View the log

git log
# to escape log page: enter qgit diff
# with --oneline option, you can get a log line by line

  • View the source git checkout -b dev1changes

git diff

  • Set/Checkout/Change branch

git checkout -b dev1
# with -b option, it creates the new branch and move to dev1 branch

git checkout main
# change to main branch

  • Merge branch

git merge dev1
# merge dev1 into main

  • Set the remote repo

git remote add origin https://github.com/ggingmin/test.git
# git remote add [remote repo name] [remote repo url]

  • Upload the project to the remote repo

git push -u origin main
# git push -u [remote repo name] [branch name]
# with -u option, we can do only do git push when push to remote repo and branch later on

  • Bring the remote repo project to the local repo

git fetch
# bring contents of remote repo to the local repo
# but, local repo is just stored in my computer, it is not the Working Directory
# thus, we are not able to work or modify by using fetch
# remote repo -> local repo

git pull
# bring contents of remote repo to the local repo
# also, merge to Working Directory
# remote repo -> local repo -> working directory