Skip to content

Git and Github

ambipomyan edited this page Aug 9, 2021 · 1 revision
1. download git

for Linux: sudo apt-get install git

for MacOS: download XCode

then, try it: git

then, set:

$ git config --global user.name "YourName"
$ git config --global user.email "YourEmail"
2. create a repository
$ mkdir learngit
$ cd learngit
$ pwd

then, git init

add a readme file, which has to be put in the learngit folder:

$ git add readme.txt
$ git commit -m "add a readme file"

if you change the readme file, check status first: git status

check what were changed: git diff

everything is OK: git add readme.txt

if you want, use git status to check the status

git commit -m "changes added to readme file"

3. manage file changes

git manages changes instead of files!

use git add to add (changed) files from Working Directory to stage, and git commit -m "changes added to readme file" to commit it.

discard changes in Working Directory, that means the changes are not added: git checkout --readme.txt

if changes are already added, that means the changes are not committed: git reset HEAD readme.txt, then, git checkout --readme.txt

if changes are committed: git reset --hard HEAD^

plus, more common situation: git reset --hard CommitId

finding previous edition commit_id's: git log

finding commits log commit_id's: git reflog

delete a file: git rm readme.txt and then git commit

realize that delete a wrong file, before committing: git checkout --readme.txt

if changes are committed: git reset --hard HEAD^

4. remote repository

link your local repo with remote repo on github.com

use ssh-keygen -t rsa -C "YourEmailAddr" to get keys

login your GitHub account -> settings -> SSH and GPG Keys, follow the guide to add keys.

then, create a new empty repository with the same name: learngit

use git remote add origin [email protected]:YourAccountName/learngit.git and git push -u origin master

every time you change the local repo and commit it: git push origin master

clone a remote repo: git clone [email protected]:YourAccountName/learngit.git, or refer to the readme document of the repo you want to clone

5. branches

branches are places where you working on for improving your work without bothering to the mater branch, when you finish, merge them to master branch

use git branch to check branch

use git branch BranchName to create branch and git checkout BranchName to switch to it, or, you can use: git checkout -b BranchName to do it with one step.

use git merge BranchName to merge it to master and git branch -d BranchName to delete it, if you want to delete a unmerged branch, use git branch -D BranchName

if there is a conflict, users need to verify it by hand, and then merge them, plus, use git merge --no-ff -m "merge without fast forwarding" BranchName to log the merge message

user may only copy the master branch, and create a local branch git checkout -b BranchName origin/BranchName, use the same name as the remote repo does, then, use git pull to get the latest version of the branch

every time you change the local repo and commit it: git push origin BranchName

if failed, try git pull before pushing it

if git pull is failed, try git branch --set-upstream BranchName origin/BranchName

6. tags

work with commits, like v 1.2.0

git tag BranchName CommitId

7. use GitHub

clone -> improve -> pull request