Git Commands

Git Commands

Table of Contents

Expand Table of Contents - [Initialize Git](#initialize-git) - [Commit Files](#commit-files) - [Create New Branch](#create-new-branch) - [List All branches](#list-all-branches) - [How to push a new local branch to a remote Git repository](#how-to-push-a-new-local-branch-to-a-remote-git-repository) - [How to checkout a remote branch into new local branch ](#how-to-checkout-a-remote-branch-into-new-local-branch) - [How to clean deleted remote branches from local git](#how-to-clean-deleted-remote-branches-from-local-git) - [How to rebase](#how-to-rebase)

Initialize Git

git init

Commit Files

Create some files, and then run the command below to stage all changes. You can also specific the full file path for selected files

git add .

Run the command below to see the staged changes

git status

Lastly, commit the staged changes

git commit -m "Add some files!"

Create New Branch

Create a new branch from a local branch

List All branches

You shall see the list of branches

How to push a new local branch to a remote Git repository

  1. Create a new branch.

  2. Edit or add some file, for example create a new file 'remote-branch-01.txt', and then commit the changes

  3. Push your branch to the remote repository.

How to checkout a remote branch into new local branch

Assume there is a new remote branch origin/Feature2020 was created by others, and it is not in your local git repo.

First, fetch the remote branches, and now you will able to see the origin/Feature2020 shown in your remote-branch list

Next, you checkout to a local branch and work on it

How to clean deleted remote branches from local git

If the remote branches was deleted but still appear in your local project, and you would like internal housekeeping to cleans up those deleted branches

How to rebase

Work On A New Branch

Create a new dev branch and checkout to that branch

Commit Regulary

Add some commits

Fetch Before Squashing

When you're ready to merge your features back into the master branch, run git fetch.

Squash Commits Into One

This will open an interactive rebase tool, which shows all the commits you've made on your branch. You'll then squash all your changes into one commit.

To do this, replace pick with s for all but the top commit. s is shorthand for squash - imagine all the changes you've made being squashed up into the top commit.

Squashing three commits into one by replace the pick to s from second to thrid commits are squashed into the first commit.

Press Esc and then enter :wq to save and exit the VIM interactive rebase tool

When you close this window you'll see all your existing commits, which you can edit down into a simpler, more concise commit message.

Replacing existing commits with a new commit message.

Exit the rebase tool and you're ready to merge.

Merge Your Changes

Checkout the master branch

Merge INTO master

Push your local master branch to remote

Cleanup

Delete remote feature branch (the colon is important!)

Delete local branch

Reference

Last updated

Was this helpful?