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.
How do you check if you have git installed
Open a cmd window or terminal on your computer.
Check to make sure that Git is installed and available on the command line, by typing the following at the command prompt:
To check your default Git global configuration, you can type the following at the prompt:
git config –list
Create a new local repository
This initializes the current folder as a git repository.
This is the master branch for your project.
When you initialize the repository, Git doesn’t create any commits for you. You’ll have to create the first commit yourself.
Get the current status of the folder.
After add one or more files to staging (index)
git add .
This means that all the files in the current directory will be added to the staging area of its repository.
Now, if You again type git status, you will see “Changes to be committed:” and shows the files are marked in green.
Commit the changes to our git repository.
So all the files as they exist at the moment, once they have been staged using the git add, then they will be committed to our git repository.
So at this point when we execute the git command, then our initial the state will now be changed to the first commit to the git repository.
How to write a commit message?
You can specify a commit message via the command line by running
git commit -m “First commit”
instead of just, git commit
Create a new repository on the command line
Optional! Create a .gitignore file to indicate all of the files you don’t want to track. For example, if you install Laravel, it automatically creates the .gitignore file.
git init git add README.md git commit -m "first commit" //Connect it to Github 1. Go to Github. 2. Log in to your account. 3. Click the "New" repository button. You'll have an option there to initialize the repository with a README file, but don't. 4. Click the "Create repository" button After, push the existing repository from the command line: git remote add origin https://github.com/your_username/repository_name.git git branch -M master git push -u origin master
What does git push -u mean?
The -u option automatically sets that upstream for you, linking your repo to a central one. That way, in the future, Git knows where you want to push to and where you want to pull from, so you can use git pull or git push without arguments.
Cloning a repository
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.
git clone https://github.com/your_username/repository_name.git
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
!!Push online an existing repository from the command line
git remote add origin https://github.com/your_username/repository_name.git
Will add online your repository.
git push -u origin master
Push the local git repository to the origin to the master branch
Shows the commit logs
git log –eonline
Will show us a brief log of all the commits
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.
Hello there!I hope you find this post useful!
I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!