GIT: Basic commands in git

Create GIT Repository
robert@ubuntu:~$ mkdir test
robert@ubuntu:~/test$ cd test
robert@ubuntu:~/test$ git init
Initialized empty git repository in /home/robert/test/.git/

Cloning an Existing Repository from remote server
$ git clone git://github.com/schacon/grit.git
or in specific directory
$ git clone git://github.com/schacon/grit.git git-directory

Show log
$ git log

Show user log
$ git log –author=”Jon”

Show all remote repositories
$ git remote
origin

Show all remote repositories with shortcut
$ git remote -v
origin git://github.com/user1/repo.git

Show all information about remote repositories
$ git remote show origin

Add remote repository
git remote add [shortcut] [url]:

$ git remote
origin
$ git remote add pb git://github.com/user2/test.git
$ git remote -v
origin git://github.com/user1/repo.git
pb git://github.com/user2/test.git

Fetch from Remote
To get data from your remote project
$ git fetch [remote-name]

Pull from Remote
To get data from your remote project and automatic scal with local code
$ git pull

Push to Remote
To push data to remote project (you must have rights and nobody push changes)

git push [remote-name] [branch-name]
$ git push origin is123

or

git push origin [local-branch]:[remote-branch]
$ git push origin is123:is123

Push to different Remote branch
git push origin [local-branch]:[remote-branch]
$ git push origin is123:ex22

Push a new local branch „branch_name” to the remote Git repo and track it
$ git push -u origin branch_name

Unctracked files (unstage files) – those files that have been added inside „test” directory but not been added yet to git repository.
$ touch test1.txt test2.txt test3.txt

Add „test1.txt” file to GIT Repository (stage files)
$ git add test1.txt

Add /home/robert/test/dir1/dir2 folder to GIT Repository (stage files)
$ git add dir1/dir2/

Add all files and folders to GIT Repository (stage files)
$ git add .

Commit changes and add comment
$ git commit -m „Add test1.txt or all files to repository”

Modify last commit (change comment or add files)
$ git commit –amend

Add „test1.txt” file to GIT Repository and Commit it
$ git commit -a -m „Add test1.txt to repository and commit”

Information about Repository
$ git status

Unstaged file
$ git reset HEAD file.php

Unstaged php files
$ git reset HEAD *.php

Staged files
$ git reset HEAD

Unctracked files
$ git clean -f

Show changes between the working tree and your HEAD commit (staged and unstaged changes together):
$ git diff HEAD

Temporarily switch to a different commit
$ git checkout (commit)

„Git revert” is a “safe” way to undo changes, „git reset” is the dangerous method. It is only git command that has the potential to lose your work. It should only be used to undo local changes. You should never reset snapshots that have been shared with other developers.

Revert single commit (safe)
To undo the changes in a particular revision without destroying any of the history.
$ git revert HEAD

Revert to specific commit (safe)
$ git revert (commit)

Revert last commit (safe)
$ git reset –soft HEAD~1

Revert all changes (unsafe – delete all commits history after revert commit)
$ git reset –hard HEAD

Revert any commits (unsafe – delete all commits history after revert commit)
To rewrite history and remove any commits that were made after that point.
$ git reset –hard be67ce2

Review Modifications
$ git diff

How to move files in git?
$ git mv dir1 dir2

Git Ignored txt files
$ vi .git/info/exclude
*.txt

Show all branches
$ git branch

*branch_name
master

Create branch „branch_name”
$ git branch branch_name

Create branch „branch_name” and switch to it
$ git checkout -b branch_name

Switch to „master” branch
$ git checkout master

branch_name
*master

Rename the branch (you must be on the branch you want to rename)
$ git branch -m newname

Marge „branch_name” to „master” branch (you must be on the „master: branch!)
$ git checkout master

branch_name
*master

$ git merge branch_name

Show all avaiable tags
$ git tag

Add light tag
$ git tag v1.4

$ git tag

v1.3
v1.4

Push all local tags to remote
$ git push origin –tags

Checkout new remote branch
$ git checkout -b new-local-branch origin/new-remote-branch
or
$ git checkout -t origin/branch-name

Diff the same file between two different commits on the same branch
$ git diff $start_commit..$end_commit — path/to/file

Difference for a file „file.txt” between now and two commits back
$ git diff HEAD^^ HEAD file.txt

Comparing two branches
$ git diff branch_1..branch_2
$ git diff branch_1…branch_2

Show commit detail
$ git show