How to use Git at Work
Tags: git, tutorial
Published: 2022-04-25 04:17:27

## Agenda

1. General Concept 
2. Caveats
3. how to clone
4. how to branch
5. how to commit
6. how to update
7. how to maintain
8. Last Words
9. References

## 1. General Concept 

Basically, you'll get two branches at work: master and develop. 
- The **master** branch is designed for production. The maintainers of your team project handle this branch to publish. 
- The **develop** branch is designed for development. The developers of your team project stretch their own branches per feature and then merge the feature branches to the develop branch. The feature branches, of course, must be tested in advance of merging to the develop branch. 

## 2. Caveats

You need to know the difference between "origin develop", "origin/develop", and "develop". 
- "origin develop" is a remote branch on sites such as GitHub, gitlab, etc. Thus, we use like:
`git fetch origin develop `
`git push origin develop `
- On the other hand, "origin/develop" is a downloaded branch from a remote server such as gitHub, gitlab, etc. This means a downloaded branch could be out of date so that you must update the downloaded branch regularly. i.e. whenever you start coding in the morning or pushing your codes. Thus, we use like: 
`git merge origin/develop `
`git rebase origin/develop `
- "develop" is a local branch you can modify. Thus, we use like:
`git checkout develop `
`git diff develop your-new-feature `

## 3. How to clone

Before you start developing, you need to clone your team project so that your teammates can work with the same project maintained by the same git. 

`git clone https://github.com/<username>/<project.git> `

and then, go to the develop branch:
`git checkout develop `

## 4. How to branch

`git checkout -b your-new-feature `

## 5. How to commit

Let's add all changed files except for declared files in .gitignore. 

`git add . `
`git status `
`git commit -m "message here." `

## 6. How to update

You need to update your downloaded branch before pushing your changes. 
`git fetch `
`git rebase origin/develop `

And then, go to the develop branch, update your local develop branch ,and push your changes to the remote git repository. 
`git checkout develop `
`git merge origin/develop `
`git merge your-new-feature --no-ff `
`git push origin develop `

## 7. How to maintain

You can track git commits: 
`git log --all --oneline --graph `

Or, you can track git commits of a specific file:
`git log -p ./path/to/file `

You can figure out what changes to be committed:
`git status `

You can figure out differences by two options:
`git diff `
`git diff --staged `

## 8. Last words

This post could be a very unkind one since I skipped describing all details of git commands but focused on how to use git commands in a chronological order. However, by doing so, you can figure out the whole picture of how to use git at work. 

I will appreciate that all comments point out something wrong on my post :) Thanks.

## 9. References

- [In Git, what is the difference between origin/master vs origin master?](https://stackoverflow.com/a/18137512)
- [When do you use Git rebase instead of Git merge?](https://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-merge/804156#804156)
- [git flow](https://nvie.com/posts/a-successful-git-branching-model/)
- [github flow](https://docs.github.com/en/get-started/quickstart/github-flow)
- [How do I delete a git branch locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely)