logo telecom ipp

GIT: Level 2

Overview:

  • git branch: manage branches
    • local
    • distant
  • git stash: record local changes in a container to be able to switch the current version in the working copy
    • git stash pop, git stash clear
  • git revert: cancel the effect of a commit by creating the opposite commit
  • git remove: stop tracking a file in git and remove the file
  • git commit --amend: add changes to the previous (unpushed) commit
  • git checkout -- file: cancel local changes of a file
  • git fetch: get the new data from the distant repository without changing the working copy nor merging anything
  • git tag: associate a symbolic name to a state of the repository
  • git reset: opposite of git add, removes changes from the stage
  • useful aliases
  • fast-forward

GIT: Cheat sheet

Classical mistakes and their resolutions can be found here

GIT: Branches

  • You have already used branches: master.
  • A branch is nothing more than a name on a commit.
  • A branch can be local or distant.
  • To remove a branch does not remove the commits, but may leave some commits "dangling", i.e. with no easy way to point at them.
  • Use explicit branch names, but no spaces or accents please.
  • When you commit, the current branch moves forward to the new commit.
  • It is possible to remove a distant branch: the command line syntax is horrible, so use a GUI (smartGit, gitKraken, eclipse, IntelliJ...)

Scenario 2

bug report on v2 while you are writing v3:

  • commit your changes to v3
  • git checkout v2
  • change v2 files, fix the bug, git add, git commit
  • git checkout v3
  • go on with v3 development

Why branch ?

Because successive commits create a line, and when two people work on the same version, it creates a fork.

A branch is just a name on a commit.

master is the default branch.

branche

GIT: Revert

You just made a mistake and committed some change to the wrong branch.

You want to remove a commit. Everything is possible in GIT, including removing a commit, but it is not the right way to fix the situation.

With Git, you can create another commit that cancels a particular commit: git revert

git revert HEAD creates a commit that cancels the commit that creates the current version.

GIT: Remove

Removing a file is not the same as stopping to track the file in GIT.

git rm file removes the file AND stops tracking it (after the commit)

git rm --cached file stops tracking the file in GIT (after the commit) without removing the file from the working copy. The file will appear as "untracked" unless it is added to the .gitignore file.

GIT: Amend

You just created a commit, and realize you forgot some changes.

Before you push, do your changes, then git add them, then:

git commit --amend to add the new changes to the previous commit.

Of course, do not do this if you pushed the commit already. If you pushed, then create a new commit, even if very small.

GIT: Checkout --

You just deleted or changed Main.java by mistake.

git checkout -- Main.java (there are spaces before and after --)

Then your Main.java file is restored to its previous state.

GIT: Fetch

You want to see the changes from all the team, but you are not brave enough to attempt the big merge you are expecting. Then, do not do git pull, do git fetch which is equivalent to pull without merge.

GIT: Tag

Branches follow new commits.

You want the equivalent of a branch that stays on the commit it was set on. This is a tag.

For example, you want to tag a version "workingVersionMarch24".

Check out the version that you want to tag then

git tag workingVersionMarch24

GIT: Reset

You just did git add on all modifications, then you remember that Main.java is not quite ready. The rest has been urgently requested.

So you need to "unstage" just Main.java.

git reset Main.java does exactly this, i.e. the opposite of git add Main.java. Main.java is out of the stage and will not be in the next commit.

Utils

  • git shortlog -s -n --all : number of commits per author
  • git log --all --decorate --oneline --graph : presents a colored text version of the branches

Fast-forward

A fast-forward merge is a trivial merge.

A merge has two parameters: the current version and the version (branch or commit) to merge. If the version to merge is on another branch, then merging is not trivial. Git needs to find the common commit, detect conflicts, etc.

If you are on the master branch, you just did git fetch, and several commits appeared on master, then as git puts it, origin/master is "in front of" master. Then, git merge origin/master is a fast-forward.

The condition is that there is a single line of commits from start to end of the merge.

ff

Resources and credits

This resource is a reference, exhaustive and somewhat hard to read: git-scm.org

If stuck, look at this page for help on common git problems

Other courses in this series are:

This course was designed by Jean-Claude Dufourd, and shared as Creative Commons 3, BY-NC-SA (attribution, non commercial, share alike)