Short introduction to git

This note can be used as a cheat sheet. Below is a brief description of most often used git commands.

Basics:

  • Working directory
    • Working directory | working tree
      Catalog in which we make changes. It contains check out latest version in a current branch: current version, HEAD, and changes that we made
  • Index
    • Staging area
      Contains changes which will be committed to a new version in the local repository. Changes from working catalog can be added to index by git add.
  • Local repository
    • A local copy of a repository. All operations are being done on a local repository. If we want to the public about changes we should push it out to a remote repository using git push.
  • Version
    • File version saved in repository | Commit
      We can connect versions by git merge. A version may have more than one parent. Git checkout is used to change files in a working directory to files of a given version.
  • Branch
    • Linearly ordered set of vertices in graph version. Usually, there is a current branch whose pointer is moved together with the HEAD when executing git commit, git reset …
  • Remote Branch
    • Branch in the remote repository.
  • Tracking branch
    • Branch in local repository that tracks branch.
  • HEAD
    • Indicates the current version from the local repository.
  • ORIG_HEAD
    • The previous HEAD value, before any of the HEAD changing operations:
      • git commit
      • git merge
      • git pull
      • git checkout
      • git reset, etc.
  • Master
    • Main branch. After creating new repository this is the current branch.  The way we work with GITs is that for every major task we create a new branch, we work in that branch, and then, as we already have the functionality, we merged this branch into a master.

 

Index operations:

Changes to the working directory will not be self-referenced to the new version in the local repository when we do git commit. Only changes recorded in the index are commenced.
Below are few basic commands and probably all commands that we need for work with git:

  • git add .
    • Adds changes to all files in the working directory I subcategory. Files that were not previously included in the current version are added.
  • git status
    • Displays information about which changes to the working directory have been stored in the index and which are not.
  • git rm file
    • Deletes files from working directory and index
  • git mv file1 file2
    • Renames the file file1 to a file and saves the change in the index.
  • git reset file
    • Reverse operation to git add. Sets the file in the index to its current version, which in effect removes changes to the file added to the index by git add.

 

Version operations:

  • git commit -m “description”
    •  Creates a new version in the local repository. This version will differ from the current changes in the index. After executing this command, the newly created version becomes current, HEAD, and the current branch point to a new version and the index is identical to the current version (no changes).
  • git commit –amend -m “description”
    • This is the most common form of the commit. The option causes the indexes to be added to the index in all files in the working directory that existed in the current version before they were uploaded.
  • git log
    • Shows the version history to the current version, ie the vertices of the version graphs that are available from the current version, always going to the father. This command has a number of additional options that let you specify exactly which versions we want to watch.
  • git diff
    • Displays the differences between the index and the working directory.

Operations on the branch:

  • git branch
    • Display branches in the local repository.
  • git branch -d  name
    • Deletes branch Name, deletes the Name pointer itself, and not the version to which this branch points. This version may still be available from other branches.
  • git checkout name 
    • Changes the current branch to name if Name is the branch name. HEAD is also set to the version indicated by name.
  • git merge name1, name2, name3
    • Creates a new version by including the versions pointed to by branches name1, name2, and name3 to the current version. Usually, we want to merge only one branch, but you can and several at a time.

Remote repository

  • git remote
    • Displays the list of remote repositories.
  • git push
    • Writes to remote branches in the default remote repository (usually the origin) changes from a branch tracking them in a local repository.
  • git pull
    • Takes changes from the appropriate remote branch to the branch that tracks the remote branches. Trying to automatically merge changes.
  • git fetch
    • It works like a git pull with the difference that it does not automatically erase changes to local branches.

Reference:

  1. Pro Git
  2. git commit murder
  3. Learn Git in a Month of Lunches
  4. Git Essentials
  5. Version Control with Git: Powerful tools and techniques for collaborative software development
  6. Professional Git

Git workshop z devWarsztaty

“… git actually has a simple design, with stable and reasonably well-documented data structures. In fact, I’m a huge proponent of designing your code around the data, rather than the other way around, and I think it’s one of the reasons git has been fairly successful […] I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” Linus Torvalds

30 stycznia wybrałem się na meetup poświęcony systemowi kontroli wersji jakim jest git. Był to pierwszy raz kiedy udało mi się w ogóle zapisać na warsztat. Nie musze chyba dodawać, że zainteresowanie jest olbrzymie, po dosłownie kilku sekundach wszystkie miejsca zostały zarezerwowane. Prelegentami były osoby z Happyteam oraz SkillsTemple.

Bardzo podobały mi się warsztaty. Myślę, że stopien wzrastał od od “git zera” do “git użytkownika” o co mi chodziło wybierajac się na to spotkanie. Dowiedziałem się o istnieniu kilu ciekawych wizualizacji, gdzie można przetestować na “sucho” swoje polecenia bez ingerencji w file system. Kolejnym plusem jest to, że każdy mógł liczyć na pomoc podczas ćwiczeń praktycznych. Pojawiła się nawet “wisienka na torcie” w postaci skomplikowanego zadania, które kilku codziennym użytkownikom gita sprawiła problem.
Jestem bardzo zadowolony 🙂

Git jest potężnym narzędziem, niezależnym od systemu operacyjnego, ale na ten temat więcej znajdziecie tutaj. Rzuć również okiem na sekcję na samym dole tego posta, znajdziesz kilka ciekawych linków.


References:

  1. Git scm
  2. Visualizing git concepts 3D
  3. Thing like a git
  4. Visualizing branch topology in git 
  5. Atlassian git tutorial
  6. Git book
  7. Linus Torvalds about git
  8. 10 years of git
  9. Learn git branch