Happy Happy to Study Git - MRLIVING/Becca GitHub Wiki

Concept

The target of a repository

  • A folder which includes files and sub-folders

Local and Remote repository - A typical model

  • local repository could be located under various account, instance and folder.

A typical working flow

  • HEAD is the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be.
  • master is a name commonly given to the main branch
  • origin is a name commonly given to the main remote. remote is another repository that you can pull from and push to. Usually it's on some server, like github.

git status
git add ${FILENAME}
git commit
git push
git log -${N} --oneline
git branch

Create a repo in local and sync to a specific URL remote

git init
git checkout -b '$BRANCH_NAME'
git add ...
git commit -m ...
git push

git remote add $REPO_NAME $REPO_URL
git push --set-upstream $REPO_NAME $BRANCH_NAME

Clone master branch only

git clone -b master --single-branch https://github.com/...

sync a file from the specific branch (${BRANCH})

git checkout ${BRANCH} -- ${PATH_TO_FILE}

create the branch v1.0 on your local machine and switch in the new branch

git checkout -b 'v1.0'

push the new branch to remote repo

git push -u origin ${BRANCH}
git diff origin/master

compare the ${FILEPATH} between local branch and remote master branch

git diff origin/master -- ${FILEPATH}

unset global user name and user email

git config --global --unset-all user.name
git config --global --unset-all user.email

commit code as a different user

  • config user name and email in every commit command
    git -c user.name='UserName' -c user.email='[email protected]' commit -m "Custom message"

  • config commit user name and email within .git/config

...
[alias]
  commit-a = -c user.name='A Name' -c user.email='[email protected]' commit
  commit-b = -c user.name='B Name' -c user.email='[email protected]' commit

usage: git commit-a -m "commit message"

git --author='UserName <[email protected]>' git --author='UserName <>' without email

Let's Git with a scenario

  1. Initial commit

  2. add index.html with <h1>hello world</h1>

3.1. add robin.html with <h1>hello world</h1> @Robin

3.2. add index.html with <h2>hello world</h2>
Merge Local/Remote of the master branch

4.1. add <h3>hi robin</h3> into index.html in remote @Robin

4.2. add <h3>hi eric</h3> into index.html in local
Merge Local/Remote of the master branch and Solve conflict

  1. create a branch dev in remote

  2. add <h4>hi General</h4> into index.html under dev @Chin-Sheng

6.1 add <h5>eric again</h5> into index.html under local master
Merge master/dev branches and Solve conflict

Reference

⚠️ **GitHub.com Fallback** ⚠️