[Resolvido] How to set up Git and GitHub with PHPStorm?

  

4
Início do tópico

setup, connect and use phpstorm with git and github

Hi, I would like to set up Git with PHPStorm and use it in collaboration with GitHub to upload my projects to my GitHub account.

How can I do it?

Thank you!

Andrew

2 Respostas
3

First you need to install Git. Download + install it on your PC from HERE: https://git-scm.com/

Then, create an EMPTY repository on GitHub - without readme, ignore and licence files. Let's name it travel-site. For this example I will be using my real GitHub repository: https://github.com/AleikovStudio/travel-site

Now you will be working with the terminal - you can use Window's cmd or the PHPStorm's terminal. I prefer to use the terminal built inside the PHPStorm - it is: better, faster and more intuitive.

From your GitHub copy the repository URL - for example https://github.com/AleikovStudio/travel-site.git (note the .git extension at the end); Instead of AleikovStudio (my username in GitHub - use yours).

git and github workflow

Inside your PHPStorm project's folder you will need to initialize Git (btw you can drag the folder with your project into the terminal). In the terminal type:

git init

Now, let Git know where to upload your project files online (your push repository). To do so, in the terminal type:

git remote set-url origin  https://github.com/AleikovStudio/travel-site.git 

or

git remote add origin  https://github.com/AleikovStudio/travel-site.git 

To check which is your push repository on GitHub, type:

git remote -v

Very common and frequently used git command is:

git status

It will check for changes and will give you recommendations.

check git status in phpstorm terminal

To add all changes to the stage, type:

git add .

The . here stands for all files. To add a particular file (for example: index.html) you can type:

git add index.html

Also you can stage your changes with (here -A is for ALL):

git add -A

To commit new commit + add a message (for example: "Added new message"), type:

git commit -m "Added new message"

You can also stage + commit in one command line: 

git commit -am "Stage and commit in one command"

To push the files (from your local PC) for the first time to your GitHub repository online, type:

git push origin master

To push the changes afterwards, simply type:

git push

Git Branches:

To create new branch, type:

git branch footer

This will create new branch named footer

To see which branch you are currently using, type:

git branch

To change (checkout) the branch:

git checkout testimonials

* instead of the testimonials (name of the branch) you can type master (the main branch):

git checkout master

To create and at the same time checkout a new branch (in this case named header), type:

git checkout -b header

To merge the branch (for exampletestimonials branch) with the master branch (the main one), type:

git merge testimonials

Please note: you need to be ON master branch to merge other branches (you can check on which branch you are currently on by typing git branch).

* instead of a testimonials you can type the name of the branch you wish to merge with the master one

Here are some other useful Git commands (and some for the terminal):

CLEAR TERMINAL (COMMAND PROMPT):

cls

STOP A TASK ON THE TERMINAL (COMMAND PROMPT):

CTRL + C

CLONE A GIT PROJECT FROM GITHUB:

CHECK GIT VERSION:

git --version

CREATE NEW DIRECTORY/FOLDER (for example: hello-world):

mkdir "hello-world"

CHANGE DIRECTORY TO (for example: hello-world):

cd hello-world

GO UP:

cd..

CREATE FILE (for example: index.html):

touch "index.html"

If you are getting an error you might need to install touch npm package. In the terminal type:

npm install touch

To learn more about npm packages and how to use them with PHPStorm, please click here.

RESTORE A FILE FROM A COMMIT:

git checkout -- .

FILE STAGED (READY TO BE COMMITED - from red color to green):

git add -A
2

Basically there are 2 main steps to configure PHPStrom to work with Git and GitHub:

  • First - to initialize Git and PHPStorm: in PHPStrom you need to navigate to VCS >> Import into Version Control >> Create Git Repository...:

configure git with phpstorm settings

This will initialize Git for your project (you need to Create .gitignore file as well).

Afterwards you will easily manage it with the Terminal commands.

  • Second - you need to connect your project with GitHub. In PHPStrom go to: VCS >> Import into Version Control >> Share Project on GitHub:

github and phpstorm settings and configuration

BTW, in PHP Storm you can use the Version Control tab to better understand your logs;

Here is a 10 min. video explaining some stuff:

Here is with BitBucket integration (German):

...and video in Russian:

Here is a video about Git and .gitignore file and which WP files to ignore:

Partilhar: