Upload Files
Clone to local
Clone the whole project.
cd path/to/store/clone/files
git clone https://github.com/~/~
Upload to github repository
Move the file I want to upload to the local repository directory.
cd local/repository/path
git add .
git commit -m 'Add a new file'
git push origin main
Create a git repository
First, create a folder and use git init
.
pwd
will show the path of folder mytest
mkdir mytest
cd mytest
pwd
git init
Then, create a .txt file, add and commit the file
git add readme.txt
git commit -m "create a new file"
Version Tracking
Show the commit history. use git log
or
git log --pretty=oneline
are both ok.
git log
I can go back to a previous version. HEAD is current
version, and it can be replaced by commit_id. Use
git reflog
to check commit id.
git reset --hard HEAD
Working directory, Staging area, Master
Using git status
to check working directory and staging
directory.
Withdraw
Withdraw all changed made in working directory by using
git checkout -- file_name
. This command in deed uses
file_name in git repository to replace the file in working
directory.
Withdraw all changed made in staging directory by using
git restore --staged file_name
, and then using above line
to withdraw working directory changes.
Delete
Delete a file in git repository.
git rm file_name
git commit -m "delete file file_name"
If I use git rm file_name
, the file will be deleted in
working directory and the file’s deleting status will be sent to staging
directory. The process keeps the same when I delete a file in Windows
Explorer.
Create a ssh key
Create a ssh key for a computer:
ssh-keygen -t rsa -C "youremail@example.com"
Then open github account setting to add a new ssh key from local user
file .ssh/id_rsa.pub
. After doing this, the computer can
push git repository to github repository.
Link a remote repository
Below shows how to link a github repository through ssh. origin is default repository name, and git@server-name:path/repo-name.git is repository link.
git remote add origin git@server-name:path/repo-name.git
Push
Push my local repository at the first time.
git push -u origin master
Push my local repository afterwards.
git push origin master
Branch
Create a branch named “dev”, and switch to the “dev” branch:
git checkout -b dev
## git switch -c dev
Or I can use git branch dev
to create a branch and use
git checkout dev
to switch to the branch.
Use git branch
to show all local branches. The branch
with * is the current branch.
Use git branch -r
to show all remote branches, which
begin with “origin/”.
Use git branch -a
to show all local and remote
branches.
Using the code below to merge specified branch with the current branch:
git merge specified_branch_name
Using the code below to delete a branch:
git branch -d specified_branch_name
If I want to delete an unmerged branch, I need to use -D
instead.
Conflict
When I try to merge two conflictive branches, the merge process will
fail, and turn into the merging status. At this status,
I can open the conflictive files and correct them. After that, I can use
git add .
and git commit -m " "
to complete
merging.
Collaboration
When clone from a github repository, I can’t see other branches. I
must use git checkout -b dev origin/dev
to create a branch
connecting to the dev branch.
If there is conflict when I push to remote repository, I can use
git pull origin branch_name
to clone and merge locally.
After slove the conflict problem locally, I can push the
branch_name to remote repository.