references:
per il commit è necessario impostare email e username, si vedono con
git config -l
e si impostano con:
git config --global user.name "username" git config --global user.email test@gmail.com
altri default utili:
git config --global color.diff auto git config --global color.status auto git config --global color.branch auto git config --global diff.external meld git config --global credential.helper cache
per gestire il merge e i conflitti
git mergetool -t meld
iniziare il repo:
git clone $url
iniziare localmente:
git init git push origin master git push -u origin master # -u o --set-upstream: collega una branch a un repo remoto
git init git add . git commit -m "first commit" git remote add origin https://github.com/devsmt/$repo.git git push -u origin master
Each repository is given a nickname, which is called a "remote". The name of this new remote repository is "origin", and it can be anything else. "master" branch is the same as the Subversion "trunk".
git remote add origin test@gmail.com:philsturgeon/pyrocms.git
aggiungere al repo:
# invia i cambiamenti solo dei files già sotto versione, ignora gli altri git commit -m "$msg" # oppure git commit -am "$msg" git push
Modify the Previous Commit's Message:
git commit --amend
error: Your local changes to the following files would be overwritten by merge:
ripristina un file
git fetch git checkout origin/master $file_name # metodo 2 git checkout -- $file_name git checkout --theirs $file_name
abbandona tutte le modifiche locali:
# metodo 1 git reset --hard HEAD # metodo 2 git fetch git reset --hard origin/master
# elimina file locali non versionati, non lascia backups git clean -f -d
ripristinare files eliminati:
git ls-files -d | xargs git checkout --
merge:
git add *
git commit -a -m "commit message"
git fetch origin master git merge -s recursive -X theirs origin/master
tornare a X commit precedenti:
git reset --hard HEAD~5
revert crea un commit inverso(toglie dove aggiunto, aggiunge dove tolto...) dei commit già inviati
# ripostare allo stato precedente il repo git revert HEAD # undo single last commit git revert HEAD^ # elimina un vecchio commit, il 3 dalla head git revert HEAD~3 # undo degli ultimi 13 commit a partire da HEAD git revert HEAD~13..HEAD # ritorna a un particolare commit git revert hashnumber
NOTA:
-n --no-commit
Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.
This is useful when reverting more than one commits' effect to your index in a row.
revert creando una Branch dei commit che risultano errati(rework)
# Let's assume that you're on the master branch and the commit you want to go back to is c2e7af2b51. #Rename your current master branch: git branch -m $branchname #Check out your good commit: git checkout c2e7af2b51 #Make your new master branch here: git checkout -b master #Now you still have your crazy experiment around if you want to look at it later, but your master branch is back at your last known good point, #ready to be added to. If you really want to throw away your experiment, you can use: git branch -D $branchname # carica/scarica commit su uno specifico branch remoto git branch --set-upstream-to=origin/$branchname $branchname
tutti i file modificati di un determinato commit (o range di commit).
git diff --name-only oldsha newsha | zip destinazione.zip -@
git gc git gc --aggressive git prune git-prune $ git-gc --aggressive $ git-repack $ git-repack -a $ git-prune-packed
Git allows you to create a local branch , check it out, work on it and commit to it all offline without contacting the remote repo. Eg:
git branch ticket5 git checkout ticket5 git commit -am "Commiting changes"
Then when you are done, you may send all of your commits to the remote branch in 'origin' using push again.
git push origin ticket5
you work on a copy of that branch then send that copy to the repo when you are done.
This command will create a new head with the given name, and point that head at the requested commit object. If the commit object is left out, it will point to HEAD.
Create and Checkout a New Branch:
# branches from currently checked out directory git checkout -b $branchName
Checkout a Remote Branch:
git checkout -b <localBranchName> origin/<remoteBranchName>
git checkout master git merge iss53
applicare i cambiamenti del branch sul master, posizionarsi su una copia di master:
git checkout master git pull origin master git merge test git push origin master git push -u origin master
push all your branches to the remote, and set-upstream tracking correctly
git push --all -u
git checkout -b <branchName> # edit files, add and commit ... git push -u origin <branchName> # -u alias of --set-upstream
Tagging, Deleting, and Pushing Tags
# Create a Tag git tag <tagName> # Delete the tag git tag -d <tagName> # Push Tags git push --tags
similar to svn externals. but:
The git submodule is bound to the revision that you give it. va aggiornato e committato separatamente perché il progetto principale ne ignora i cambiamenti.
git submodule add git://github.com/sympal/sympal.git plugins/sfSympalPlugin # scarica il submodule git submodule update --init --recursive
checking out a tag that you want the submodule to "stick" at. guida submodule
git blame <fileName>
# a un Fork locale, aggiungi il riferimento al master remoto git remote add fuel-core https://github.com/fuel/core.git # merge modifiche remote git fetch fuel-core git merge fuel-core/1.1/develop git status # push al Fork su GitHub git push
per aggiornare il codice di un fork:
# After your fork is made, clone your fork to your local machine: git clone test@gmail.com:YOURNAME/$project.git # Add the original $project repository as a remote repository. You'll use this later to fetch changes from the $project repository. #This will let you stay up to date with $project: cd $project git remote add upstream git://github.com/$project/$project.git
fatal: Authentication failed for 'https:...'
ricorda che va usato il token e non la password se si ablita 2FA(two factor authentication)
1.7.9 and later:
# default 900s git config --global credential.helper cache # 10h git config --global credential.helper 'cache --timeout=36000' # store to file unencripted git config --global credential.helper store --file ~/.git-credentials
for HTTPS repositories:
gedit .git/config # find the url and paste the credentials: http://username:password@bitbucket.org/...
for HTTPS repo with "2 factor Auth"(Github), use the token as a password
# differenze della versione attuale rispetto al remote, ma ignora whitespaces git diff -b -w # cosa è stato fatto oggi? git log --pretty=short --since="1 days ago" --author devsmt --no-merges git config --global diff.external meld
setup visual diff: salva un wrapper in /user/local/bin/git_diff.py git passa 7 parametri al difftool, meld ne vuole 2:
#!/usr/bin/python import sys import os os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))
Now we can set Git to perform it's diff on our new script :
git config --global diff.external git_diff.py git diff filename
git log $path git diff 9ff9956ddf50a40e82a92d7692b230c55dad5577 -- $path git difftool -y 9ff9956ddf50a40e82a92d7692b230c55dad5577 -- $path git diff HEAD~2 HEAD -- filename.txt
ottenere la versione corrente( utile in un post commit hook):
#short git log --pretty=format:'%h' -n 1 #long git log --pretty=format:'%H' -n 1
"The following untracked working tree files would be overwritten"
# fai commit locale per mantenere le modifiche gitg . # estrae l'ultima versione da remoto git checkout HEAD^ templates/dir/index.php git pull # dovrebbe fare merge e eventualmente dare errore sui conflitti da risolvere a mano
git pull error: Your local changes to the following files would be overwritten by "merge"
git fetch --all git reset --hard origin/master
git fetch downloads the latest from remote without trying to merge or rebase anything.
Then the git reset resets the master branch to what you just fetched. The hard option changes all the files in your working tree to match the files in origin/master, so if you have any local changes, they will be lost. With or without hard, any local commits that haven't been pushed will be lost.
usare git stash , le modifiche locali vengono messe in un commit temporaneo e tolte, in modo da poter facilmente fare merge con una versione remota in conflitto. il commit temporaneo può essere poi applicato separatamente.
git stash apply
# After changes have been made... git stash # Do some other stuff here, like switch branches, merge other changes, etc. #Re-apply the changes git stash pop
prendere differenze da remoto e controllare l eultime modifiche
git fetch git log -p HEAD..FETCH_HEAD
1. Initialize a local repository
git init <directory>
The
2. Clone a remote repository
git clone <url>
3. Add a file to the staging area
git add <file>
To add all files in the current directory, use . in place of
git add .
4. Commit changes
git commit -m "<message>"
If you want to add all changes made to tracked files & commit
git commit -a -m "<message>"
git commit -am "<message>"
5. Remove a file from the staging area
git reset <file>
6. Move or rename a file
git mv <current path> <new path>
7. Remove a file from the repository
git rm <file>
You can also remove it from staging area only using --cached flag
git rm --cached <file>
Basic Git Concepts Default branch name: main Default remote name: origin Current branch reference: HEAD Parent of HEAD: HEAD^ or HEAD~1 Grandparent of HEAD: HEAD^^ or HEAD~2 13. Display branches
git branch
Useful flags:
-a: Display all branches (local & remote) -r: Display remote branches -v: Display branches with last commit 14. Create a branch
git branch <branch>
You can create a branch and switch to it using the checkout command.
git checkout -b <branch>
15. Switch to a branch
git checkout <branch>
16. Delete a branch
git branch -d <branch>
You can also force delete a branch using the -D flag.
git branch -D <branch>
17. Merge a branch
git merge <branch to merge into HEAD>
Useful flags:
no-ff: Create a merge commit even if the merge resolves as a fast-forward squash: Squash all commits from the specified branch into a single commit Fast forward Merge Fast-forward-merge
Non-Fast forward Merge No-fast-forward-merge
It is suggested to not use the --squash flag as it will squash all commits into a single commit, leading to a messy commit history.
18. Rebase a branch Rebasing is the process of moving or combining a sequence of commits to a new base commit
Rebase
git rebase <branch to rebase from>
19. Checkout a previous commit
git checkout <commit id>
20. Revert a commit
git revert <commit id>
21. Reset a commit
git reset <commit id>
You can also add the --hard flag to delete all changes, but use it with caution.
git reset --hard <commit id>
22. Check out the status of the repository
git status
23. Display the commit history
git log
24. Display the changes to unstaged files
git diff
You can also use the --staged flag to display the changes to staged files.
git diff --staged
25. Display the changes between two commits
git diff <commit id 01> <commit id 02>
26. Stash changes The stash allows you to temporarily store changes without committing them.
git stash
You can also add a message to the stash.
git stash save "<message>"
27. List stashes
git stash list
28. Apply a stash Applying the stash will NOT remove it from the stash list.
git stash apply <stash id>
If you do not specify the
You can also use the format stash@{
git stash apply stash@{0}
29. Remove a stash
git stash drop <stash id>
30. Remove all stashes
git stash clear
31. Apply and remove a stash
git stash pop <stash id>
32. Display the changes in a stash
git stash show <stash id>
33. Add a remote repository
git remote add <remote name> <url>
34. Display remote repositories
git remote
Add a -v flag to display the URLs of the remote repositories.
git remote -v
35. Remove a remote repository
git remote remove <remote name>
36 Rename a remote repository
git remote rename <old name> <new name>
37. Fetch changes from a remote repository
git fetch <remote name>
38. Fetch changes from a particular branch
git fetch <remote name> <branch>
39. Pull changes from a remote repository
git pull <remote name> <branch>
40. Push changes to a remote repository
git push <remote name>
41. Push changes to a particular branch
git push <remote name> <branch>