Git Tips and Tricks

Let's become a Git master

1. Git Basics

(1) Start project management with Git : git init

  • Create a TIL folder to record what you learn going forward. Create this folder at the top level

  • After moving to the TIL folder in git bash, start git management with the following command

git init

(2) Staging for Commit : git add

  • Select files to take a snapshot of the current code state (== Add files to Staging Area)

git add [filename] # . uploads all changes to staging area

(3) Save snapshot for version management : git commit

  • commit the snapshot of the current state to proceed with version management.

(4) Add remote repository information : git remote

  • Create a Github remote repository and connect it with the TIL folder

  • Enter only when a new remote repository is added.

(5) Pull code to local repository git pull

  • Pull code from Github remote repository and apply commits for each branch.

(5) Push code to remote repository git push

  • Finally push to Github remote repository.

Tip

: If the current branch is the branch of the repo you want to push to, you only need to enter $ git push command!

(6) Other commands

  • Check current git status git status

  • Check version management history

  • git configuration (user.name & user.email) : Set once at the beginning

  • Undo git add before commit

2. README.md

A markdown document that records information about remote repository. Generally describes how to use the project.

(1) Create README.md file

  • Create README.md file in the TIL folder (top level). The name must be set to README.md.

(2) Add simple content about (your own) principles

  • Write the README.md file based on what you learned and practiced in the markdown writing pdf.

  • Write freely in format but follow markdown syntax (semantic).

(3) Save and version management : add, commit, push

  • When writing is complete, leave commit history and push to remote repository with the following commands.

3. Shared Repository

Fast forward merge practice

4. Go back to specific point

Go back based on specific commit

Create test branch and move

Check reference log

Check where head is

Go back to test branch

5. Revert commits uploaded to remote repository

Method 1) Revert commit locally then force Push

Revert commit with $ git reset

Force Push with -f or --force command

Method 2) Use git revert

  • Method 1 has the problem of forcibly manipulating remote repository commit history

  • but, using git revert treats the work of reverting commits as one commit and adds it to commit history

Create another commit that removes changes from specific commit

+

  • Content in master branch is automatically deployed

    • master is in deployable state

Clone only specific branch

Get specific branch from remote repository

First check what branches exist in remote repository

Check remote repository + local branches

Get branch from remote repository with same name

Get branch from remote repository with name change

Modify unpushed Commit message

git fetch

Can check how far remote branch has gone

  • git pull == git fetch && merge

git rebase

Reposition

  • Adding -i at the end allows interactive execution

6. Using Submodule

What is Submodule?

  • Having another sub repo inside Git main repo

    • main repo does not manage sub repo!

  • Using Submodule allows sharing commonly used logic between projects!

Add Submodule

  • After this, you can commit without separate git add

Update Submodule

Update main repo

  • Running the following command at main repo root updates main repo

Update sub repo

  • Running the above command at main repo root updates sub repo according to the relationship between updated main repo and sub repo

Last updated