Git
Git
-
initialize:
git init
-
Add all:
git add .
(be careful not add all your data) -
Commit: `git commit -m “comment…
- Withdraw current commit:
git reset
=git reset --mixed HEAD~1
: withdraw commit and staging area, keep working directorygit reset --soft HEAD~1
: withdraw commit, keep staging area and working directorygit reset --hard HEAD~1
: withdraw everything (dangerous)
- Withdraw current commit:
-
Ignore some files :
touch .gitignore
nano .gitignore
-
Check git status:
git status
git diff
: check changes not in staging areagit diff --staged
: check changes already in the staging area
-
Check what files been tracked:
git ls-files
1.Delete tracked files cache (not delete file itself :git rm -r --cached build/
-
Check current branch name:
git branch
- Change the branch name:
git branch -M main
- Change the branch name:
-
Use SSH add to Github: `git remote add origin git@github.com:username/repo.git
- “origin” is how you call your remote repo, it can be changed to any other name, for example “myrepo”
git remote set-url origin git@github.com:username/repo.git
- check current remote origin:
git remote -v
- Delete current remote origin:
git remote remove origin
- Add the remote origin you want: `git remote add origin git@github.com:username/repo.git
- Delete current remote origin:
-
Push:
git push -u origin main
-u
: set upstream, which means that by default, “main” branch will be push to “origin”(remote repo name) “main” branch. In the future, only need to writegit push
-
.gitignore
:- ignore folder:
folder_name/
- ignore everything but only add certain folders/files:
*!folder_name/
- ignore for example
__pycache__
in all recursive folders:**/__pycache__/
- ignore folder:
-
check current repo history:
* Check all push history:git log
(include author name, date, actions)git log --oneline
: every log just one linegit log --graph
: using graph way to show merge and branchesgit log --all
: show all branches pushes(not just current branch)- all together:
git log --oneline --graph --all
- exit log :
q
Git + ssh
Generate ssh key
- List all ssh key currently own on the machine
ls -al ~/.ssh/
-
Generate one
ssh-keygen -t ed25519 -C "your_email@example.com"
-
Check your public key
cat ~/.ssh/id_ed25519.pub
How to push to Github
-
Add this SSH key in personal settings (not the individual repository! ).
- add to personal setting, the ssh key can be used on all repositories that belongs to this personal account
- go to Github -> personal Settings -> SSH and GPG keys -> SSH keys
- add to individual repository will make this key only available for this single repository
- Go to github -> repo setting -> Deploy keys -> Add deploy key -> copy whole public key (from
cat ~/.ssh/id_ed25519.pub
)
- Go to github -> repo setting -> Deploy keys -> Add deploy key -> copy whole public key (from
- add to personal setting, the ssh key can be used on all repositories that belongs to this personal account
-
In your repository on local machine:
- Check current remote address by using `git remote -v:
- if returns https, it sets on https, when you push, it will ask for username and password, and it is already deprecated by Github (sometimes even you already set remote address as ssh, bit it still show https,so need to double check) ->
- Change using HTTP to SSH
Terminal window
git remote set-url origin git@github.com:username/repo.git
```
* if returns “git@github.com xxx” then you are on the right track - Check current remote address by using `git remote -v:
-
Then
git push origin main
- Check if SSH connection is good:
ssh -T git@github.com
, and it returns:Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
Branch
- Switch branch:
git checkout branch_name
git checkout -b branch_name
: if branch_name not existed yet, will automatically create this branch- The commit will be applied to the current branch. When switch the branch, what you can see in the vscode will be changed immediately.
- Check branches:
git branch
- Push branch to Github:
git push -u origin branch_name
. It automatically create a remote branch:origin/branch_name
-u
:--set-upstream
, create the connection between current local branch and remote branch. After create this connection, next time can just typegit push
then it will push to the branch that you have set up.- use
git remote -v
to check the name of the remote repository
Recursive
Recursive clone: git clone --recurse-submodules <repo_url>
If already cloned, update recursive: git submodule update --init --recursive
git clone --recursive https://github.com/pytorch/pytorch
Issues:
Failed to push some refs to …
Reason: the remote repository might already have some files (license / readme file)
Solve:
- Can force push and overwrite files on remote:
git push -u origin main --force
- Pull first if you want to keep them :
git pull origin main --allow-unrelated-histories