A simple demo repository to show the basic Git workflow
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:
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
git add file_name
git reset HEAD file_name
git restore file_name (or checkout file_name)
git commit
git commit --amend
-- reopen and compose commit message again
git mv file_name new_file_name
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
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)
rm file_name
git rm file_name (the files you can see with "git ls-files" commands)
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)
git add -A
git commit
git log --abbrev-commit
git log --oneline --graph --decorate
git log commit_newer_ID...commit_older_ID
git log --since="3 days ago"
git log --follow -- file_name
git show commit_ID
git diff
git difftool
git diff HEAD
git difftool HEAD
git diff --staged
git difftool --staged
git diff -- (path)file_name
git difftool -- (path)file_name
git diff commit_id commit_other_id
git difftool commit_id commit_other_id (HEAD: latest commit on the current branch)
git diff local_branch_name origin/remote_branch_name
git difftool local_branch_name origin/remote_branch_name (origin: refer to remote)
git diff local_branch1 local_branch2
git difftool local_branch1 local_branch2
git branch -a
git branch new_branch_name
git checkout your_branch
git branch -m branch_name new_branch_name
git branch -d branch_name (not allowed when you are there)
git checkout -b branch_name
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
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
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)
git rebase --abort
git rebase main
git mergetool
(git add file_name)
git rebase --continue
(git rebase --skip)
git log --oneline --graph --decorate --all
-- 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
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
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)
-- 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
-- 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
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
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
git diff tag_name1 tag_name2
git difftool tag_name1 tag_name2
git log --oneline --decorate --graph --all
-- copy commit_id
git tag -a tag_name commit_id
git tag -a same_tag_name -f commit_id
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
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)
git revert commit_id
git reflog : show logs of everything we've done including HEAD changes of resets or anything like that
.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"
git status can track consistently for the same file like mod1, mod2