GIT

ONLY PUT SOURCE FILES INTO VERSION CONTROL, NEVER GENERATED FILES

git init projectName
cd projectName

# credential
git config --global user.name "User"
git config --global user.email "user@user.com"

# staging
git add .

# write to history
git commit -m "message"

# branches [-c:copy, -m:move, -d:delete]
git branch -c my-branch
# checkout to branch
git checkout my-branch

# merge with current branch
git merge my-branch

# remote
git remote add vue https://github.com/vuejs/vue.git

# Changes
# stashes - create tmp records of WIP to return
git stash
git stash list
git stash apply stash@{0}

# cancel all current stage changes
git reset
# revert to commit ID
git revert fc95eb...


Example of gitignore

__pycache__
venv
env
.pytest_cache
.coverage

Checkouting paste SHA or label

git checkout 946b99.... (20-bit)
Note: checking out....

You are in 'detached HEAD' state. 

If you want to creat ea new branch to retain commits you create, you may do so (now or later) by using -b with the checkout cmd e.g.:

	git checkout -b <new-branch-name>
	
HEAD is now at 956b99b... added myname module

Commit

git commit -a - auto-stage all tracked files (modified tracked files not staged)

git difftool - use difftool to diff

git diff --staged

git rm

git rm --cached - keep file in working tree but remove it from staging

git rm log/\*.log - \* due to Git does own filename expansion in addition to shell’s filename expansion, removing all .log extension in log/ dir

git mv old_name new_name - combines mv old new; git rm old; git add new;

Log

git log

-p / --patch

--pretty=oneline, short, full, fuller

--pretty=format:"<PATTERN>"

%H, %h, %T (tree hash), %t, %P, %p, %an, %ae, %ar, %ad, %cn, %ce, %cr, %cd, %s

git log -S function_name

--since, --after, --until, --before, --author, --committer, --grep

Undoing

git commit --amend - replace previous commit with new staged

git reset HEAD FILE_2_UNSTAGE - unstage file

git checkout -- FILE_2_UNMODIFIEd - revert to original (last commit, initially cloned, or however got into working tree) this is dangerous - any local changes are gone!

Tag

git tag -a v1.4 -m "detail of 1.4”

git tag

git tag show v1.4

git tag -a v1.2 9fceb02 - tag a commit afterwards

git push origin v1.5 - sharing tag to remote branch

git push origin --tags - pushing all tags to remote server

git tag -d v1.5 - delete tag

git push origin :refs/tags/v1.5 - null value before colon is being pushed to remote tag name, effectively deleting it

git push origin --delete <tagname> - more intuitively

git checkout 2.0.0 - view tags

git checkout -b version2 v2.0.0 - version2 branch will be slightly diff than v2.0.0 tag since it will move forawrd with new changes so do be careful

Alias

git config --global alias.co checkout

git config --global alias.unstage 'reset HEAD --'

git config --global alias.last 'log -1 HEAD'

Branching

git log --oneline --decorate -graph --all - shows divergence

CASE:

  1. do some work on web
  2. make branch for new user story
  3. do some work in branch
  4. critical fix
    1. switch to PROD branch
    2. make branch to add fix
    3. after tested, merge fix and push to PROD
    4. switch back to original user story and resume

MANAGEMENT

Rebasing

Branching

git checkout -b new_feature

Example of Comparison:

# the label repr
* [new_feature] commit 4
! [master] commit 3
--
* [new_feature] commit 4
* [new_feature^] commit 1
* [new_feature~2] added code for featur ex
+ [master] commit 3
+ [master^] commit 2
*+ [new_feature~3] created .gitignore

# SHA repr
git show-branch --sha1-name new_feature master

3 Ways to Branch In

Merge

Rebasing

Cherry-Picking

Remote Repo

clone / fetch / pull / push

clone is simple -> Download

git remote show origin

git fetch <remote>

Fetch

Pull

Simple Git Workflow

  1. git status make sure current area is clean
  2. git pull get latest version from remote - saving merging issues later
  3. Edit files and make changes - test and linter
  4. git status find all files changed - make sure watch untracked files too
  5. git add [files] add the changed files to the staging area
  6. git commit -m "message" make new commit
  7. git push origin [branch-name] push changes up remote

Pro Git

https://realpython.com/advanced-git-for-pythonistas/


code · notebook · prose · gallery · qui et quoi? · main