Skip to content

Instantly share code, notes, and snippets.

@fidenz-chim
Last active March 10, 2020 11:20
Show Gist options
  • Save fidenz-chim/d705f8e06431c554e463fe45f5aab215 to your computer and use it in GitHub Desktop.
Save fidenz-chim/d705f8e06431c554e463fe45f5aab215 to your computer and use it in GitHub Desktop.
Basic and frequently used Git commands for on-demand use

Clone a project to local machine

  • Create a repository in GitHub.com
  • In local machine/project_folder ...
git init // In local machine from project's root folder
git add . // Add all the files in directory to local git
// Make chanegs to the source
git commit -a -m "sample message for the commit"
git remote add origin <remote repository url from github.com>
git push -u origin master // push the master branch to github.com

Create a branch, switch to it and work on it

git branch <new_branch>
git checkout <new_branch> // switch to new branch
git status - verify the current branch

Delete a branch

git branch -d <branch_name>

Merge a brach to Master

git checkout master // switch to master
git pull origin master // update master branch
git merge dev-branch // merge the dev-branch
git push origin master // update the remote master branch

Clone a repository

git clone <repository url>

Compare two branches

// (..) compare heads of two branches
git diff branch1..branch2

// (...) compare common ancestor of two branches with head of right side branch
git diff branch1...branch2

Remove file from git

// remove from repo and disk
git rm file1.txt
git commit -m "remove file1.txt"
// remove from repo only
git rm --cached file1.txt
git commit -m "remove file1.txt"

Clone single branch

git clone -b <branch_name> --single-branch <git_url>

Clone specific branch

git clone -b my-branch https://git@github.com/username/myproject.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment