[go: up one dir, main page]

DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

Dive into GIT

Let's dive into the world of GIT. Whether you're a seasoned developer or just starting out, understanding these commands is crucial for efficient version control and collaboration. Below, I'll introduce some essential Git commands that every developer should know:

Cloning a Repository

Clone an existing repository to your local machine using git clone <repository-url>.

It creates a copy of the entire project.

note: This does not fetch the submodules, use git clone --recurse-submodules <repository-url> to fetch the submodules.

Creating a Repository

Use git init for initializing a Repository

Adding a Remote

Use git remote add for Adding a Remote

e.g. git remote add origin https://github.com/0xWDG/repisotory.git

Staging Changes

use git add for Staging Changes

Add specific files or entire directories to the staging area using git add <file> or git add ..

A quick (and dirty) way to stage all changes is git add -A ..

Creating a Commit

use git commit for Creating a commit

After staging changes, create a commit (snapshot) with a meaningful message using git commit -m "Your message here".

Commits help track the history of your project.

If you want to add all changes and commit in one step, use git commit -am "Your message here".

Pushing Changes to Remote

use git push for Pushing changes to remote.

This shares your work with others and updates the remote branch.

Updating from Remote

Pull changes from a remote repository using git pull.

It fetches and merges the latest changes into your local branch.

Current Status

use git status for Checking the current status

It shows which files are modified, staged, or untracked.

See commit history

View the commit history with git log.

It displays information about each commit, including the author, date, and commit message.

Branching

Create a new branch using git branch <branch-name>.

Switch between branches with git checkout <branch-name>.

Delete a branch with git branch -d <branch-name>.

List all branches with git branch.

Combining Branches

Merge changes from one branch into another using git merge.

It combines the commit history of both branches.

Reverting Changes

Revert changes using git revert.

e.g. git revert <commit-hash>.

It creates a new commit that undoes the changes made in the specified commit.

Stashing Changes

Use git stash to stash changes.

It temporarily stores changes that are not ready to be committed.

Tagging

Create a tag using git tag -a <tag-name>.

💡 Tip: Use tags to mark important milestones, such as releases or versions.

e.g. git tag -a v1.0 -m "Version 1.0"

Push tags to the remote repository using git push --tags.

💡 Tip: Create the tag after the commit you want to tag.

Tagging a Specific Commit

Create a tag for a specific commit using git tag -a <tag-name> <commit-hash>.

e.g. git tag -a v1.0 9fceb02

Ignoring Files

Create a .gitignore file to ignore specific files or directories.

💡 Tip: Use wildcards to ignore files with a specific extension or pattern.

e.g. *.log or node_modules/

Syncing Forks

Sync a forked repository with the original repository using git fetch upstream.

It fetches the latest changes from the original repository.

Then merge the changes into your forked repository using git merge upstream/<branch>.

Finally, push the changes to your forked repository using git push origin <branch>.

NOTE: You should setup the original repository as a remote using git remote add upstream <repository-url>.

e.g. git remote add upstream https://github.com/0xWDG/original-repisotory.git

Script to sync fork

I use this script to sync my forked repositories with the original repository.

i have it in my ~/bin directory and it is called sync.

before using it, make sure to make it executable with chmod +x ~/bin/sync and add ~/bin to your $PATH variable.

You can use it like sync to sync the current branch or sync <branch-name> to sync a specific branch.

#/bin/zsh

if [! -d ".git"]; then
    echo "Not a git repository"
    exit
fi

if [-z "`git remote -v | grep upstream`"]; then
    echo "Upstream remote not found"
    echo "Add upstream remote with:"
    echo "git remote add upstream https://original/path/to/repo.git"
    exit
fi

if [-z "$1"]; then
    BRANCH=`git rev-parse --abbrev-ref HEAD`
else
    if [-z "`git branch -l $1`"]; then
        echo "Branch $1 does not exists"
        exit
    fi

    BRANCH=$1
fi

echo "Syncing with upstream/$BRANCH"
git fetch upstream
git merge upstream/$BRANCH -m "Fork Sync"
git push origin $BRANCH
Enter fullscreen mode Exit fullscreen mode

Viewing Changes

Use git diff to view changes between commits, branches, or files.

It shows the differences between the working directory and the staging area.

Viewing Remote URLs

View the remote URLs with git remote -v.

It displays the fetch and push URLs for each remote.

Viewing Remote Branches

View remote branches with git branch -r.

It lists all remote branches.

Viewing Local Branches

View local branches with git branch.

It lists all local branches.

Viewing Tags

View tags with git tag.

It lists all tags.


Dangerous but useful commands:

Resetting changes on fork to match original repository

# pulls all new commits made to upstream/branch
git pull upstream $BRACNH

# this will delete all your local changes to branch
git reset --hard upstream/$BRACNH

# take care, this will delete all your changes on your forked branch
git push origin $BRACNH --force
Enter fullscreen mode Exit fullscreen mode

Delete all commits

Replace $BRANCH with the name of the branch you want to delete all commits from.

# This will create a new branch called latest_branch
git checkout --orphan latest_branch

# This adds all the files in the directory to the staging area.
git add -A

# This creates a new commit. (initial commit)
git commit -a -m "Initial commit"

# This deletes the $BRANCH branch
git branch -D "$BRANCH"

# This renames the latest_branch to $BRANCH
git branch -m "$BRANCH"

# This pushes the new branch to the remote repository
git push -f origin "$BRANCH"
Enter fullscreen mode Exit fullscreen mode

Resources:

Top comments (0)