Git Learning Note

3 minute read

Posted on

My Git Learning Note

Author: Changsheng Lu (卢长胜)

0、Install Git

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

1、Create repository in two ways

i) create a local repository by cloning repo from cloud. The default up-stream is the repo that you cloned.

   # option 1
   git clone https://github.com/AlanLuSun/Circle-detection.git
   # option 2 (recommended), use PAT to be free of authentication 
   git clone https://AlanLuSun:<token>@github.com/AlanLuSun/Circle-detection.git  

   #-------------------optional-------------------
   # option 1 
   git remote add origin https://github.com/AlanLuSun/Circle-detection.git
   # option 2 (recommended), use PAT to be free of authentication
   git remote add origin https://AlanLuSun:<token>@github.com/AlanLuSun/Circle-detection.git  
   #-------------------optional-------------------

   git branch -M main  
   git push -u origin main

ii) create a local folder, and then initialize it with instructions as follows

   git init  
   git add README.md (or using git add . to add all files)    
   git commit -m "first commit"  
   git branch -M main  

   # option 1
   git remote add origin https://github.com/AlanLuSun/Circle-detection.git 
   # option 2 (recommended), use PAT to be free of authentication
   git remote add origin https://AlanLuSun:<token>@github.com/AlanLuSun/Circle-detection.git

   git push -u origin main

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) not recommended
git remote add mine https://github.com/AlanLuSun/Circle-detection.git  
# 2) (recommended) using the command which attaches **Personal Access Token (PAT)** for authentication,  
git remote add mine https://AlanLuSun:<token>@github.com/AlanLuSun/Circle-detection.git  
git remote -v  
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