Git Operations
From charlesreid1
Routine Git Operations
Stop Github Asking for Username and Password
If git keeps pestering you for your github username and password, the problem is that all of your repositories are being checked out using their HTTPS url to github. This always requires you to enter your password.
Instead, you should set up your computer to be identified by Github, which will allow you to check out repositories using their GIT url to github. This will be an identical repository, but accessed using a different authentication system (public/private keys over SSH). This is a much more efficient and safe method for authenticating.
The steps:
- Create your public private key pair
- Tell Github your public key
- Change your github repo to use the GIT url and not the HTTPS url
Step 1: Set up Github SSH Keys
Useful instructions: https://help.github.com/articles/generating-ssh-keys
If you don't have a public key, you will need to make one.
If you have one already, your public key is in ~/.ssh/id_rsa.pub so give Github the contents of that file. Log into your Github account, click account settings, and find the SSH Keys option. Add a new SSH key, and copy and paste the contents of ~/.ssh/id_rsa.pub to the box and save the key.
$ cat ~/.ssh/id_rsa.pub ........blah..........
Make sure you don't give Github your private key, which would be a bad idea. Your private key is in ~/.ssh/id_rsa (no pub extension means it is private).
Step 2: Switch Repos from HTTPS to SSH
You can switch your repos from HTTPS to SSL without opening the browser. First check the repo url using the git remote command:
$ git remote -v origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)
Find the addresses of the github remotes, and switch the protocol from HTTPS to GIT (note the syntax):
$ git remote set-url origin git@github.com:user/repo.git
Scenarios
This section covers some scenarios I often run into, but sometimes forget how to resolve.
Git Conflicts
Sometimes, when you try and push your commits with git push, you'll see a message like this:
$ git push To https://github.com/siluria/transport.git Merge branch 'master' of https://github.com/siluria/transport ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/siluria/transport.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first merge the remote changes (e.g., hint: 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Then, if you try and pull the latest changes with git pull, you might see a message like this:
$ git pull remote: Counting objects: 8, done. remote: Compressing objects: 100% (8/8), done. remote: Total 8 (delta 0), reused 2 (delta 0) Unpacking objects: 100% (8/8), done. From https://github.com/siluria/transport 153782f..10e0b68 master -> origin/master Auto-merging twodomain/SubdomainCatalyst.py Auto-merging twodomain/Interface.py Auto-merging twodomain/Driver.py CONFLICT (content): Merge conflict in twodomain/Driver.py Automatic merge failed; fix conflicts and then commit the result.
In this case, you can edit the file with the conflict, in this case, Driver.py. You'll see some lines like this:
<<<<<<< HEAD old code ======= new code >>>>>>> iss53
Edit this file to merge the code however you decide. Once you do that, you'll run git add on the code with the file, which tells git you have resolved the conflict.
$ git add Driver.py
Then - this is key - you run git commit with NO OTHER ARGUMENTS.
If you try and add arguments, i.e., specifying which file to commit, or adding a commit message, you'll see an error like this:
$ git commit Driver.py -m 'resolving conflict.' fatal: cannot do a partial commit during a merge.
Just run git commit with no arguments:
$ git commit [master d1d98ed] Merge branch 'master' of https://github.com/siluria/transport
Now you should be able to run git push with no problems:
$ git push Counting objects: 23, done. Delta compression using up to 8 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (12/12), 1.48 KiB | 0 bytes/s, done. Total 12 (delta 10), reused 0 (delta 0) To https://github.com/siluria/transport.git 10e0b68..d1d98ed master -> master