Posts Git Notes
Post
Cancel

Git Notes

One of the main book I referred is Github 入门与实践.

Work with Github and Multiple accounts

I configured this based on the guidance. Here is another better instruction.

  1. Set the ssh for my personal github account.

    ssh-keygen -t rsa -C “your github account email”

  2. Add the id_rsa.pub file in the SSH of Setting on github website.

  3. Connect using ssh key

    ssh -T git@github.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
    

    Once again,

    ssh -T git@github.com

    and it works!

  4. 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 git@github.umn.edu

  5. To push the repository to the personal github account

    git init

    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 git@github.com:username/test.git

    git push -u origin master

    For work account, just replace github.com with github.umn.edu and change the corresponding username.

  6. 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 ~/.ssh/config file.

    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

  1. 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.

  2. Always remember to git pull before making modification.

  3. 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.

Git quicknotes

  1. combine two commits

    git rebase -i HEAD~2

    Then change pick to fixup.

  2. Revert to previous commit

    git reset –hard 23780769395fae

  3. Delete the commits in Github First delete local commits, then

    git push –force

Github quicknotes

  1. Add the sharable download URL for files in Github. Use the following to format:

    https://raw.githubusercontent.com/user/repository/branch/filename

  2. Upload files to Github If a repository is empty, then files could not be uploaded. Either add a README.md, either commit for once.
This post is licensed under CC BY 4.0 by the author.