Git commands

Git SCM

Base

Get help:

git help <command>
git <command> --help
man git-<command>

Set git user username and email:

# globally (for current user)
git config --global user.name "Some One"
git config --global user.email "someone@mail.com"
# locally (for current project)
git config user.name "Some One"
git config user.email "someone@mail.com"

Manage repositories

Init:

git init .
git add main.c module.c
git commit -a -m 'init'

Add a remote repository:

git remote add github git@github.com:username/project.git
# or edit .git/config manually

Get information

View untracked files:

git status

View file history:

git log my_file

View commits differences:

git diff xxxxxxx..xxxxxxx # xxxxxxx - commit hash
git diff master..my_branch
# current uncommited changes:
git diff

Diff for specified user:

git diff master..my_branch --author 'User Name'

View list of changed files:

git diff --name-only master..some_branch
git diff --name-only SHA1 SHA2
git diff --name-only HEAD~10 HEAD~5

Show list of commits:

git log
# for user
got log --author 'User Name'

Show file from specified revision:

git show <treeish>:<file>
git show HEAD~4:tests.py

Show changes history for specified line of code:

git blame path/to/file -L <line number>

Branches

Create a new branch:

git branch my_branch_name
git checkout my_branch_name
# or simpler:
git checkout -b my_branch_name

Pull the latest branch code from remote repository:

git pull origin my_branch

Pull latest changes from remote repository and ignore local changes:

git fetch
git reset --hard origin/mybranch

Delete a local branch:

git branch -d the_local_branch

Delete a remote branch:

git push origin --delete branch_to_remove

Copy branch from origin:

git fetch
git checkout -b new_branch origin/new_branch

Merge branch:

git commit -a -m 'some changes'
git checkout master
git merge my_branch --no-ff

Work with codebase

Push change to remote repository:

# some changes
git commit -a -m 'Something was changed'
git push origin master

Move or delete files:

git mv my_file.c new_name.c
git rm my_file.c

Restore deleted files:

git checkout deleted_file.c

Extend latest commit:

# changes
git commit -a -m 'Something was changed'
git add some_new_file.c
git commit -a --amend

Rollback the last commit:

git reset --hard HEAD^

Revert changes in one file:

# upstream master
git checkout origin/master -- filename
# the version from the most recent commit
git checkout HEAD -- filename
# the version before the most recent commit
git checkout HEAD^ -- filename

Copy commit from one branch to another (cherry-pick):

git cherry-pick xxxxxxx # xxxxxxx - commit hash

Hide/restore not commited changes (stashing):

# move uncommented changes to stack
git stash
# show the stack
git stash list
# restore latest stashed changes
git stash apply

Rebase (interactive):

git checkout master
git pull origin master
fir checkout my_branch
git rebase -i origin/master
# follow instructions
# to squash all commits into one: mark all commits with "s" except the first one
# force push:
git push -f origin my_branch

Links:

Licensed under CC BY-SA 3.0