How to setup Git in existing project and Commit to Github
1. Run git init
command
Now open the terminal and
cd
into existing project, then run
git init
command to initialize git.
This will create a .git folder and .gitignore file to keeping
track of files and folder which will not be versioned by Git.
Notes: For the first time, before creating a commit,
please config git username and email:
git config --global user.name "your username"
git config --global user.email "your email address"
2. Execute Git Command to Commit locally
Before committing files to Github repository online, we will execute some
commands to create a bunch of files to be commited.
-
git status
displays the files which are new/changed but not committed yet. -
git add .
add files and folders need to be committed. The.
means all new/changed files. -
git commit -m "Initial commit"
commits the file with a message.
3. Register GitHub and Create a repositoy
- Sign Up/ Sign In on the Github portal
-
Now hover on the top right + icon, then click the New repository link
-
Fill the project repository name and visiblity setting private/public, then click Create repository button.
-
Generate SSH key:
ssh-keygen -t rsa -C "your message"
with-C "your message"
is optional. -
Copy generated public key and add New SSH key to GitHub:- Run command:
cat /home/kenjinguyen/.ssh/id_rsa.pub
and copy public key to clipboard.- Go to GitHub SSH keys and click on New SSH key button, then patse public key to the Key box.
4. Push project on Git repository
You need to run some commands to push the existing project to the Git
repository which just created:
git remote add origin git@github.com:test/project-demo.git git branch -M master git push -u origin master
That's it now you have successfully committed your files on GitHub repositoy!
Just refresh the repository page on GitHub to see committed files.