One of the main book I referred is Github 入门与实践.
Work with Github and Multiple accounts
- Set the ssh for my personal github account.
ssh-keygen -t rsa -C “your github account email”
Add the id_rsa.pub file in the SSH of Setting on github website.
- Connect using ssh key
ssh -T email@example.com
In this step, an error message comes out to be Permission denied. To fix this problem, add the following content in the config file. (If do not have a config file, just create one)
1 2 3 4 5
#Default GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_hotmail # may change the name of rsa
ssh -T firstname.lastname@example.org
and it works!
- Add my UMN github account. Following the same procedure as above, then add the following content to the config file.
1 2 3 4 5
#Default GitHub Host github.umn.edu HostName github.umn.edu User git IdentityFile ~/.ssh/id_rsa_UMN
Then use the following command to connect using ssh.
ssh -T email@example.com
- To push the repository to the personal github account
git commit -am “first commit”
(contents of other commits) # remember this could not be empty, or errors would come out. You must have got something to commit.
git remote add origin firstname.lastname@example.org:username/test.git
git push -u origin master
For work account, just replace github.com with github.umn.edu and change the corresponding username.
- To save the passphrase in the Keychain so that you don’t have to enter the passphrase every time you want to push to Github. Check Adding your SSH key to the ssh-agent for detail. Firstly, start the ssh-agent in the background.
eval “$(ssh-agent -s)”
Then, rewrite the
1 2 3 4 5 6 7
# UMN account Host github.umn.edu HostName github.umn.edu User git IdentityFile ~/.ssh/id_rsa_UMN AddKeysToAgent yes UseKeychain yes
Finally, add the SSH key to the ssh-agent.
ssh-add -K ~/.ssh/id_rsa_UMN
Collaborate on Git and Github
Set up collaboration on Github I referred to this post. For team creater, after creating the repo, go to
Settings -> Collaborators, add the collaborators you want. Abd for collaborators, git clone the repo, and do not fork it, then you are ready to go.
Always remember to
git pullbefore making modification.
Replace master branch with another branch entirely. Check this post for more detail. The idea is to use “ours” merge strategy.
1 2 3 4
git checkout newbranch git merge -s ours master git checkout master git merge newbranch
Change Git username on terminal
References: How to change my Git username in terminal? Since we have let the credential manager to help remember our username and password, we need to change credential name as well.
1 2 3 4 git config user.name "xxx" git config user.email "xxx" git config credential.username "xxx" git push
Then we would be asked to input the password, then done.
- combine two commits
git rebase -i HEAD~2
- Revert to previous commit
git reset –hard 23780769395fae
- Delete the commits in Github First delete local commits, then
git push –force
- Add the sharable download URL for files in Github. Use the following to format:
- Upload files to Github If a repository is empty, then files could not be uploaded. Either add a README.md, either commit for once.