Git Command Line Tutorials

While GUI tools like Tortoise git makes it easier to use git, it is good to get familiar with shell commands of git
A good reference is Git Basics

Create a new Git in current folder

git init
git add .
git commit -m "First commit comment"

git add *.php could be used, if you want only php files to be part of git

git commit -a -m 'added new benchmarks' coud replace two lines add and commit

Command line Commit Message Editor

This is useful when you want to enter multi line commit message. It also popus when you dont add message after git coomit. ie git commit without -m flag.

Press Esc and then type :wq to save and exit

Change commit message later

Several options as mentioned here

Optional commands

They must be added before git commit

 git add LICENSE

Manually make .gitingnore

Create a text file .gitignore.
Add the names of files you dont want to push to git
The rules for the patterns you can put in the .gitignore file are as follows:

  1. Blank lines or lines starting with # are ignored.
  2. Standard glob patterns work.
  3. You can end patterns with a forward slash (/) to specify a directory.
  4. You can negate a pattern by starting it with an exclamation point (!).

Same can be accived with command line

echo  .gitignore >> .gitignore 
echo addExistingPjt2Git.bat >> .gitignore


echo # list of files and folders to ignore in git  >> .gitignore
for %f IN (.gitignore,addExistingPjt2Git.bat) do echo %f >> .gitignore

PS: if used in command line add escape % before each %

echo # list of files and folders to ignore in git  >> .gitignore
for %%f IN (.gitignore,addExistingPjt2Git.bat)do echo %%f >> .gitignore

Add and restore files

To update what will be committed

git add <file>...

to discard changes in working directory

git restore <file>...

Remove(delete) file

git rm <file>...

Run commit and push to reflect in remote repository

git commit -m "Deleted the file from the git repository"
git push

Remove(delete) folder

git rm -r folder1

To remove(delete) from git, but keep in folder

git rm --cached <file>


git mv file_from file_to

Working with remote repository

Create a clone from a remote git

git clone

Add Remote Repository to a local git repository

git remote add origin <remoteRepositoryURL>

To add additional remote repositories, use different shortnames


git remote add osolcu

Now you can use osolc to in leu of the full url
eg: To fetch all the information that osolcu has but that you don’t yet have in your repository

git fetch osolcu

To get list of remote remositories

git remote

The above command will gives shornames of remote repositories eg: origin
To get full urls of remote git add -v

git remote -v

To get current local branch and remote brank use

git show-ref

As shown here.

This comes handy when error message src refspec master does not match any comes

To remove a remote

git remote remove origin

Git fetch and pull

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.

If you clone a repository, the command automatically adds that remote repository under the name “origin”. So, git fetch origin fetches any new work that has been pushed to that server since you cloned

Git checkout

In Git, the term checkout is used for the act of switching between different versions of a target entity. The git checkout command is used to switch between branches in a repository. Be careful with your staged files and commits when switching between branches.


git checkout <different branch>

Pushing to Your Remotes

When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: git push [remote-name] [branch-name]. If you want to push your master branch to your origin server (again, cloning generally sets up both of those names for you automatically), then you can run this to push any commits you’ve done back up to the server:

git push origin master

OR if there are multiple branches use origin/<branch name>

git push origin origin/main

This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push.

If a project is developed from different locations this happens. Solving this will be explained later



Clone a specific branch


git clone --branch <branchname> <remote-repo-url>


git clone -b <branchname> <remote-repo-url>

Here -b is just an alias for --branch

Option Two

git clone --branch <branchname> --single-branch <remote-repo-url>


git clone -b <branchname> --single-branch <remote-repo-url>

Here -b is just an alias for --branch.

This performs the same action as option one, except that the --single-branch option was introduced in Git version 1.7.10 and later. It allows you to only fetch files from the specified branch without fetching other branches.

Create a branch

git branch testing

Create a new branch and switch to same

git checkout -b testing

Push new branch to remote server

git push origin -u testing

delete a local branch

  1. checkout to a different branch
  2. delete intented branch
git checkout main
git branch --delete bob2

git branch -d old-branch also works

Run the git branch -a command to verify the local Git branch is deleted

Get info of current branch

git branch --show-current

delete a remote branch

git push origin --delete <branchname>

Rename git branch

git checkout bob
rem if already done push to bob, delete remote bob with  git push origin --delete bob
git branch -m bob2
git push origin -u bob2

Pull from a remote Branch

git checkout bob
git pull origin bob





  1. git diff HEAD to compare the both staged and unstaged changes with your last commit.
  2. git diff <branch_name1> <branch_name2> to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches. So diff's outcome will change depending on the order.
  3. git diff <commit_hash> <commit_hash> the command to compare the changes between two commits. Like branch comparison, order does matter in comparing commits.
  4. You can run the below commands to compare the changes for specific file:

    1. git diff HEAD <file_name>
    2. git diff <file_name>
    3. git diff --staged <file_name> or git diff --cached <file_name>,
    4. git diff <branch_name1> <branch_name2> <file_name>
    5. git diff <commit_hash> <commit_hash> <file_name>

Merge branches

  1. checkout to original branch
  2. run git merge command with the branch to merge
git checkout master
git merge hotfix



Rebase and Merge Differences

Use different user locally

git config --local <username>
git config --local <useremail>
git config --local user.signingkey ""
git config --local credential.helper wincred
git config --local credential.username <username>

Delete a Gibhub repository


Retain an empty directory

Git as such cannot retain empty folders. But there is a workaround

  1. save an empty file .gitkeep in theat folder
  2. add the following lines to .gitignore
    # exclude everything in generatedDocs
    # exception to the rule for generatedDocs

Revert to Previous version


Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.

An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.

To reset, you’ll need a reference to the commit you want to move back to. You can get this by running reflog:

git reflog


git log –-oneline

There are 4 ways to revert to previous version

Types of Git resets
Image Reference

  1. git reset --soft, which will keep your files, and stage all changes back automatically.
  2. git reset --hard , which will completely destroy any changes and remove them from the local directory. Only use this if you know what you’re doing.
  3. git reset --mixed <seven digit code of commit id>. This is then default mode
  4. Alternatively, there is a shorthand method to roll back the commit: To revert commits without knowing the necessary commit ID, admins can use the command below to revert code versions relative to where the current head is. In the example, ~1 refers to the number of commits backward to which the code tree will revert.
    git reset head~1

    HTML pages in gitbub

    Github documentation
    Basically these are the steps

  5. create a branch gh-pages
  6. add commit and push
  7. go to github repository >> Settings
  8. Click Pages under "Code and automation"
  9. Under "GitHub Pages", use the None or Branch drop-down menu and select a publishing source(branch, here it is gh-pages)
  10. Click Save

Git Commands

git init
git checkout -b gh-pages
git add . && git commit -m "First commit to upload html"
git remote add origin
git push -u origin gh-pages

Follow the steps here
Go to

HTML Pages in Bitbucket

To set up a static website hosted on :

1.From within Bitbucket, create a repository, and have the Repository name use the scheme

2.Clone the repository to your local system.

  1. Create a file called index.html in the repository root directory, in lowercase letters. Uppercase and special characters are not permitted.

Why lowercase?

Filenames are case sensitive; Bitbucket treats index.html and Index.html as different file names.

  1. Create a file named index.html in your repository, add some content, commit the change, then push the change to Bitbucket.

  2. Navigate to the site. Bitbucket displays the HTML in the site's index.html.

For example...

For a workspace with the ID tutorials you would navigate to


Unlike github, which allows seperate branches to serve html pages for each directory, only one repository is allowed for one bit bucket account . So for for getting html site for each repository, create folders for each of them. Eg: Bitbucket HTML for MVC Maker


1. "git error refname refs/heads/master not found"

ie , like this

git branch -M main
error: refname refs/heads/master not found
fatal: Branch rename failed

Solution portugese

Solution code

git fetch
git reset --hard origin/main

2. "error: failed to push some refs to"


git push -u origin gh-pages
error: src refspec gh-pages does not match any
error: failed to push some refs to ''

Solution code

git pull --rebase origin main
git push origin main