Git guides for rookies
Repository
Git repository is a data structure to store the change history of files and directories.
There are two types: local repository” and “remote repository”.
The “local repository” is the “.git” directory on your PC.
The “remote repository” is a repository on the Internet like GitHub.
$ cd [YOUR PROJECT PATH]
$ git initor$ git clone [Remote repository URL] [FOLDER NAME]
Usually, git repositories are created either by “init” or “clone”.
https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-init
https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone
$ git checkout .$ git clean -df .
As an aside, you can undo all changes in the local repository with the above command. Undo the changes with “git checkout” and “git clean” remove the file add. However, added or committed files can not be canceled with this command. The way to cancel them is described later in the add and commit paragraphs.
Remote Repository
Most of git’s commands are for local repositories. The few exceptions are push, pull (fetch). These commands affect to remote repositories. The “git remote” is a command to manage remote repository links.
https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches
https://www.atlassian.com/git/tutorials/syncing
$ git config --global user.name "Tsubasa Kondo"$ git config --global user.email tsubasakondo@example.com
You should have to do these things only once on any given computer.
$ git remote -v show
This command shows a list of remote repositories related with your local repository.
If you created a repository with “git clone”, the link to a remote repository named “origin” will be created.
If you created a repository with “git init”, the link to the remote repository will be empty.
$ git remote add origin [Remote repository URL]
This command relates your local repository with the remote repository.
In general, the “origin” is an alias for the remote repository.
$ git remote rm [ALIAS NAME]
This command deletes the remote links related with the local repository. This command only removes the link. The remote repository itself is not deleted.
$ git pull origin [REMOTE BRANCH NAME](e.g.) $ git checkout issue5_new_item_list$ git pull origin master
The “git pull” fetchs the remote branch changes into the local then merge them into the current branch.
In the above example, it merge the remote master changes into your issue5_new_item_list.
$ git push origin [REMOTE BRANCH NAME](e.g.) $ git push origin issue5_new_item_list
The “git push” updates the branch of remote repository.
In the above example, issue5_new_item_list brach of remote repository is updated. If there is no issue5_new_item_list branch in the remote repository, it will be newly created.
In our company, we use Pull Request, so we do not allow push to the master branch directly. Master branches are only updated by pull request merge, as described later.
Resolving Conflicts
If conflict occurs in “git pull”, you can easily resolve conflicts by the following way.
See this links for how to resolve conflicts in the text editor.
https://www.atlassian.com/git/tutorials/using-branches/merge-conflicts
$ git checkout --theirs [FILE PATH]or$ git checkout --ours [FILE PATH]
The “theirs” makes the specified file the same as the remote ones. The “ours” makes the specified file the same as the local ones.
$ git fetch origin master$ git reset --hard origin/master
This commands will force your branch to be replaced by origin/master. Be careful that your changes will be lost.
The “git fetch” command update your “remote tracking branch”. The “origin/master” is a remote tracking branch that holds the contents of the remote “master” branch into your local repository.
git add
The “git add” adds changes in your working directory to your staging area.
https://www.atlassian.com/git/tutorials/saving-changes
(A) $ git add .(B) $ git add [FILE NAME] ([FILE NAME2] [FILE NAME3]…)
The “.” (dot) stands for all files.
The “git add.” adds all changes to the staging area. The “git add.” is very useful but should be careful.
You can use the “git status” to check the files to be staged, the files that have been staged, and how to unstage.
(A) $git reset .(B) $git reset [FILE NAME] ([FILE NAME2] [FILE NAME3]…)
Undo git add.
git commit
The “git commit” captures a snapshot of the project’s currently staged changes. Git can manage history only for changes you have committed. If you don’t want to lose your changes just commit it anyway.
https://www.atlassian.com/git/tutorials/saving-changes/git-commit
$ git commit
By default, the commit message is written in a text editor. See following URL for how to write a commit message.
https://github.com/agis/git-style-guide
$ git commit -m “[COMMIT MESSAGE]”
The shortcut command that immediately creates a commit with a passed commit message.
By default, git commit will open up the locally configured text editor, and prompt for a commit message to be entered.
Passing the -m option will forgo the text editor prompt in-favor of an inline message.
I often use “-m”. But, you should include more information in the commit message. So you should not use “-m”.
(A) git reset --hard HEAD~1(B) git reset --soft HEAD~1
Both commands remove the last commit from the current branch.
The “hard” , you will lose all uncommited changes. The “soft”, your file changes will stay in your working tree.
Branch
The “master” branch should not have bugs. So you should not push to the master branch directly.
To change master branch, you should use merge instead of push.
In that case, you create a new branch then merge into the master branch.
If you develop using Pull Request, you do not have to merge in the local repository. In that case, merge using the “Pull Request” function of the remote repository.
https://www.atlassian.com/git/tutorials/using-branches
https://www.atlassian.com/git/tutorials/using-branches/git-merge
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
$ git branch [BRANCH NAME](e.g.)$ git branch issue5_new_item_list$ git branch feature/add_upload_profile_icon$ git branch hotfix/fix_endpoint_name_typo
This command creates a new branch.
In my case, I use the above branch names.
$git checkout [BRANCH NAME]
Switch the current branch.
$ git checkout -b [BRANCH NAME]
This command creates the new branch and immediately switch to it.
The “-b” option is a convenience flag that tells Git to run “git branch” before running “git checkout”.
$ git branch -D [BRANCH NAME]
This command removes a branch.
The “-D” option allows you to remove unmerged branches.
If you use Pull Requests, you do not merge in your local repository, so you use “-D” option.
$ git stash$ git branch [another_branch]$ git stash apply
If you accidentally work in another branch (eg master), you can use the above command to move changes to another branch.
Pull Request
Pull Request is a feature to merge your branch into other remote branches.
Pull Request prevents developers from pushing to the master branch.
You can discuss with your team or stakeholders, in pull requests.
Pull requests are not Git’s own features. Git’s hosting services, like GitHub and CodeCommit, have Pull Request feature.
https://docs.aws.amazon.com/codecommit/latest/userguide/pull-requests.html
https://help.github.com/en/articles/about-pull-requests
https://help.github.com/en/articles/merging-a-pull-request
https://www.atlassian.com/git/tutorials/making-a-pull-request
https://blog.ploeh.dk/2015/01/15/10-tips-for-better-pull-requests/
GitHub-Flow
I prefer “GitHub flow” which is one of git’s workflow.
https://help.github.com/en/articles/github-flow
https://guides.github.com/introduction/flow/
https://es.atlassian.com/git/tutorials/using-branches/git-merge
(1) $ git branch
Confirm the current branch is issue5_new_item_list.-- Note -- The "issue5_new_item_list" is just an example, replace it with your branch name.(2) $ git pull origin master(3) Edit your source file.(4) $ git commit(5) $ git push origin issue5_new_item_list(6) Create Pull Request on the remote repository. If your Pull Request is rejected or "Not mergeable", repeat #2,#4,#5 again.(7) If there is no problem with your Pull Request, the reviewer will merge your Pull Request into remote master branch.(8) $ git checkout master(9) $ git pull origin master(10) $ git branch -D issue5_new_item_list
In GitHub flow, you push your feature branch then send Pull Request, then review your changes, then merge it into the remote master branch.
If your Pull Request is “Not mergeable” (Can’t automatically merge), pull the latest changes from remote master to your feature branch. Then push that branch again.
If your Pull Request is “Mergeable” (Able to merge), you wait for the reviewers feedback. The reviewers reviews your Pull Request, if there are no problem, he/she merges your feature branch into the remote master branch.
Actually, you can merge on your own, but our company does not allow rookies to merge on their own.