7. Git - alexattia/myWiki GitHub Wiki
Git est un contrôleur de version.
Basic commands
git init
: initialize a new repo on your computer. Only after that command you can use the following ones.
git config
: to configurate git
git config --global user.name "Votre Nom Ici"
git config --global user.email "votre_email@votre_email.com"
git config --global http.proxy http://user:password@[etu]proxy.enpc.fr:3128 #proxy
git config --global --unset http.proxy #remove proxy
git help
: opens the help on your terminal. If we enter the name of the command you are looking for, you will directly access to the help for this command.
git status
: Shows your repo's status. You can see which files are inside and the changes not staged.
git add mypage.html
: stages mypage.html
git commit -m mymessage
: commits your changes on your local repo, with the commit message "mymessage". It creates a new node, a new version of your work.
git branch mybranch
: creates a new branch called "mybranch".
git checkout mybranch
: migrates to mybranch.
git checkout -b mybranch
: Creates a new branch called "mybranch" and makes it the active branch.
git branch -d mybranch
: Deletes mybranch.
git merge
: merges your branch into master.
git push
: Pushes online your modifs.
git pull uptsream master
: gets online's upstream version of master and stores it in your local repo.
git pull --rebase
: gets online's upstream version of master and stores it in your local repo and vanish yours
git remote add origin https://github.com/nomutilisateur/MonProjet.git
: relie mon repo local avec le repo online.
git stash
: stashes any changes from the working directory since last commit.
git stash apply
: restore stashed changes in the working directory.
###Merge and rebase
Difference between git merge
and git rebase
Suppose originally there were 3 commits, A,B,C:
Then developer Dan created commit D, and developer Ed created commit E:
Obviously, this conflict should be resolved somehow. For this, there are 2 ways: MERGE:
Both commits D and E are still here, but we create merge commit M that inherits changes from both D and E. However, this creates diamond shape, which many people find very confusing.
REBASE:
We create commit R, which actual file content is identical to that of merge commit M above. But, we get rid of commit E, like it never existed (denoted by dots - vanishing line). Because of this obliteration, E should be local to developer Ed and should have never been pushed to any other repository. Advantage of rebase is that diamond shape is avoided, and history stays nice straight line - most developers love that!
Revert changes
If the changes are already in the index, we can restore the index to the last commit using git reset:
git reset HEAD somefile.txt
restores a specific file from the index.
git reset HEAD
restore the whole index.
If you have already committed your changes, you can make use of git’s built-in "undo" command called git revert :
git revert 068b9b9
###Git Workflow with existing file
At first
Clone and install from the remote repo :
git clone https://github.com/user/your-repo
cd your-repo
npm install && bower install
gulp test
: will catch eventually sub dependency packages upgrade issues.
Create your new feature
git cob fix-something
: creates a new branch called "fix-something".
git cm "fix(area): All the issues"
: commits the fix with comment.
git push
: push the branch to the remote.
Integrate it to master First, initiate a pull request on github, and get approval for your work from your team.
git co master
: migrates to master.
git sync
: syncs remote master with local master.
git co fix-something
: migrates to the branch named fix-something.
git merge master
: merges your branch with master. You just synced last version of master to your local repo. You are now merging it, in your branch, in your local repo. You just worked over last version of master. Now you potentially need to deal with some conflicts.
git ready
: take all the commits and remake the history of changes. You can squash (make all the commits only one commit). A menu appears in your terminal and you should only use f and r in the first place. This step is in the end of your pull request, when everybody agrees on the potential conflicts.
When you have just resolved an issue, you should put on the commit comment : Fixes #21
(#21 standing for the issue's id).
git push -f
: overwrites github’s remote version of the branch with your local one. You just rewrote the story. Git doesn't know anymore how to merge my nodes with the online ones, but i force the process.
Than merge the pull request on github : deletes my branch.
git bdone
: comes back to my local repo. Does the same thing than the merge on github (previous step), but does it in my local repo.
If you want to ungit a repo, just delete the git file with :
rm -rf .git