A Beginner's Guide to Git

Farhat Sharif - Sep 5 - - Dev Community

After seeing a beginner struggling with the use of Git, I planned to write this post to explain some basic git commands in a simple way. Here's a step-by-step guide on how to use Git. I have added a brief description with each command.

1. Initialize a Local Git Repository

Command: git init
Description: Initializes a new Git repository in the current directory.
From command line, switch into any directory which you want to turn into git repository; using cd <directory_path> and execute git init command there.
(It can be an existing project directory containing your code files or an empty directory where you plan to add your files.)

2. Create a Remote Repository

Go to GitHub or another Git hosting service and create a new repository.

3. Connect Local Repository to Remote Repository

Command: git remote add origin <remote_repository_url>
Description: Links your local repository to the remote repository on GitHub.
(Copy the URL of the newly created repository at github and use here.)

4. Add Files for Staging

Command: git add <fileName>
e.g. git add sampleFile.php
Description: Stages changes in file(s) for commit. Staging is the area where you group changes you've made to files, to include in the next commit.
If you have code changes in more than one files and want to commit all together. Use:
Command: git add .
Description: Stages all changes in all files for commit.

5. Commit Code Changes

Command: git commit -m "Your commit message"
e.g. git commit -m "Update xyz method"
Description: Permanently stores your staged changes in your local repository. Use a descriptive message to document what changes have been made.

6. Push Changes to Remote Repository

Command: git push -u origin master
Description: Uploads your local commits to the remote repository's master branch. (More on branches in some next post.)


By following the above commands, you can successfully initialize a Git repository and upload your project from your local machine to GitHub.

. . . . . . . . .
Terabox Video Player