Skip to content

Git overview

Flowchart

git_flow

Undo Things

undoing things

Remote-tracking Branch

remote tracking branch

git init

Initiate local repo

cd /Users/joy/Documents/R
git init

git add

Track file/add to stage

git add Git.Rmd
git add .

git commit

Commit staged files and edit message in editor
git commit
Commit staged files and edit short message
git commit -m 'enter message here'
Stage and commit at the same time
git commit -a -m 'message'
git commit -am 'message'

git status

Check git status
git status

git diff

Check file edit details (unstaged changes)
git diff
Check file edit details (between staged one and committed one)
git diff --staged

git log

Check history commit
git log
git log --pretty=oneline 
git log --pretty=format:"%h-%an, %ar:%s" 
git log --graph
git log --graph --all
  • %h simple hash
  • %an author
  • %ar edit date (from now)
  • %ad edit date
  • %s message

Check part of the log history
git log featureA..origin/featureA
show the commit list included in the later branch (origin/featureA) but not in the former (featureA)

git clone

Create bare repository
git clone --bare /Users/joy/Documents/R R.git
Clone the entire repo
git clone https://github.com/JoyDing0330/Rnote.git
Cloning to a specific folder
git clone <repo> <directory>
git clone ssh://john@example.com/path/to/my-project.git my-project

git push

Push to main branch
git push origin main
Push to feature1 branch
git push origin feature1
Push local feature1 branch to remote feature2 branch in origin repo
git push origin feature1:feature2
Push the locally new-created branch featureA to remote origin
git push -u origin featureA
git push --set-upstream origin featureA
Push the locally new-created branch featureB to the branch featureBee on remote origin
git push -u origin featureB:featureBee
git push remote_name local_branch:remote_branch
Delete remote branch
git push origin --delete serverfix

git checkout

Change to feature1 branch
git checkout feature1
git switch feature1
Create branch and switch to that branch
git checkout -b feature2
Create a local branch named serverfix from remote origin/feature1
git checkout -b serverfix origin/feature```

## `git fetch`

``` git title='Fetch the master branch from remote to local origin/mymaster'
git fetch origin master:refs/remotes/origin/mymaster
Fetch multiple branches
fit fetch origin master:refs/remotes/origin/mymaster \ topic:refs/remotes/origin/topic
Fetch all of the tracked branches
git fetch --all

git rebase

Rebase the topicbranch to the basebranch
git rebase <basebranch> <topicbranch>

Or

Rebase the topicbranch to the basebranch
git checkout <topicbranch>
git rebace <basebranch>

git reset

head # this commit
head~ # last commit
head~2 # the one before last commit
Cancel the commit but the file is still stages (soft)
git reset head~ --soft

Cancel the commit and the file is not staged
git reset head~
Cancel the commit and remove the edits
git reset head~ --hard

git stash

Save the unfinished work and keep the working directory clean to checkout to another branch
git stash
Restore the saved work
git stash apply`
Get the store list, {0} is the last one
git stash list
Get the previous save thing
git stash apply stash@{2}
Drop a stash record
get stash drop stash@{0}

git sqush

git checkout -b featureBV2 origin/master
git merge --squash featureB

.gitignore

ignore all except one folder/file
*
!folder
!folder/*

Show current local repository

git rev-parse --show-toplevel

Nano Editing

  • Ctrl+O or i enter message
  • Ctrl+X or Ese:wq exit
  • Ctrl+C cancel

Branches

  • The refs for local branches are stored in the ./.git/refs/heads/
  • Remote branch refs live in the ./.git/refs/remotes/
  • To view your remote branches, simply pass the -r flag to the git branch command. git branch -r
  • If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> add_line