DEV Community

Cover image for A Beginner's Guide to Git
Farhat Sharif
Farhat Sharif

Posted on • Updated on

A Beginner's Guide to Git

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 <branch-name>
e.g. git push -u origin master
Description: This command sets the upstream branch and uploads your local commits to the remote repository's 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.

Top comments (3)

Collapse
 
farhatsharifh profile image
Farhat Sharif • Edited

Configure your information to be associated with your commits in GitHub when setting up Git for the first time.
Commands:
git config --global user.name "Your Name"
git config --global user.email "example@email.com"
Description: Sets your name and email globally for Git commits.

Collapse
 
brunodevphp profile image
Bruno Felipe Silva • Edited

Greetings from Brazil! Thank you so much for sharing this great tutorial. It really was useful for a newbie like me. It pretty much covers all the major topics of the issue and can be used as a revision for me when I forget something. Can't appreciate it enough, Farhat! Please, write more tutorials like this. You are such a talented teacher!

Collapse
 
farhatsharifh profile image
Farhat Sharif

Hi Bruno @brunodevphp , Thank you!
I’m happy to hear that the tutorial was helpful for you. Your feedback motivates me even more to write in future. Feel free to reach out if there’s any specific topic you’d like me to cover. Happy learning! 💻