× logo Home HTML CSS Javascript React-App Angular.js logo
logo

Git Commands Handbook & Tips


This is simple and quick guide.

Git lets you branch out from the original code base. This lets you more easily work with other developers, and gives you a lot of flexibility in your workflow.

Setting a repository

It's recommended that first of all you assign a username and email.
[$ git config --global user.name [ name] ]
[ $ git config --global user.email [ email] ]

There a three different ways to start a new repository:

From scratch
[ hjs-MacBook-Air-2:hj$ mkdir hj-project ]
[ hjs-MacBook-Air-2:hj$ cd hj-project ]
[ hjs-MacBook-Air-2:hj-project hj$ git init ]
To initialize a git repository in the root of the folder,
run the [ $ git init ] command:

From an existing project in your local machine
[ $ git init ]

To add a remote server for these new repositories use:
[ $ git remote add [remote] ]

From an existing project in a remote repository (i.e. GitHub
[ $ git clone [remote] ]

When creating a git repository, a .git directory is created where all the committed artifacts and metadata is stored.

Basic Commands Diagram

It's important to know that git provisions 3 different areas that are the core of the repository. The first one is the working directory which is the root of your Git project, here the files go through any change that the user performs. The staging area, also known as index, is where changes are built up, in this area all files are converted to tracked files by git. And the last one called as the commit area safely stores all files already committed. As seen in the image below, there's another area called Remote Repository, that represents the remote location of the origin repository.
principal commands in Git areas.

To move files between the working directory to the staging area:

// to add only one specific file
[ $ git add [ file_name ] ]

// to add all files in working directory
[ $ git add .]

// if you want to unstage a file
[ $ git restore --staged [ file_name ] ]

Once the desired files are in the staging area, commit them using:

[ $ git commit -m [commit_message]]

If there any files that are already tracked by git (indexed), a express commit can be perform, that will directly move the changes from working directory to the commit area:

[ $ git commit -am [commit_message]]

[ touch foo.text ] // to create a file
[ rm foo.text ] // to remove a file
[ git log ] // a list what commits have been made

Create a New Branch

Run this command (replacing my-branch-name with whatever name you want):
[ git checkout -b my-branch-name ]

// Switch to a Branch In Your Local Repo or to move between branches
[ $ git checkout [branch_name]

To view the status of the files in working directory and staging area, a simple command is used:

//See What Branch You're On ////Run this command
[ $ git status ]
[ $ git status -s ]

List All Branches
NOTE: The current local branch will be marked with an asterisk (*).

The log git command, helps to keep track of the different commits made to the repository:

[ $ git log ]

// to view a compact version of the commit history
[ $ git log --oneline ]

[ $ git log --graph ]

There are scenarios where we accidentally commit a change that wasn't intended to be present in that commit, or we simply introduce a bug to the source code that is impacting the correct behaviour of all the code. For this the revert command comes in handy:

// revert the last commit
[$ git revert HEAD ]

// revert to and specific commit id
[ $ git revert [commit_id] ]

If it is desired to remove files in the staging area or revert all changes made in working directory and staging area:

// only for staging
[$git reset ]

// both, working directory and staging
[ $ git reset --hard ]

Fetch command retrieves all updated files from the remote repository (origin) into the commit area but it does not merge them with your current branch, this is used to keep to repository up to date.

[ $ git fetch origin master ]

Switch to a Branch That Came From a Remote Repo
Pull command retrieves all the updated files from the remote repository into your working directory. Pull is the combination of two commands: fetch and merge.

[ $ git pull origin master ]

To get a list of all branches from the remote, run this command:
[ $ git pull ]

Push to a Branch:

Push command sends the committed changes to the remote server.

[$ git push origin master ]

[ $ git push -u origin HEAD ]

NOTE: HEAD is a reference to the top of the current branch, so it's an easy way to push to a branch of the same name on the remote. This saves you from having to type out the exact name of the branch!

If your local branch already exists on the remote, run this command:
[ $ git push ]

Merge a Branch
You'll want to make sure your working tree is clean and see what branch you're on. Run this command:
[ $ git status ]

First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch). If you're not already on the desired branch, run this command:
[ $ git checkout master ]

NOTE: Replace master with another branch name as needed.

Diff

When working on projects, specifically with other team members involved, a lot of changes are made through the different files in the repository. In order to display modifications in detail between versions of these files, a diff is needed.

// diff working dir - staging
[ $ git diff ]

// diff working dir - commit area
[ $ git diff HEAD ]

// diff staging - commit area
[ $ git diff --staged HEAD ]

// diff specific commit - commit area
[ $ git diff [commit_id] HEAD ]

// diff commit area - remote
[ $ git diff master origin/master ]

// diff branch - commit area
[ $ git diff [branch_name] master ]

Branches

// To see local branches, run this command:
[ git branch ]

// create a branch [ $ git branch [branch_name] ] // To see remote branches, run this command:
[ git branch -r ]

// To see or list all local and remote branches, run this command:
[ git branch -a ]

// Rename branch
[ $ git branch -m [branch_name] [new_branch_name]]

// delete branch
[ $ git branch -d [branch_name]

[ git branch -D [ branch-name] ]

NOTE: The -d option only deletes the branch if it has already been merged. The -D option is a shortcut for --delete --force, which deletes the branch irrespective of its merged status.

// delete branch remotely
[ $ git checkout develop ]

[ $ git push origin --delete remoteBranchName ]




logo