Origins and How to use it
Created by Linux Torvalds in 2005
For his design criterion, he specified that patching should take no more than three seconds, and added three more points:
Tools to manage version of codes
Lexical to know:
For one tree, each branch will have many commits.
Git Illustration
In git, we have mostly two branches :
You can create others branches if you need
When you work on git, you can add a remote server to push your codes. Many times is Github or Gitlab.
Your code hosted in your computer = Local
Your code hosted in Github/Gitlab Server = Origin
GIT
≠
Gitlab
Github
BitBucket
etc.
Git is a software.
Github, Gitlab, etc are Git Server providers.
Git don't required a server.
You can use git only for you without Github, etc.
You can select a branch by using this command
> git checkout my-branch
You can create a branch by using this command
> git checkout -b my-branch
You can delete a branch by using this command
> git checkout -d my-branch
> git fetch --all
You can add file to a commit by using this command
> git add --a (Add all files)
> git add myfile.txt (Add only one file)
Now you can add commit message :
> git commit -m "this is my commit message"
> git commit (Will open vim and you will add commit message)
Recommendation: Use Semantic Commit
If you local branch is different with your origin branch, you need to sync your local codes :
> git pull (Will fetch commits in origin and added it to your local)
If your local modifications are in conflict with remote commits, you will need to resolve conflicts.
Tutorial by Github : https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line
Now that you have created yout commit and you have sync commits with remote, you can push your commits :
> git push origin/my-remote-branch
To check all modifications before commit :
> git status
If you want to see every commit done in your current branch, you can use:
> git log
If you have created some commit in local and you want to remove previous commit :
> git reset --hard COMMITHASH
COMMITHASH : Hash given when you see git history (git log)
If you have done a revert in local branch, you need to force push to be sure that your local and your remote are sync :
> git push -f origin/my-branch
If you have created one commit and you want to revert it :
> git revert COMMITHASH
COMMITHASH : Hash given when you see git history (git log)
Information :
When you do a revert, the initial commit will stay in history and a new commit will be added to revert your changes.
If you have push some commits in master, you have push some other commits in develop, you want to sync develop with master.
> git checkout master
git pull
git checkout develop
git rebase master
If you have created multiple commit, and you want to merge them to one commit you can squash it :
1 - type git rebase -i COMMIT HASH OR git rebase -i HEAD~NB
(NB is equal of commits number, ex: 3 last commit -> HEAD~3)
2 - You see all your commits in VIM
3- Edit
Keep pick at the beginning of the first commit you want to keep
Replace pick by f (or type :%s/pick/f and type enter) at the beginning of the commits you want to squash
type Echap (to exit insert mode) then :wq to exit VIM
If you want to apply your commit later, you can create a patch file.
> git diff from-commit to-commit > output-file
later
> git apply output-file
Add git gone command :
git config --global alias.gone "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"
Type in your repo :
- git gone
Git flow is a tools for git to add some conventions in naming branch and others things.
More informations: https://github.com/nvie/gitflow