44
How to make friends with git

How to make friends with git

Embed Size (px)

Citation preview

How to make friends with git

Before start

Pro GIT by Scott Chacon & Ben Straub

https://git-scm.com/book/en/v2

How git works?

Basics

>> git commit

Basics

Basics

Basics

The three states

1. Modified

2. Staged (git add)

3. Committed

File statusFile in the project

Tracked Untrackedwas in your last commit was not in your last commit

git status

Staged

Modified

Untracked} NOT STAGED

git add filename or

git add -A

Lifecycle

Committing

Committing

Branching

git branch testing

Branching

git branch testing

Branchinggit checkout testing

Branchinggit commit -am “testing changes”

Branching

git checkout master

Branchinggit commit -am “master changes”

Merging

Merginggit checkout master git merge hotfix

Merging

Merginggit checkout master git merge iss53

Merginggit branch -d iss53

Remote branches

git clone -o booyah => booyah/master

Remote branches

Remote branches

Remote branches

Rebase

git checkout experiment git rebase master

Rebase

• Going to the common ancestor of the two branches • Getting the diff introduced by each commit of the branch

you’re on • Saving those diffs to temporary files • Resetting the current branch to the same commit as the

branch you are rebasing onto • Applying each change in turn.

Rebase

Rebase

Be careful with it!!!Do not rebase commits that exist outside your repository.

If you are thinking what is better — choose merge

What to do if something went wrong?

I’ve forgotten to add some files to commit or mess up with message

git commit --amend

• If you had made no changes it would change commit message

• If you had added untracked file to stage it will add it to your commit

I’ve staged wrong filegit reset HEAD filename

Unstages file with name filename

While git reset can be a dangerous command if you call it with --hard, in this instance the file in your working directory is not touched. Calling git reset without an option is not dangerous - it only touches your staging area.

I’ve modified wrong file

Discards all changes in the file filename

git checkout -- filename

I need to undo my commit

Moves changes of commit_checksum to front

git revert commit_checksum

I need to remove my commit

reseting branch point to commit_checksum

git reset —HARD commit_checksum

I need to move commit from one branch to another

applying commit C to current branch

git cherry-pick C

I want to switch branch, but don’t want to commit

Store you changes, but without committing.

git stash; git stash apply

Command line tips&tricks

Operation with files• git status -s — short status • git diff — actual changes • git diff —staged — staged changes • git commit -m “message” — commit with a message • git commit -am “message” — stages all the tracked files • git branch -vv — shows all branches (including remotes) • git rm filename — untrack file • git mv file_from file_to — renames (moves) file_from to

file_to

History• git log — shows commits history • git log -p — shows the difference introduced in each

commit • git log —pretty=oneline — print all in one line • git log —since=2.weeks — print only commits for 2

weeks • git log --oneline --decorate --graph —all - draw a

graph of branches • etc…

What’s next?

• Introduction to machine learning

• Advanced data structures and algorithms