Git can be used in a couple of ways. The 2 main ones are:
- Local first - Creating a repository locally on your machine
- Remote first - Cloning a repository
Local first is used when you already have a project on your machine, or want to just start on your own machine before pushing your code anywhere.
To start your repository locally use the
git init command.
Personally I have a project folder in my users folder called Code and inside of there I have my repositories, so this is my workflow:
cd ~/Code git init <project> cd <project>
Then I setup project and before writing any code, I do an initial commit:
git add -A git commit -m "Initial Commit"
If you see a ">" on yoru screen, you probably forgot the closing quotation mark of the git commit. To exit out of that type in Ctrl+C and start again.
Adding a remote connection¶
Now if you want to be able to push this code a remote repository, you need to create a repository on GitHub and then use the url to that repository to link your local repository to your remote repository.
- Go to https://github.com/new to get started.
- Name your repository and give it an optional description
- Set the repository to public or private (both are free)
- Ignore the "Initialize this repository with a README" checkbox.
Now that you have a remote repository setup you need to link it by typing in:
git remote add origin <url-to-repo>
If you mistype anything, you can remove a link by typing in:
git remote rm origin
Origin is the default name for the remote connection.
Now you can "push" your code to the remote repository using:
git push origin master
If there is a repository already created on GitHub, then you can use that as a base. For this, you will need to clone the repository:
git clone <url-to-repo>
This will create a folder with the repository in it. My workflow for this is:
cd ~/Code git clone <url-to-repo> cd <project>
After this you will see the code from the project and you can just start working on it.
A couple of things to note:
If this is not your own repository, you cannot push to it unless you have permission to do so.
If you want to push to it without permissions, you need to fork* it first, but it will never be merged into the original repository and you won't receive updates for it after the date you forked it.
When you clone a repository, it already includes the remote connection since it came from a remote place. So adding another remote connection is possible, but not required.
The steps above are the very minimum you need to do to work with git, but there is much more to it - which we will look into next.
Since we can add and update files to a git repository, it would be useful if we can go back to a previous state. Here are some of the basic options you have.
||does not change any of your files, but goes back the referenced commit (keeps the files in the staging tree)|
||changes the files in the working area|
||goes back to the referenced commit reverting back all changes|
||Reverses the changes of a file to the most recent commit|
||Change the last commit message in a vim editor|
||Change the last commit message inline|
From there you can recommit or make any changes you need.