12 Sep How to Use Git? importants tips
Now before to start i’ll show you a little introduction about git and the benefits to use it
$ git help command e.g. git help diff
To use a certain name of user in the commit we can declare that
$ git config --global user.name “heriberto perez”
$ git config --global user.email heriberto.perez@crowdint.com
To initialize a one repository you must located in the dir that you want and run
$ git init
To see what changes there are after the last commit
$ git status
To see the history of the commits in the repo that you are done severals commits
$ git log
There are diferent ways to use the git add the next are some examples of how to use the git add for example:
To add only the files that you want
$ git add listfiles in the same line
To add all the files pendents to add
$ git add --all
To add all the files pendents too
$ git add .
To add all the files with the extension txt
$ git add *.txt
To add all the files in the directories dir
$ git add directories/*
To add all the files txt in the dir directories
$ git add directories/*.txt
To see the diferences or the changes in some files or after the last commit you can run
$ git diff
but if before that you run git add the changes are staged and you dont can see that changes then for see that changes you can run
$ git diff --staged
if you have for example one file added to commit and before the commit you dont want that files that is the staged you can unstaged using the command
$ git reset HEAD filename
After that command you return to the before commit and the changes will dissapear.
But if you done some modifications to some file and you want disacer that modification you can use the command checkout and the file will be the origin at the before commit
$ git checkout filename
If you want to do more fast to add some files to the commit you can use the next command:
$ git commit -a -m “Add some files from tracked or staged files directly”
For example if some commit was wrong and we can undo that modification we can run and the files or modification return at the point before to do the commit and the files modification will located in the staged
$ git reset --soft HEAD^
Note important: Dont forget the symbol ^ after HEAD
Maybe if we forgot to add some files at the previous commit we can modify that without return to the before commit for example we only add the file and use ammend like:
$ git add file.txt $ git commit --amend -m “Adding some files and file.txt”
If we dont sure if dont need a file and we can delete but maybe in the future you can use that for example you can do the next example code
$ git rm public/index.html
At this point the file is delete for git and you can commit that file
$ git commit -m “Delete the index file in public directory”
But later you need that file again you can do this usando reset you can undo that commit and specify what commit depending in what number of commit yo want to undo for example 2 commit before
$ git reset --soft HEAD~2
to see the files modification created, or deleted in that commit
$ git status
Now if we can see the file that we need we can recover like:
$ git reset HEAD public/index.html $ git checkout public/index.html
or you can use the
$ git checkout -- public/index.html
The before line is to avoid to checkout accidentally a branch just one that.
And if you need checkout more than one file you can use the next example:
$ git checkout -- files1.html file2.html file3.html
You can do a commit in one line at the same time add the new files and commit that with:
$ git commit -a -m "the pet shop will soon be offering badgers"
The problem with the before aproximation is that we have more files modification or added in the next commit at the before laste all the files involucrated will be in staged and not only the file that we want to recover.
Now if we can destroy all the changes and not only put that files in the staged we can use:
$ git reset --hard HEAD^
or if we want destroy all the changes done in the last 2 commit we can run:
$ git reset --hard HEAD^^
To clean all the files untracked before to do one commit we can run
$ git clean -df
To upload a one repo to another remote computer or server
$ git push
To download from another server a repository
$ git pull
REMOTE REPOSITORY
To add a repository and linked that we can use the next code in this example we can use github.com and before that we must create the repo and we need access or configured our SSH keys from github we can find the tutorial how to realize that from github.https://help.github.com/articles/generating-ssh-keys
Important note: if we don’t configure the ssh keys when we run push (explained in future) we need to specify in every push
Then after to create our repository we run:
$ git remote add origin address
Where
origin: is the name for this remote repository and and we can name that as we want
address: the address where we have located our repository
add: especify that we have a new repo
This is a good example
$ git remote add origin git@github.com/heridev/example-of-aplication-backbone.git
For example to link another remote repository for example bitbucket
$ git remote add bitbucket direccion-de-shell-bitbucket
To do pull or push you can run
$ git push bitbucket
To see our repository we can run:
$ git remote -v
Now to push for example we can run:
$ git push -u origin master
Where
origin: is the remote branch
master: is the local branch that we can push to the remote server or computer
If for example one more person is working in the same repo and we need to dowload his modification we can run:
$ git pull
Important Note: after to do git push -u origin master in the futures push or pull we dont need to specify that and we only need run git push or git pull
How do we start collaborating?
To begin all the team need the repo cloned in his machine pc or mac to do that we need to search the address for the repo for example i have the next repo
$ git remote add origin git@github.com/heridev/example-of-aplication-backbone.git
Now lets go to clone that in our mac.
$ git clone git@github.com/heridev/example-of-aplication-backbone.git
And after to download the repo we have a directory called like the name repository and we can see that if we moved intro the directory
$ cd example-of-application-backbone
But we can called the directorie like we want for example if we can rename the repo as backboneapp only we need to specify the name after the address repo, for example:
$ git clone git@github.com/heridev/example-of-aplication-backbone.git
After that we downloaded the repo we can see the list of our remote with the code:
$ git remote -v
So to begin working in the project and somebody want to add some feature at the origin project the best is work in a new branch and dont modify the origin master branch for create a new branch we can code:
$ git branch feature_new
now if we code:
$ git branch
We can see all the branchs that exist, and to switch to another branch we code in terminal:
$ git branch feature_new
And now we can start to working the new feature independent to the master origin branch, for example if we add some text to somefile and we do a commit with that changed, that changed only exist in the branch feature_new and if we switch to the master branch that modification will not exit in that branch.
Later that we have a new feature finished we can merge the new feature to the master and updated with the next useful feature for do that we switch to master branch
$ git checkout master
And we do the merge with the branch that we need in this case feature_need
$ git merge feature_new
Now that feature and new modification exist in the master branch.
To create a new branch and swith to that we can code only one line and run that operations more fast with the next.
$ git checkout -b feature_admin
LEVEL 4 Colaboration basics
colaboration scenarios when in one project we need colaborate.
Example of one Scenario :
Jane and Gregg want to colaborate in one project in common then both of them clone one project and they started to work in the repositorie but one of they do one commit and push his changes and the other one do his own commit in local so at the moment when he wants to do push at the remote repository they receive a git push rejected because his repo loca is not update then the solution to this problem is do before to send our own commit this is the process
Do our own commit and add the new features
$ git status $ git add --all # or only the files that we need $ git commit -m “Add some awesome feature”
Then before to send our own commit we check if our local repo is updated
$ git pull
Now we can to do our push to remote repo
$ git push
Now let get to see a one scenario for explain the merge conflicts
For example mark and jhon are working in the same file for example : Readme file and they write diferente content in her own file then mark do its own commit and push to the remote repo, and later jhon do its own commit and wants to send its push to remote repo then at this point he will receive merge conflic at the moment at do $ git pull to update his local repo then will need to resolve that merge conflict cleaning and updating for example manually that file.
When he runs
$ git status
He can see the files modified after the pull request then he found a file with the next code example:
heres is the readme file
<<<<<<<<<<HEAD the cake is lie =============== the cake is telling the truth >>>>>>>>>>>>>>>> 4de849jdk493k3k4o393k3k3k4plñdpd
Then to resolve he can do manually and clean the file and delete the extra text
the he can run after clean the file readme
$ git commit -a
And now he can update the remote repo
$ git push
LEVEL 5 BRANCHING
creating remote branch
$ git checkout -b shooping_cart $ git push origin shooping_cart
Now let’s go to update a new feature in our new branch
$ git add cart.rb $ git commit -a -m “Add basic cart feature”
At the moment at do git push we’re updated our branch shooping because we’re checkout in the branch shooping cart
$ git push
Now when other colaborator do
$ git pull
Now he will have the new branches in your local repo
for example to clone one remote branch
$ git pull origin remote_branch $ git fetch
But at this moment if he run:
$ git branch
Only we can see the master branch but if we run
$ git branch -r
Now appear all the list remote branches but to use the new branch remote only we need to checkout to the correct branch
$ git checkout shooping_cart
And now we can contribute and push in that branch
To show all the remote branches related with origin we run:
$ git remote show origin
To remove a remote branch
$ git push origin :shooping_cart
And later we need to delete our local branch at the same name
$ git branch -d shooping_cart
Now we will receive and alert that continue with the delete branch we need to run:
$ git branch -D shooping_cart
Now if other colaborator push at that that branch only update in the local branch because that branch doesnt exist in remote repository if we run
$ git remote show origin
After that code we can see that the branch doesnt exist
And then we can run to tidy prun branches, and clean up deleted remote branches
$ git remote prune origin
TAGGIN
A tag is a reference to a commit
Using tags is for releases of one product, and now let go to see how to use the tags
List all the tags
$ git tag
Checkout code at one commit
$ git checkout v0.0.1
To add one tag
$ git tag -a v0.0.3 -m “Version 0.0.3”
To push new tags
$ git push --tags
RETRIEVE TAG
The client is requesting that you roll back to the prior release. (Seriously? What could have gone wrong with the hamsters?) Retrieve the release tagged “v1.3.1”.
$ git checkout v1.3.1
Rebase Belong to us
To avoid the merge origin master commit we can use the rebase command like the next for example in remote repo
For example the common way is run the next next when we need to update our local repo from remote repository:
$ git pull $ git push
And after that we can push our local updates.
Other way to do this is doing the next code:
$ git fetch #but does not merge $ git rebase
and doesn’t there are merge commit
For local repo
$ git checkout master $ touch anyfile.rb $ git add anyfile.rb $ git commit -m “Add anyfile.rb in master” $ git checkout -b new_branch $ touch otherfile.rb $ git add . $ git commit -m “Add otherfile.rb from new_brach” $ git rebase master $ git checkout master $ git merge new_branch
Later to do this code we don’t have a commit only for the merge “Merge branch ‘new_branch’”
But What with the conflicts?
For example two colaborators edit the same file and we will have and conflict the way to treat that conflicts is doing the next:
$ git fetch $ git rebase # we move all the new to a temporal area
After that we receive and message where inform about the conflict in specifics files and show us what code we must run after resolve that problems, the next code we can use:
when you have resolved this problem run
$ git rebase --continue
if you would prefer skip this patch:
$ git rebase --skip to check out the original branch and stop rebasing run $ git rebase --abort
later we need to check the status
$ git status
Now we can see the message where show us that the files was modified and is not in any branch, later that we edited the file readme we can continue with the next code:
$ git add Readme $ git rebase --continue
And now the rebase and updated will take change.
At continue we found some examples using rebase:
You’ve made some commits to a feature branch, but you’ve also committed a hotfix on master that would make a merge messy. Check out the kennel branch so you can rebase it on master.
$ git checkout kennel
OK, you’re on the kennel branch. Our goal is to be able to merge kennel back into master without conflicts or a merge commit. Rebase the current kennel branch on master.
$ git rebase master
With the rebase complete, kennel should merge with master cleanly. Switch branches back to master.
$ git checkout master
We’re on master, and we know the kennel will merge cleanly. Go ahead and merge in the kennel branch.
$ git merge kennel
Your co-worker has pushed changes to the master branch on the origin repo. Retrieve it without merging it so we can replay our work on top of it.
$ git fetch
Now that your local repo knows of the latest changes on origin/master, move your master commits after the commits from origin/master.
$ git rebase
Your co-worker has pushed before you yet again. Better fetch the changes…
$ git fetch
Now run another rebase to move your commit after the latest fetched one.
$ git rebase
Uh, oh! Looks like the rebase is in conflict this time! Edit index.html to fix the conflicting lines. We want to keep our version with Cats and Dogs.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Our Cat-alog</title> </head> <body> <nav> <ul> <<<<<<< HEAD <li><a href="cat.html">Cats</a></li> <li><a href="dog.html">Dogs</a></li> ======= <li><a href="cat.html">Felines</a></li> <li><a href="dog.html">Canines</a></li> >>>>>>> Add dogs. </ul> </nav> </body> </html>
Now we edit the file and the file result looks like the next:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Our Cat-alog</title> </head> <body> <nav> <ul> <li><a href="cat.html">Cats</a></li> <li><a href="dog.html">Dogs</a></li> </ul> </nav> </body> </html>
Save the file index.html and we continue with the next command:
$ git add index.html
Now that all conflicts have been resolved and those files added, continue the current rebase in process.
$ git rebase --continue
LEVEL 7 HISTORY AND CONFIGURATION
To see list of commits history
$ git log
To enable color in git log
$ git config --global color.ui true
To show the list in one line
$ git log --pretty=oneline
To show the list commit like you want we can use the format like the next:
$ git log --pretty=format:"%h %ad- %s [%an]"
To see more options
$ git help log
And for patch
$ git log --oneline -p
and then we can see the lines where was updated in each commit
And to see in specific commit what lines were updated we can run:
$ git show hereyouputyourshacode --oneline -p
example
$ git show 7bfa7d22b70397dadf25023f2a99eb4079a530f5 --oneline -p
For see all the changes content in the list history commits
$ git log --oneline --patch
For see STATS
$ git log --oneline --stat
For see a little graphs
$ git log --oneline --graph
And if we want only show a date ranges of the history commits we can use:
$ git log --until=1.minute.ago $ git log --since=1.day.ago $ git log --since=1.hour.ago $ git log --since=2000-01-01 --until=2012-12-21
To see the changes before to do and commit
$ git diff
To see the changes staged and unstaged
$ git diff HEAD
To see last commit
$ git diff HEAD^
to see the before last commit
$ git diff HEAD^^
to see and specific commit
$ git diff HEAD~5
Diff between two branches
$ git diff master bird
To see who modified that file and when and what he add we can use blame
$ git blame index.html --date short
If we want to exclude some directories from our commits, we can edit the file
.git/info/exclude and we can add the directories that we want to exclude for example
experiments/
we can too exclude some files like
videe.mp4 *.mp4 #all the files videos experiment/ exclude directories
And too we can exclude using the file .gitignore
for example exclude all the files .log in the directory logs we need add this line in the file gitignore
logs/*.log
How to remove files from the repository
$ git rm file.html
Some global configuration
$ git config --global user.name “jose heriberto” $ git config --global user.email “<a href="mailto:heriberto.perez@crowdint.com">heriberto.perez@crowdint.com</a>” $ git config --global core.editor vim #what editor will use $ git config --gloabl merge.tool opendiff #use opendiff for merging conflicts
Will show us the current configuration
$ git config --list
Now if we code we can see the email configuration is currently using
$ git config user.email
We can use aliases for reduce the format of certainly codes or commands
for example to use some aliases to show certain format in the history commit
$ git config --global alias.mylog \ “log --pretty=format:’%h %s [%an] --graph’”
and for the common commands like git status, or git commit we can do for example:
$ git config --global alias.st status $ git config --global alias.co checkout $ git config --gloabl alias.br branch $ git config --gloabl alias.ci commit
If we want to use macvim to add some messages to our commit we can set our editor with the next command
git config --global core.editor "mvim -f"
If you want to set another editor you need to change mvim -f instead “nano” or “emacs” or whatever you want or need
And now if for example we need see what branchs there are we can run:
$ git br
Or for check the status
$ git st
Now lets go to see some examples
All those e-mail addresses and SHAs are making it hard to see commit messages in your history. Try viewing the log with one commit per line.
$ git log --oneline
The client called with an urgent question about chew toys, and now you can’t remember what you last modified. Bring up a summary of file changes.
$ git diff
You’ve finished adding elephants to the catalog. You need to write up a change log for the client, and you want to ensure you don’t miss anything. Compare the master branch to your elephant branch to show what’s new.
$ git diff master elephant
You rebased your latest commit after a commit from your co-worker, but now the page is rendering strangely. To figure out why, get a diff that includes the previous commit, as well as its parent.
$ git diff HEAD^^
Well, you see the changes, but you’re not sure what your co-worker was trying to accomplish. Display the diffs along with the log to determine what’s going on.
$ git log --oneline --patch
Wait, what? You don’t understand these lines in index.html. You’d better find out who committed them, so you can ask them what they’re supposed to do.
$ git blame index.html --date short
Your server writes files to the logs directory. You’re not going to commit them, but they keep appearing in your git status output, and you’d like them not to. Edit your .gitignore file to ignore all files in the logs/ directory whose names end in .log.
logs/*.log
Just for this repo, set the user.email value to admin@example.com.
$ git config user.email "admin@example.com"
Set your name as the user.name value for all Git repos.
git config --global user.name "heriberto perez"
commit just won’t suffice for your code. You need a command that conveys the importance of the work that you’re entrusting to Git. Alias git commit to git beholdmyamazingcode (or something equally awesome).
$ git config --global alias.beholdmyamazingcode commit
Gift Pet
Posted at 06:36h, 29 JuneAwesome things here. I’m very satisfied to see your article. Thank you a lot and I am taking a look ahead to contact you. Will you please drop me a mail?