Created by Alexey Novak / @twitanvk
When should I use "git lost" or "git life" or "git cherry-pick"?
Do not panic, this presentation should give you an idea what to use and when.
You better do it if you have not done it yet
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Helps to find you code in history and tells everyone who wrote that amazing chunk of code
Get the script first
cd ~
curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
Add those lines to your ~/.bash_profile
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
Windows users can do all those changes through GitBash or other Linux-like consoles
This change is a pure joy for anyone who has to use git CLI
From the little steps to the big git-based projects
mkdir my-project
...
cd ./my-project
...
git init
...
git remote add origin httplink-to-the-remote
Most of the time need to be done only once... unless you messed remote link
git fetch origin
It does not effect your local files. Try to do it often to be aware of changes on the remote.
git remote
...
git branch
List existing branches and remotes
git checkout localBranch
Switching between branches
git fetch remoteName
...
git checkout localBranch
...
git pull remoteName remoteBranch
Let me show you something more familiar ->
git fetch origin
...
git checkout master
...
git pull origin master
We just merged latest changes from remote master branch into our local master branch
git checkout -b localBranch remote/remoteBranch
Let me show you something more familiar ->
git checkout -b staging origin/staging
You create your branch once until the moment you delete or rename it
Aha! Gotcha, not a big deal
git checkout localBranch
...
git checkout -b newBranch
Let me show you something more familiar ->
git checkout master
...
git checkout -b newFeature
git branch -D localBranch
It will remove your branch without any questions. Small d will prevent branch from deletion if its changes are not present in the master
Take a look at my Big D. It is all matter of taste and size does not matter so far if you know what you are doing.
If not, then maybe it is time to switch back to SVN why to bother with GIT ?
The process of creation
If you have not worked on "this" feature/fix before then create a branch for it.
Cannot stress enough - check what branch are you on before making any changes
git status
Will show you all untracked, changed, removed and created files
git diff
Will show you all changes you have done. They are better be good!
git add fileName
...
git add path/folder/fileName
...
git add path/folder/
This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit.git add documentation
git reset path/folder/fileName
Will unstage the file so that it would not be commited
git checkout -- path/folder/fileName
Will reset file changes to the last commit
git rm fileName
...
git rm path/folder/fileName
...
git rm path/folder/
git commit -m "some message"
"git commit -a" a pure evil or a misunderstood command?
This command will push your current local branch to the remote.
git push remoteName localBranch
Let me show you something more familiar
git push origin newFeature
Or something even more familiar
git push origin master
of course it is the easiest scenario and there are more complex usecases... we will get there
Think of "git add" as a process of packing your items into a box ready to be sent to your friends.
While "git commit" would be a process of sealing the box.
While "git push" would be you sending.
You can put many items in a box. (add)
You look only into one box at a time. (commit)
You can send many boxes. (push)
git reset --hard
It will reset your local file sytem to a branch's HEAD
Maybe stash would work for you better?
Use GIT often. Use GIT branches.
Scenario: Creating a new feature and pushing it to master branch
origin master: o-?
local * master: o
git checkout master
Switch the branch to master since we want to create our new feature based on it
origin master: o-?
local * master: o
git checkout master
git fetch origin
Update your local stack with knowledge about remote
origin master: o-o
local * master: o
git checkout master
git fetch origin
git pull origin master
Update your local master branch with changes from remote
origin master: o-o
local * master: o-o
git checkout master
git fetch origin
git pull origin master
git checkout -b newFeature
Create new branch for your feature/change
origin master: o-o-?
local master: o-o
* newFeature: o-o
make changes
git add blah
git commit -m "blah one more time"
...
Make and commit new changes
origin master: o-o-?
local master: o-o
* newFeature: o-o-x-x-x
git checkout master
git fetch origin
git pull origin master
Update your master one more time before push since there might be changes since the last time you updated your local stack
origin master: o-o-o
local * master: o-o-o
newFeature: o-o-x-x-x
git merge newFeature
git push origin master
Merge your branch into master and push it to the remote
If there is a conflict then resolve it.
Resolution will be a separate commit
origin master: o-o-o-y
local * master: o-o-o-y
newFeature: o-o-x-x-x
"y" will be a merge commit for your master which represents "x-x-x" changes
git stash
(allows to save your changes without a commit)git cherry-pick
(allows to move specific commits from branch to branch)git rebase
(just like cherry-pick but for multiple commits. Also allows to change commit messages)Add them to .gitignore
Files must be in gitignore and untracked in order to be ignored
Example of .gitignore
*.exe
*.db
Web.config
App_Code/
!App_code/MyImportantClass.cs
!start.exe
Use ! to make exceptions
Just like in coding - keep your .gitignore file as minimal as possible
Your tool to simplify your work even further...
Helping to visualize your ugly console commands
Well not only that to be honest ;)
It is fun and educational. Do not be lazy and try it out! But it will require a github account to complete. Perfect timing to create if you do not have one.
Any questions?