-
Notifications
You must be signed in to change notification settings - Fork 0
Git Basics
(copyright pending)
Remote repository: Repository that isn't hosted by you, such as a repository on GitHub
Local repository: A folder on your PC that contains a git repository (basically a folder with .git folder in it)
Pull: Download from remote repository to local repository
Push: Upload from local repository to remote repository
Clone: Make a local repository by copying remote repository to your machine
Commit: A set of changes
Before you do anything, install Git from here, or if you're on a sane operating system - its package manager.
First, setup your credentials
git config --global user.name "Your Name Here"
git config --global user.email "me@example.com"
For example:
git config --global user.name "Luna D."
git config --global user.email "luna@nighty.cloud"
This does not actually need to be your GitHub E-Mail, but it really should be something tied to your GH account so that commits get correctly attributed to you.
Then enable rebase on pull by default
git config --global pull.rebase true
This will ensure that you will be rebasing instead of creating garbage "Merge remote branch" commits.
Rebase basically does this:
- Put your changes aside
- Roll back to the last commit in remote repository
- Apply your commits on top of what is in the repository
You'll need to tell GitHub that your PC is authorized to access your account. For that you need an SSH key. Luckily if you installed Git from git-scm.com you should have ssh-keygen command-line utility already pre-installed and available.
To generate the key, use this command and simply click through it. Make sure that the file name is id_rsa, otherwise you'll have a bad time™.
ssh-keygen -b 4096
It should create two files, id_rsa and id_rsa.pub in one of the following folders:
- Windows -
C:\Users\YOUR_USERNAME_HERE\.ssh - Literally anything else, even your toaster -
/home/YOUR_USERNAME_HERE/.ssh
id_rsa is your private key, which is like a password you can use to authorize your actions. id_rsa.pub is a public key, which can be used by others to verify that your private key really authorized the action.
NEVER SHARE THE CONTENTS OF id_rsa WITH ANYONE, PROTECT IT LIKE YOUR GITHUB PASSWORD
Copy the contents of id_rsa.pub here.
You should now be set up for using git from commandline!
git clone git@github.com:USERNAME/REPOSITORY [FOLDERNAME]
FOLDERNAME is optional.
Examples
git clone git@github.com:Meow/spacegame
git clone git@github.com:infomediadesign/projektarbeit-ii-team-7 pa2
In project's root folder, first add all files to the commit:
git add .
Then actually commit them
git commit -m "Commit description"
Before you push the commit to the remote repository, make sure you have the latest state of the remote repository
git pull --rebase
And now push
git push
# (you might need to do this on your very-first commit tho)
git push --set-upstream origin master
If you're an insane person, you can use
git diff
but you really should just download Visual Studio Code and open the local repository folder in it. VSCode has git integration built-in. Visual Studio Code IS NOT Visual Studio.
You sometimes may want to work in a different branch. To switch to a branch that already exists in the repo, simply:
git checkout BRANCHNAME
Example
git checkout feature-testing
But sometimes you may also want to create your own branch
git checkout -b BRANCHNAME
Example
git checkout -b super-amazing-feature
When you push to a new branch you created for the first time, you may need to use --set-upstream, such as
git push --set-upstream origin super-amazing-feature
Sometimes you may want to bring the repository back, especially if you screw up in some way that you can't easily recover from. This is accomplished by using git reset.
git reset --hard COMMIT_ID
Example
git reset --hard origin/master
git reset --hard b346ffcd5c5dea6991acd0f7c6e1117a83151109
If you simply want to undo a commit but keep your changes, do a soft reset:
git reset --soft COMMIT_ID
Example
git reset --soft HEAD~1
git reset --soft origin/master
git reset --soft b346ffcd5c5dea6991acd0f7c6e1117a83151109
git log
Git has a cherry-pick feature that lets you take a certain commit and bring it to your current branch. Use this only if you're certain you know what you're doing:
git cherry-pick COMMIT_ID
Example
git cherry-pick b346ffcd5c5dea6991acd0f7c6e1117a83151109