Git

Description

Git is a version control system

Setup

Install GIT: https://git-scm.com/downloads

Initialize

Create a project folder and add a file „README.md“ there. This is a markdown file to globally describe your project. Change to this directory and open the command prompt there, then enter

git init
git add .
git commit -m „First commit“

Now you can (locally) keep track of changes by „freezing“ the current state of your code with a commit. There are several UI tools to visualize your „history“ [1]

Best practices [2]

  • Commit Related Changes
  • Commit Often
  • Don’t Commit Half-Done Work
  • Write Good Commit Messages
  • Use Branches
  • Agree on a Workflow

Workflow (stub, because 1 person, locally atm) [3]

  • Let [branchname] be a placeholder for say: feature/frontend
  • Every feature starts with a branch: git branch [branchname]
  • Work on this branch by changing to it: git checkout [branchname]
  • List all existing branches via git branch -a
  • Commit changes: git commit -a -m ‚[commit message]‘
  • See status: git status
  • Discard uncommitted changes: git reset –hard
  • Remove „untracked“ changes: git clean -df
  • Have a look on git stash as kind of a clipboard [4]
  • Show commits: git log –oneline (recognize the commit-ids)
  • And in case you did something wrong, revert a commit: git revert [id]

Finally after completing a feature respectively branch, you may want to „merge“ those changes into the „main“ codebase and probably delete your local branch:
git checkout master
git merge [branchname]
git branch -d [branchname]

When working remotely, you’d have to transfer your branch via pull and push. Ideally the merge is done automatically, in reality you often run into merge conflicts, to be fixed manually. The easy way is using an IDE for this repetitive tasks. The clever way is to know at least some command line commands, because Git has a lot of capabilities, not necessarily covered by those IDEs.

References

[1] https://en.wikipedia.org/wiki/Comparison_of_Git_GUIs
[2] https://www.git-tower.com/learn/git/ebook/en/command-line/appendix/best-practices
[3] https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
[4] https://git-scm.com/docs/git-stash


Beitrag veröffentlicht

in

von

Schlagwörter: