DEV Community

Cover image for Git for New Devs: Your Code's Grand Adventure !
Kavin HBN
Kavin HBN

Posted on • Edited on

Git for New Devs: Your Code's Grand Adventure !

Git for Beginners: A Simple Guide to Your Git Workflow

When you are learning Git, the number of commands can feel overwhelming.

The good news is that most of your day-to-day work follows the simple Git workflow.

Once you understand that Git workflow, handling problems like merge conflicts or rejected Git pushes becomes much easier.

Git for beginners can be easy to learn.

Think of Git as a journal for your project.

Every meaningful change is recorded so you can track your progress collaborate with others using Git and even travel back in time if something goes wrong with Git.


The Normal Git Workflow

Most of the time you will follow these steps in your Git workflow.

Step 1: Initialize Your Git Repository

When starting a project initialize Git inside your project folder using Git.

If you are using GitHub connect your Git repository to the remote Git repository using Git.


git init

Enter fullscreen mode Exit fullscreen mode

If you are using GitHub you can connect your Git repository to the remote Git repository using Git.


git remote add origin https://github.com/your-username/your-repository.git

Enter fullscreen mode Exit fullscreen mode

Now your local project knows where it should push and pull changes using Git.


Step 2: Stage Your Changes in Git

After writing or modifying code tell Git which files should be included in the next Git commit.

Stage every changed file in Git:


git add.

Enter fullscreen mode Exit fullscreen mode

Or stage a file in Git:


git add app.js

Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Commit in Git

A commit in Git is a snapshot of your project at a specific point in time using Git.

Write meaningful commit messages that explain what changed in your Git project not that something changed in your Git project.


git commit -m " project setup"

Enter fullscreen mode Exit fullscreen mode

Good examples of commit messages in Git are:


git commit -m "Add user authentication"

git commit -m "Fix login validation bug"

git commit -m "Update portfolio homepage"

Enter fullscreen mode Exit fullscreen mode

Step 4: Push Your Changes in Git

Upload your commits to GitHub using Git.

The first push using Git:


git push -u origin main

Enter fullscreen mode Exit fullscreen mode

Future pushes using Git:


git push

Enter fullscreen mode Exit fullscreen mode

At this point your work is safely stored both locally and on GitHub using Git.


When Things Do Not Go as Planned in Git

Working is simple in Git.

Working with developers introduces a few common situations you will eventually encounter in Git.

Fortunately they are easy to fix once you understand what is happening in Git.


Problem 1: Push Rejected in Git

Sometimes Git refuses your push. Shows a message similar to this in Git:


Updates were rejected because the remote contains work that you do not have locally.

Enter fullscreen mode Exit fullscreen mode

This means someone has already pushed commits to GitHub that you do not have on your computer using Git.

Git blocks your push to prevent overwriting their work in Git.


Solution: Pull First in Git

Download the changes from GitHub using Git.


git pull origin main

Enter fullscreen mode Exit fullscreen mode

If your changes and the remote changes do not overlap Git merges everything automatically using Git.

Then simply push again using Git.


git push

Enter fullscreen mode Exit fullscreen mode

Problem 2: Merge Conflict in Git

Sometimes both you and another developer edit the part of the same file in Git.

Git does not know which version should be kept in Git.

Of guessing it asks you to decide in Git.

You will see something to this in Git:


<<<<<<< HEAD

My version

=======

Their version

>>>>>>> origin/main

Enter fullscreen mode Exit fullscreen mode

How to Resolve It in Git

Open the file and decide which code should remain in Git.

You can keep your version keep their version or combine both versions in Git.

After editing the file remove all the conflict markers in Git.

Then stage the file in Git.


git add filename

Enter fullscreen mode Exit fullscreen mode

Create a commit in Git.


git commit -m "Resolve merge conflict"

Enter fullscreen mode Exit fullscreen mode

Finally push your updated code in Git.


git push

Enter fullscreen mode Exit fullscreen mode

Working Safely with Branches in Git

Branches let you develop features without affecting the main project in Git.

Create a new branch in Git.


git checkout -b feature/login

Enter fullscreen mode Exit fullscreen mode

Work in Git.


git add.

git commit -m "Add login page"

Enter fullscreen mode Exit fullscreen mode

Switch to the main branch in Git.


git checkout main

Enter fullscreen mode Exit fullscreen mode

Merge your feature in Git.


git merge feature/login

Enter fullscreen mode Exit fullscreen mode

Once merged your new feature becomes part of the project in Git.


Undoing Commits in Git

Everyone makes mistakes in Git.

Git provides ways to undo changes in Git.

View your commit history in Git.


git log --oneline

Enter fullscreen mode Exit fullscreen mode

Undo the commit but keep your code in Git.


git reset --soft HEAD~1

Enter fullscreen mode Exit fullscreen mode

Undo the commit and permanently remove those changes in Git.


git reset --hard HEAD~1

Enter fullscreen mode Exit fullscreen mode

Use --hard carefully because the discarded changes cannot be recovered easily in Git.


Understanding git status in Git

One of the useful commands is in Git:


git status

Enter fullscreen mode Exit fullscreen mode

It tells you exactly what is happening inside your repository in Git.

Common status indicators include:

| Symbol | Meaning

| ------ | ------------------------------ |

M` | Modified file |

| A | Newly added file |

| D | Deleted file |

| U | Unmerged file (merge conflict) |

| ?? | Untracked file |

Running git status frequently helps you understand what Git is tracking before committing in Git.


Common Git Workflow Cheat Sheet in Git

Initialize a repository in Git:

`bash

git init

`

Stage files in Git:

`bash

git add.

`

Create a commit in Git:

`bash

git commit -m "Your message"

`

Push to GitHub in Git:

`bash

git push

`

Download changes in Git:

`bash

git pull

`

Create a new branch in Git:

`bash

git checkout -b feature-name

`

Switch branches in Git:

`bash

git checkout main

`

Merge a branch in Git:

`bash

git merge feature-name

`

View commit history in Git:

`bash

git log --oneline

`

Check repository status in Git:

`bash

git status

`


Final Thoughts on Git

Git can seem intimidating at first but most developers use the same handful of commands every day in Git.

The basic Git workflow is simple:

  1. Make changes in Git.

  2. Stage them with git add in Git.

  3. Save them with git commit in Git.

  4. Share them using git push in Git.

As you collaborate with others you will occasionally encounter pull requests merge conflicts and branches in Git.

These are not signs that something's wrong. They are simply part of working with version control in Git.

The more you practice the more Git starts to feel less like a list of commands and like a reliable safety net for your code, in Git.

Top comments (0)