How to work with Git and GitHub?
1. Install Git and create a GitHub account
The first two things that You will have to do are to install Git and create a free GitHub account.
- Install Git here. If you already have Git installed, check its version using the command git –version
- Create a GitHub account – GitHub accounts are FREE for public and private repositories.
With a free account at github u will get:
- Unlimited public repositories
- Unlimited private repositories
- 3 collaborators for private repositories
- Issues and bug tracking
- Project management
2. Create a local git repository
To create a new project on your local machine using Git, first u have to create a new repository (repo). When you save a version of your files in git, meaning when you make a “commit“, you will serve a version of every file in Your repository. Git store a version of your files, each time you choose to commit.
In short, a commit is a snapshot of every file in your repository at the time you make the commit.
Create a new local repository
When you initialize the repository, Git doesn’t create any commits for you. You’ll have to create the first commit yourself.
Add one or more files to staging (index)
git add *
How to write a commit message?
You can specify a commit message via the command line by running
git commit -m “Commit message”
instead of just:
Cloning a repository
git clone – allowing you to copy an entire repository from one computer to another, creating a clone of the repository.
If someone else gives you the location of their directory or repository, you can copy or clone it to your own computer.
To use it:
$ git clone https://github.com/…
git clone will also copy the commit history of the repository. Also, the state of every file in the directory or repository is copied.
Commit changes to head (but not yet to the remote repository)
git commit -m “Commit message”
Send changes to the master branch of your remote repository
git push origin master
git log – Shows the commit logs.
git diff – Show changes between commits, commit and working tree, etc
git diff [<options>] <commit> <commit>
– view the changes between two arbitrary
diff -u and git diff show very similar outputs
git diff considers the first argument as the “original”, and the second argument as the “new” version
git diff with no arguments, will show any change you’ve made that you haven’t added to the staging area yet.
git diff –staged
Will show the differences between the staging area and the most recent commit.
git checkout – Switch branches or restore working tree files.
A commit saves a snapshot of all files in the repository at the time the commit was made, so checking out an earlier commit will result in all the files being reverted to their state at the time the commit was made.
git reset --hard
This command discards any changes in either the working directory or staging area.
If you run this command you can’t get those changes back!
Git allows you to create labels for your commits, that are called “branches“.
“Master” is the name give to main branch in most Git repositories. Every time you create a repository, Git create a master branch for you.
Git branch is the command to create and view branches. Git branch without any argument will shows the current branches.
Git branch with an argument, it creates a new branch with that name, like:
git branch my-branch-name
It is possible to have multiple branch labels attached to a single commit, but making a new commit will only update the branch that you have checked out and leave any others alone.
When running command “git granch”, the star next to branch mean that is the branch that is currently checked out. So that’s the one that will update if we make a change.
git checkout my-branch-name
This command will switch to my-branch-name branch, which will be starred.
You can checkout this branch and commit the changes.
When is indicated to use branches?
Anytime when you want to test something different, not big changes, you can create a new branch.
Adding an existing project to GitHub using the command line?
- Open Git Bash
- Change the current working directory to your local project.
- Initialize the local directory as a Git repository
$ git init
- Adds the files in the local repository and stages them for commit – $ git add .
- Commit the files that you’ve staged in your local repository
$ git commit -m "First commit"
- In the Command prompt, add the URL for the remote repository where your local repository will be pushed – $ git remote add origin remote repository URL
remote repository URL is Your GitHub repository url
- Push the changes in your local repository to GitHub
$ git push origin masterCareful!!!
If this is your first push, you should use this command
git push -f origin master
The GitHub repository should be empty or contain same files as your local repository, otherwise u will get this error: “Updates were rejected because the remote contains work that you do. hint: not have locally.”
Git Errors and Warnings Solution
Should not be doing an octopus
Octopus is a strategy Git uses to combine many different versions of code together. This message can appear if you try to use this strategy in an inappropriate situation.
You are in ‘detached HEAD’ state
HEAD is what Git calls the commit you are currently on. You can “detach” the HEAD by switching to a previous commit. Git warns you about that, so that you’ll realize you’re doing it.