Creating an ssh key and adding it to your github account means your computer will have access to your account's repositories.
ssh-keygen -t rsa -b 2048
From here, you will be given some prompts, one for the file location (at university the .ssh
folder will have to go in your /u drive, on personal computers, the .ssh
folder will generally live in the /c drive), the next two will be to add a passphrase; the passphrase doesn't need to be added.
You then want to copy the public key and add that to your github.
cat <path-to-.ssh>/id_rsa.pub
Now you've got the ssh key set up, you just need to configure your git on your machine. This just includes setting the name and email address that match your github account.
git config --global user.name "<your-username>"
git config --global user.email <[email protected]>
Note that there are quotation marks around the username and not the email.
If you create the repo with a README, you can simply clone the repo, rather than going through the trouble of initialising them.
git clone <ssh-repo-code>
To commit changes to a repository, you need to go through 3 stages. You first stage your changes (add), then commit your changes locally (commit), and then push it to the remote (push).
First step is to create a text file as an example.
git add file-name.txt
git add .
git status
If the changes have been staged, the file will show in green, if not, it will be red.
git commit -m "your message."
NOTE: adding a commit message is important, make sure it is informative and makes clear what was changed with the commit.
git push
To get the changes somebody else has made to the repo onto your local machine, simply use
git pull
If there are conflicts here, you can stash your changes. You may also want to just reset your repository to what it is currently if there are conflicts (given you haven't contributed anything of value) using
git checkout -- .
To stash your commit use
git stash
to put the stashed changes back in, you can use
git stash apply
Go into github and find the commit that you want to go back to. Copy the commit hash, now you can get everything back to this stage.
git checkout <commit-hash> .
NOTE: Don't forget the .
!! If you miss this, the HEAD will become detatched, and you will no longer be on a branch.
After doing this, you can stage, commit, and push your changes.
To solve this problem, just make a new commit, but don't push yet!
After committing, you need to rebase. This means changing the record of your commit history. You only need to change the last commit.
git rebase -i HEAD~2
From here, just replace pick
with f
and then save and quit.
Because you have now diverged from the general course of the branch, you will have to force push your changes.
git push --force-with-lease
is the safer option, but equally, when working on a project on your own
git push -f
also works.
git checkout -b <branch-name>
To push to the branch, you will have to set the upstream:
git push --set-upstream origin <branch-name>
To switch back to the main branch simply
git checkout <branch-name>
To list all the branches you have created
git branch
To see all of the ones (including on remote)
git branch -a
git merge <other-branch>