Giter VIP home page Giter VIP logo

github_demo's Introduction

github_demo

A simple demo repository to show the basic Git workflow

git commands

git status ; check your git status in 3 staging area(local, stage, .git-repository) => remote
git init ; you can initialize git in your existing local folders or new folders
git add ; you can add the untracted files by git to staging area and preferbly specifying file name

  • opt
  • -a: adding at the stage but only works for tracked files by git
  • .: adding all files recursively all the different levels
    git commit ; you can see which branch you are in and unique SHA-1 commit id after commiting
    git push (remote_branch_that_we_cloned_from) (remote_branch_name) ; always push from local to remote
    git ls-files : list tracking files in git
    git reset HEAD file_name: you can reset staged changes to untracked changes
    git mv: you can update file changes or file location status including delete and new file status at the staged status simultaneously
    git log:

Basic commands order (add=>commit=>pull=>push)

git add -A or filename (avoid using .)
git commit
git pull origin main(or your branch): to avoid conflit when you share one repo with other developers
git push

Restore commands order for staged area(0=>add=>reset=>restore=>0)

git add file_name
git reset HEAD file_name
git restore file_name (or checkout file_name)

Commit message fix

git commit
git commit --amend
-- reopen and compose commit message again

how to rename filename and moving file in git

git mv file_name new_file_name

if you use bash mv commands

mv file_name new_file_name
git add -A : you need to use -A option since add command only track new file and ignore deleted old_file status

if you rename by GUI operations

git add new_file_name (: better to avoing using git add -A because your methods(GUI) will be remained or you can add the string to .gitignore)
git add -u (: to make sure git understand we are just renaming file, changed_file)

how to delete files in git

if you haven't add file at staged area

rm file_name

if you added file at staged area

git rm file_name (the files you can see with "git ls-files" commands)

if you tracked files by git removed by mistakes

git reset HEAD file_name (or)
git restore file_name (I personally prefer this one tho teacher used the one above because I don't want to use reset)

want to include delete status at staged area and commit

git add -A
git commit

how to have fun with log commands

abbreviate commit ID

git log --abbrev-commit

see clear and neat

git log --oneline --graph --decorate

see specific logs

git log commit_newer_ID...commit_older_ID

specify the date(like 3 days ago)

git log --since="3 days ago"

check the rename history for file

git log --follow -- file_name

check the diff the specific commit

git show commit_ID

how to compare files

compare local working dir vs staged area

git diff
git difftool

compare local working dir vs local repository

git diff HEAD
git difftool HEAD

compare staged area vs local repository

git diff --staged
git difftool --staged

compare specific file

git diff -- (path)file_name
git difftool -- (path)file_name

compare commits

git diff commit_id commit_other_id
git difftool commit_id commit_other_id (HEAD: latest commit on the current branch)

compare local repo vs remote repo

git diff local_branch_name origin/remote_branch_name
git difftool local_branch_name origin/remote_branch_name (origin: refer to remote)

compare local repo1 vs local repo2

git diff local_branch1 local_branch2
git difftool local_branch1 local_branch2

Branch

show all branch(includes remote branches)

git branch -a

create new branch

git branch new_branch_name

change the current branch

git checkout your_branch

change branch name

git branch -m branch_name new_branch_name

delete branch

git branch -d branch_name (not allowed when you are there)

create new branch and mv at the same time

git checkout -b branch_name

Merge

Fast Forward Branch Merge: only works when target branch does not have any changes from your current branch

After commiting--
git checkout main_branch
git merge target_branch
git log --oneline --decorate --graph : check if the current commit points target_branch & merged_branch

None Fast Forward Branch Merge: commits in target_branch will be different but merge_branch will integrate and merge all commits at the same time

After commitng--
git checkout main_branch
git merge target_branch --no-ff
git log --oneline --decorate --graph : check if the current commit integrates all commits in merge_branch

Automatic Merge:

After commiting at target_branch--
After different commiting at main_branch--
git merge target_branch : eventually main_branch will merge unless merge does not have conflicts (changes in same files) because main is always prioritized

Conflict Merge Resolution: when conflict happens is when multipul branches have different changes at the same time

After commiting changes for file1 at target_branch--
After commiting changes for file1 at main_branch--
git checkout main
git merge target_branch : Conflicts happens => how to resolve below
git mergetool: pick up the choices you have and save
git commit -m "Resolved Merge conflicts"
git status
Interestingly, git will automatically create .orig file after resolving conflicts to backup! Therefore, lots of people add "*.orig" in .gitignore

how to rebase

Normal case1(rebase from above_branch): fundamental reasone we use this is for when we want to continue developing (like creating codes) at proceeding branch but some changes happen to main or branch above your developing branch. Example below.

git checkout -b your_dev_branch
-- keep commiting and want to continue tasks here
-- but someone edited and commited branch above your_dev_branch directly
-- in this case, you should use them
git rebase main (above_branch)

Rebase Conflicts(if you want to leave rebase mode)

git rebase --abort

Rebase Conflicts(if you want to fix conflicts)

git rebase main
git mergetool
(git add file_name)
git rebase --continue
(git rebase --skip)
git log --oneline --graph --decorate --all

Rebase remote branches

-- publish commiting at local
-- publish commiting at remote
git fetch origin main : you need to update remote status at local git
git status
=> will say "Your branch and 'origin/main' have diverged
git pull --rebase origin main

how to stash

Normal case (when you are not ready to commit your changes in the current state and allow you to change gear and work on something else)

-- modified file 1
git stash (save)
git status
=> your modified untracked file is gone
-- modified another file but you want to get back the stash state above 2
git stash apply : this allow you to go back the state including the second modifications you saved with stash commands and see status too
-- modified adding changes to modification1 and complete
git commit
git stash list : list all stash you saved in git
git stash drop : you can drop the last unneccesary stash record after applying

Stash Untracked / Pop

git ls-files
-- modification tracking_file.txt in ls-files
vim non-trackedfile.txt
git status
=> shows modified state tracking_file.txt & Untracked files state, non-trackedfile.txt
git stash
=> only works for tracking_file.txt
-- clean up stash
git stash list
git stash drop
-- but you want to stash including Untracked files
git stash -u : you can stash Ubntracked files too excluding files specified in .gitignore
-- another new modification
git stash pop: integrated commands (git stash apply & drop)

Multiple Stashes

-- if you want to stash several times, you should use this commands to identify each stashes well
git stash save "Message"
git stash list
git show stash@{stack_num} : you can see what the chosen stash is
git stash apply stash@{stack_num} : you can stash apply for specific stash
git stash clear : you can stash all stash_lists at the same time

Stashing into Branches: when you avoid commiting current changes on main or your current branches

-- a bit messed up changes (like staged:1 tracked_file:2 Untracked_file:1 on main) and you want to avoid commiting at main branch
git stash save -u: stash branch commands requires existing stash beforehand
git stash branch new_branch_name

how to tag in git

Lightweigh: tag allows us to refer specific commits

git tag tag_name : Creating tag at latest commit
git tag --list : show list of tag
git show tag_name : works with tag_name commit
git tag --delete tag_name : delete tag_name tag

Annotated tags: Lightweigh tag + extra info(messages)

git tag -a tag_name (or git tag tag_name -m "Message")
-- open editor to add messages for this tag
git tag --list
git log --oneline --decorate --graph --all => same as Lightweight
git show tag_name => you can see the message

Comparision between tags

git diff tag_name1 tag_name2
git difftool tag_name1 tag_name2

Taggin Previous Commits

git log --oneline --decorate --graph --all
-- copy commit_id
git tag -a tag_name commit_id

Update Tagging (for same tag_name)

git tag -a same_tag_name -f commit_id

Using Tag with Github

git push origin target_branch tag_name
-- In Github we can see at Releases (tags) and you can download zip or tar.gz file at that commit sources
-- want to push all tags at one time in remote repo
git push origin target_branch --tags
-- want to delete tags after pushing remote repo
git push origin :tag_name

Reset & Reflog & Revert : Unfortunately, teacher does not explain well here

Reset soft: when you commited mistakingly in local

git reset HEAD^(num): only undo the commit and it is more like your state will be movable to specific commit but in the staged area and local workdir do not have any changes
git reset reflog_style(HEAD@num)

Revert: when you pushed commit mistakingly in remote

git revert commit_id

Reflog

git reflog : show logs of everything we've done including HEAD changes of resets or anything like that

git config_file

.gitconfig is our config file for git setting
my commands for git alias setting lists below

git config --global alias.ss status
git config --global alias.ls ls-files
git config --global alias.sh stash
git config --global alias.shls "stash list"
git config --global alias.shap "stash apply"
git config --global alias.shp "stash pop"
git config --global alias.shd "stash drop"
git config --global alias.shcl "stash clear"
git config --global alias.shbr "stash branch"
git config --global alias.tg tag
git config --global alias.tga "tag -a"
git config --global alias.tgls "tag --list"
git config --global alias.tgd "tag --delete"
git config --global alias.br branch
git config --global alias.bra "branch -a"
git config --global alias.brm "branch -m"
git config --global alias.brd "branch -d"
git config --global alias.brdd "branch -D"
git config --global alias.co checkout
git config --global alias.cob "checkout -b"
git config --global alias.adu "add -u"
git config --global alias.adup "add -u -p"
git config --global alias.com commit
git config --global alias.coma "commit --amend"
git config --global alias.mg "merge --no-ff"
git config --global alias.mgff "merge --ff"
git config --global alias.rest restore
git config --global alias.cp cherry-pick
git config --global alias.log1 "log -1"
git config --global alias.log2 "log -2"
git config --global alias.log3 "log -3"
git config --global alias.logo "log --oneline"
git config --global alias.hist "log --oneline --graph --decorate --all"
git config --global alias.logn "log --name-status --oneline"
git config --global alias.rl reflog
git config --global alias.firstcom "commit --allow-empty -m "Initial commit""
git config --global alias.dtool "difftool"
git config --global alias.mtool "mergetool"

memo

git status can track consistently for the same file like mod1, mod2

github_demo's People

Contributors

catakaevy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.