Git Basic Command Line Usage

Git is a distributed version control system.

Git Config

git config --global user.name "Your name here"
git congig --global user.email "Your email id here"

Get the list of git configuration setup currently:

git config --list

Git Help

To get help you can run:

git --help

Also, a better way is to target a specific target action to get the help for:

git merge --help

Git initialize

To create a git repo from your local, you can create a folder and initialize it to be a git repo:

mkdir my-project
cd my-project
git init
ls -a

.git

Git ignore file

File containing the list of files that shouldnt be tracked by git.

// Create file
touch .gitignore

Places where your code can be

  • Local repository(Working Directory)
  • Staging area
  • Remote Repository (.git directory)

Working with staging area

To add file to staging area, you can set files to be commited as follows:

git add <file-name>
git add .gitignore

Add everything to staging area:

git add -A

To remove files from staging area:

git reset <file-name>

To remove everything from staging area:

git reset

Committing files

To commit files to local branch, you can do commit as follows:

git commit -m "your message here"

To undo a commit:

git checkout <file-name>

To get the commit history:

git log

To get the commit history with files list:

git log --stat

Cloning remote repo

To clone a remote repo, get the repo git url and run:

git clone <git-url> [foldername]

Getting info about repo

To view info about the remove repo:

git remote -v

Get all the branches in remote repo:

git branch -a

Pushing your changes to repo

After doing commit, you need to push your changes to the repo. Before doing push you need to make sure that you have the latest changes that others have pushed before you.

So do a pull followed by a push:

git pull origin master
git push origin master

Working with branch

Create a branch:

git branch <new-branch-name>
git checkout <new-branch-name>

To get the list of branches in the local:

git branch

Gives a list of branches you have locally. The branch you are on will be preceded by a *.

To ge the list of all the branches along with the ones in the remote repository:

git branch -a

Where remote repositories are colored in red

To switch a branch:

git checkout <branch-name>

Associate the local branch to the remote branch:

git push -u origin <branch-name>

Here the -u specified binds the local branch to the remote repo, so that next time onwards, to do pull and push you can do just the following:

git pull
git push

Merging branches

Merging feature branch to the master branch:

git checkout master
git pull origin master

// Get list of branches already merged
git branch --merged

// Merge the changes in feature branch to master
git merge <branch-name>

// Push the changes to master
git push origin master

Deleting branches

Delete a branch locally:

git branch -d <branch-name>

Delete the remote branch:

git push origin --delete <branch-name>