Git - version control system

Git vs SVN popularity:

git commands

git status
git commit -a
git push
git pull

Local Changes

Changed files in your working directory:
git status

Changes to tracked files:
git diff

View changes in file.txt:
git diff path/to/file.txt

Add all changes to the next commit:
git add .

Commit all staged changes:
git commit -m "Added header - JIRA-777"

Commit History

Show all commits, starting with newest:
git log

Show changes for a file.txt
git log -p path/to/file.txt

Show log in shorter view:
git log --oneline

Who changed what in file.txt
git blame path/to/file.txt

Branches & Tags

List all branches:
git branch -av

Switch to develop branch:
git checkout develop

Create new branch based on your current HEAD (based upon whatever branch you were on):
git branch new_branch

Delete local branch:
git branch -d branch-name

Delete local branch even if it is not fully merged:
dit branch -D branch-name

Merge & Rebase

Rebase commits on top of develop branch:
git pull --rebase origin develop

Continue the rebase (Add file with "git add path/to/file.txt" after resolving conflicts in the file):
git rebase --continue

Abort the rebase:
git rebase --abort

Advanced

Squash number of commits:

  • git rebase -i HEAD~2
  • git push --force

Working with stash

Save changes to stach:
git stash save "Updated code"

Show list of changes in stash:
git stash list

Apply stash with ID=0:
git stash apply stash@{0}

Delete stash with ID=0:
git stash drop stash@{0}

Searching in Git

Search in repository:
git grep "Search string"

Search 'target' in /custom/folder:
grep "target" /custom/folder

Search 'target' in any case (i.e. case insensitive search):
grep -i "target"

Search 'target' or 'other':
grep -E "target|other"

==========================

Checkout the "feature/branch-001" branch:

  • git pull origin feature/branch-001
  • git checkout -f feature/branch-001

Push:

  • git push

Push to specific branch:

  • git push -u origin feature/ABC-321

Reset recent commit:

  • git reset --hard

Get latest develop branch into your branch:

  • git pull origin develop

Get latest version of styles.css file from develop branch:

  • git pull origin develop web/css/styles.css

Leave a Comment