Git Learning Note

2 minute read

Posted on

My Git Learning Note

Author: Changsheng Lu (卢长胜)

0、Install Git

1) download Git from official website and install it in your computer. Please note that downloading suitable version according to the OS of your computer.
2) run the Git bash in your desired directory.

1、Create repository in two ways

1) create a local repository by cloning repo from cloud. The default up-stream is the repo that you cloned.
git clone https://github.com/AlanLuSun/Circle-detection.git

2) create a local folder, and then initialize it with instructions as follows
git init
git remote add origin https://github.com/AlanLuSun/Circle-detection.git
git push -u origin master
it should note that we need to specify the remote repo as up-stream by using git remote add origin …

2、Add, show and delete remote repos

1) git remote add mine https://github.com/AlanLuSun/Circle-detection.git
or using the command which attaches Personal Access Token (PAT) for authentication,
git remote add mine https://AlanLuSun:<token>@github.com/AlanLuSun/Circle-detection.git
1) git remote -v
2) git remote remove mine

3、Set the head of the remote up-stream. say there exists two remote repos: mine and origin, and we wanna set origin as head.

1) git remote set-head origin

4、Add files to staging environment

1) git add . //add all files
2) git add test.md //add test.md

5、Make a commit for packaging the added files

1) git commit -m “my commit”

6、Build or delete a branch and switch to desired branch

1) git branch my-branch-name
2) git checkout my-branch-name //switch to branch my-branch-name
3) git checkout -b my-branch-name //this command line equals to above two
4) git checkout master //go back master branch
5) git branch -d branch-name //delete fully merged branch
git branch -D branch-name //delete a branch even if not merged

7、Merge branches. Suppose that we currently are at master branch and would like to merge new-branch

git merge new-branch

8、Pull remote repo into local repo, which aims to synchronize local repo and remote repo

1) git fetch origin master
2) git merge origin/master //merge to local branch
3) git pull origin master //this command line equals to above two lines
above command lines can deal with the case of when remote files are different with local files which will lead to push problems.

9、Automatically create a new branch repo for remote master. We first create a new-branch for local master, and then push it to remote repo

1) git checkout -b new-branch
2) …//do something
3) git push origin new-branch //note that current branch is new-branch instead of master

10、Show current status

1) git status

11、Configure user information

1) configuring user information in local repository
git config –local user.name AlanLuSun
git config –local user.email xxx
we can find the config file in .git/
1) configuring user information globally
git config –global user.name AlanLuSun
git config –global user.email xxx
we can find the .gitconfig in C:/Users/14545/

12、

Leave a Comment