## 1. install git
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
sudo apt-get update
sudo apt-get install git
- set up global user information:
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
- set up specific user for each project:
git config user.name "Your Name"
git config user.email "youremail@domain.com"
## 2. Config file mode
File config in a project: `.git/config`
Don't check file mode in global: `git config --global core.filemode false`
Don't check file mode in specific project:`cd project_name && git config core.filemode false`
Check after change: `git config core.filemode`
## 3. Command to specify your preferences
git config --global core.editor your_editor_choice
EX: `git config --global core.editor nano`
## 4. Where is gitconfig
The global Git configuration file is stored
at $HOME/.gitconfig on all platforms.
git config --global --edit
## 5. Increase performance of git
git prune
--------Enable the filesystem cache
`git config core.fscache true` or
git gc(https://git-scm.com/docs/git-gc)
git clean
git config --global core.preloadindex true
git config --global core.fscache true
git config --global gc.auto 256
## 6. Clone new project
git clone https://github.com/libgit2/libgit2
git clone git@git_server_IP_address:johnsproject
## 7. create new branch
Make format branch name clearly: feature/<date>_<feature-name>
git checkout develop
git checkout -b feature/20170605_check_connection
git push origin feature/20170605_check_connection
## 8. GET ONE COMMIT FROM ANOTHER BRANCH
git cherry-pick 62ecb3
https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/
## 9. Show list branches
git branch
git branch -a | grep -v 'remotes'
git branch onflict
Delete local branch: git branch -D <branch_name>
Delete remote branch: git branch -dr <branch_name>
## 10. Show current git info
- Show file changes: `git status`
- git show
- you can show commit_id by this command: `git log`
- git log -2
## 11. Fetch data
git fetch
git fetch --all
## 12. Pull data
git pull
$git pull origin master
***Pull data with develop to refesh commit already appear in branch develop***
$git pull --rebase origin develop
$git push -f origin feature/201807_output_announce
## 13. Get list remote branches
git remote -v
## 14. Reset or Revert to HEAD version
git reset HEAD --hard
git reset --hard HEAD~n
## 15. Checkout new branch or switch local branch
- switch local branch: git checkout <branch_name>
- create new branch: git checkout -b qa/201705_new_branch origin/qa/201705_original_branch
## 16. Git commit with message
$git commit -a -m "<message>"
$git commit --allow-empty -m "<message>"
- commit with ignore check validation before commit:
git commit -m "update code" --no-verify
## 17. Push data to server
- git push origin master
- Force all thing in local branch to remote branch:
`git push -f origin feature/20200202_force_all_data`
## 18. Show diff status between remote and local branch
$ git diff
------------git: diff between file in local repo and origin
$ git fetch origin master
$ git diff origin/master -- [local-path]
$ git diff master:<path-or-file-name>
------------Show overwrite file, change files with git diff
git diff origin/develop --name-only --diff-filter=acdrtuxb -- ./frontend/touch/common
## 19. Add new file
$ git add keydir/john.pub
$ git add Documentation/\*.txt
$ git add git-*.sh
Add all file: $git add .
## 20. Undo git add
git reset <file>
git reset
git reset HEAD --hard
git rm <path>
git rm --cached <path>
git reset HEAD <path>
Examples:
git add file1
git stage file2
git reset HEAD -- file2
git reset HEAD -- file1
git reset HEAD myfile.txt
git reset HEAD *.bmp
------will "un-add" everything you've added from your current directory recursively
git rm --cached . -r
git reset *
## 21. Submodule
git submodule sync
git submodule update --init --recursive
-----setup submodule
git submodule foreach git reset --hard HEAD
git submodule foreach git status
git submodule update --init --recursive
git submodule foreach git reset --hard HEAD
## 22. Merge branch
git checkout -b qa/201705_test origin/qa/201705_test
git merge origin/feature/201705_feature_1
git push origin qa/201705_test
## 23. Revert n commit
you can show commit_id by this command: `git log`
-
git reset --hard HEAD~n
OR
git revert (commit_id)
- git push -f origin feature/<branch_name>
## 24. Delete a commit
$git rebase -i
https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git
https://www.clock.co.uk/insight/deleting-a-git-commit
## 25. Merge commit bằng rebase - Rebasing
Step 1: merge n commit
$ git rebase -i HEAD~n
==> when editor open, keep first commit as current,
for another commit change text "pick" to "f"
and then save.
Step 2: push commit after merge( with force -f)
git push -f origin feature/<...>
If you want to UNDO a git rebase
⇒ git rebase --abort
https://git-scm.com/docs/git-rebase/1.8.2.2
## 26. Fix Conflict File In Git
Replace file with file get from remote branch:
`git checkout --theirs Path/file.dll`
> $ git add Path/file.dll
> $ git commit -m "Resolved merge conflict"
## 27. Rename a branch
- a) Rename your local branch.
If you are on the branch you want to rename: `git branch -m new-name`
If you are on a different branch: `git branch -m old-name new-name`
- b) Delete the old-name remote branch and push the new-name local branch.
Note: keep “:”, don’t remove it.
git push origin :old-name new-name
- c) Reset the upstream branch for the new-name local branch.
Switch to the branch and then: `git push origin -u new-name`
## 28. Revert a file
git checkout HEAD filename
Or
git checkout [commit-ref] -- [filename]
git checkout [commit-ref] [filename]
git checkout HEAD~5 -- foo.bar or git checkout 048ee28 -- foo.bar
$ git add filename
$ git commit -m 'restoring filename from first commit.'
Example:
git checkout 3db97069620dce547f0a30d5d66196ccc347b6b8 frontend/v3.1/abc.zip
git add frontend/v3.1/abc.zip
git commit -m 'revert abc v3.1'
git push origin feature/201803_revert
## 29. Revert a deleted file
If the deletion has not been committed,
the command below will restore the deleted file in the working tree.
$ git checkout -- <file>
You can get a list of all the deleted files
in the working tree using the command below.
$ git ls-files --deleted
If the deletion has been committed,
find the commit where it happened, then recover the file from this commit.
$ git rev-list -n 1 HEAD -- <file>
$ git checkout <commit>^ -- <file>
In case you are looking for the path of the file to recover,
the following command will display a summary of all deleted files.
$ git log --diff-filter=D --summary
## 30. How to change your commit messages in Git
https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4
Not pushed + most recent commit: `git commit --amend`
This will open your $EDITOR and let you change the message.
Continue with your usual git push origin master.
Already pushed + most recent commit: `git commit --amend`
`git push origin master --force`
We edit the message like just above.
But need to --force the push to update the remote history.
⚠️ But! Force pushing your commit after changing
it will very likely prevent others to sync with the repo,
if they already pulled a copy. You should first check with them.
Not pushed + old commit: `git rebase -i HEAD~X`
# X is the number of commits to go back
# Move to the line of your commit, change pick into edit,
# then change your commit message: git commit --amend
# Finish the rebase with: git rebase --continue
Rebase opened your history and let you pick what to change.
With edit you tell you want to change the message.
Git moves you to a new branch to let you --amend the message.
git rebase --continue puts you back in your previous branch
with the message changed.
Already pushed + old commit:
Edit your message with the same 3 steps process as above
(rebase -i, commit --amend, rebase --continue).
Then force push the commit: `git push origin master --force`
⚠️ But! Remember re-pushing your commit after changing
it will very likely prevent others to sync with the repo,
if they already pulled a copy. You should first check with them.
## 31. Delete a file
Use git rm: `git rm file1.txt git commit -m "remove file1.txt"`
But if you want to remove the file only from the Git repository
and not remove it from the filesystem, use:
git rm --cached file1.txt
git commit -m "remove file1.txt
And to push changes to remote repo
git push origin branch_name
## 32. How to remove local (untracked) files from the current Git working tree?
As per the Git Documentation git clean
Step 1 is to show what will be deleted by using the -n option: `git clean -n`
Clean Step - beware: this will delete files: `git clean -f`
To remove directories, run `git clean -f -d` or `git clean -fd`
To remove ignored files, run `git clean -f -X` or `git clean -fX`
To remove ignored and non-ignored files, run `git clean -f -x` or `git clean -fx`
Note the case difference on the X for the two latter commands.
If clean.requireForce is set to "true" (the default) in your configuration,
one needs to specify -fotherwise nothing will actually happen.
Again see the git-clean docs for more information.
If needed to remove untracked files from particular subdirectory, git clean -f {dir_path}
And combined way to delete untracked dir/files and ignored files.
git clean -fxd {dir_path}
after this you will have modified files only in git status.
Example: $git clean -fd frontend/
## 33. Fix error “fatal: missing object in git”
rm -rf objects/c65d9adb7d5151df89aac3d52a2e018066dafc65
find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full
rm .git/index
git reset HEAD --hard
rm .git/HEAD
echo 'ref: refs/heads/master' > .git/HEAD
git status
git reset HEAD --hard
Git pull origin master
fatal: missing object c65d9adb7d5151df89aac3d52a2e018066dafc65 for refs/heads/feature/201804_sheep
$ git reset HEAD c65d9adb7d5151df89aac3d52a2e018066dafc65
$ git fsck
## 34. Git – Revert File to Previous Commit
Git – Revert Changes to File
Revert (reset) changes to a file if they haven’t been committed yet:
`$ git checkout -- <file>`
Revert (reset) a single file to a specific revision:
`$ git checkout <commit_hash> -- <file>`
## 35. How to view a file at a specific commit in git
$ git show REVISION:/path/to/file
You can also save a copy by
$ git show REVISION:/path/to/file >file.copy
Ex: git show HEAD~4:src/main.c
To show only commits of an individual file, run this command:
`$ git log -- <file>`
Run the below command to show commits of the particular file with diffs for each change:
`$ git log -p -- <file>`
Show the entire history of a file (including history beyond renames):
`$ git log --follow -p -- <file>`
https://www.thegeekstuff.com/2014/04/git-log/
$ git log --pretty=format:"Commit Hash: %H, Author: %aN, Date: %aD"
## 36. overwrite a branch in Git with master
git checkout dev_branch
git reset --hard master
git push -f origin dev_branch
## 37. Change default branch of a repository
https://stackoverflow.com/questions/51274430/change-from-master-to-a-new-default-branch-git
## 38. Git stash
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit. // use -u when you want to save new file
git stash save "save 2" -u
--------Get list file changes have save
git stash list -p
// See detail of a specific changes:
git stash show stash@{index}
// Apply a stash:
git stash apply stash@{index}
OR
git stash pop index
--------Remove stash
Apply a stash and then delete it:
git stash apply stash@{index}
git stash drop stash@{index}
OR
git stash pop stash@{index}
Delete all stash:
git stash clear
Không có nhận xét nào:
Đăng nhận xét