2018年6月19日火曜日

gitの使い方

git help config
git config --list --show-origin

git config --global user.name "<Your Name>"
git config --global user.email "<Your Email>"

# makes sure that Git output is colored
git config --global color.ui auto

# displays the original state in a conflict
git config --global merge.conflictstyle diff3

git init
or git clone https://github.com/project-name your-project-name

git status

git log
git log --oneline
git log --stat
git log -p
#ignore white space
git log -w

git show fdf5493


we have some new files that we want Git to start tracking
for Git to track a file, it needs to be committed to the repository
for a file to be committed, it needs to be in the Staging Index
the git add command is used to move files from the Working Directory to the Staging Index

git add
move files from the Working Directory to the Staging Index
move modified files to the Staging Index

The goal is that each commit has a single focus. Each commit should record a single-unit change. Each commit should make a change to just one aspect of the project. Conversely, a commit shouldn't include unrelated changes

git diff
The git diff command can be used to see changes that have been made but haven't been committed, yet.

.gitignore
Globbing lets you use special characters to match patterns/characters. In the .gitignore file, you can use the following:

blank lines can be used for spacing
# - marks line as a comment
* - matches 0 or more characters
? - matches 1 character
[abc] - matches a, b, or c
** - matches nested directories - a/**/z matches
a/z
a/b/z
a/b/c/z

If you make a merge on the wrong branch, use this command to undo the merge:

git reset --hard HEAD^

When a merge happens, Git will:
1)look at the branches that it's going to merge
2)look back along the branch's history to find a single commit that both branches have in their commit history
3)combine the lines of code that were changed on the separate branches together
4)makes a commit to record the merge

When we merge, we're merging some other branch into the current (checked-out) branch. We're not merging two branches into a new branch. We're not merging the current branch into the other branch.

Git tracks lines in files. A merge conflict will happen when the exact same line(s) are changed in separate branches.

git commit --amend
git revert <SHA-of-commit-to-revert>

branchを作成
git branch <branchname>
branchを確認
git branch
branchを切り替え
git checkout <branchname>
branchを削除
git branch -d <branchname>

Merge
git merge <branchname>

fetchを実行すると、リモートリポジトリの最新の履歴の取得だけを行うことができます。取得したコミットは、名前の無いブランチとして取り込まれます。このブランチはFETCH_HEADという名前でチェックアウトすることができます。

ローカルリポジトリからリモートリポジトリにpushするときは、pushしたブランチがfast-forwardマージされるようにしておく必要があります。もし、競合が発生するような場合は、pushが拒否されます。