Minimal Git Knwo-How

This post is for people, who once in a while have to deal with git and therefore don’t repeat the commands frequently enough in order to keep them in longtime memory. 😛 I’ll provide a simple short reference containing the commands needed most frequently and enrich the reference when necessary.

Clone an existing Repository from Remote

If you have an existing remote repository of a project you want to work for but which is not available locally, then you need to switch to the directory, wehre you want the repository to be fetched into and use the clone-comamnd in order to get a local copy from the repository:

$ git clone https://github.com/your_account/your_repo.git
Cloning into 'fractals'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

After this command has been completed you’ll find a folder named ‘your_repo’ at the current location in your command line containing all files from the remote repository pointing to the master branch.

Add local Changes to a Repository

If you have made changes in files of a given project / repository, navigate to the project root folder. Now type the following to check the current status:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        doc/source/file_xyz.py

nothing added to commit but untracked files present (use "git add" to track)
$ 

Git now lists all untracked files or potentially previously added changes to the index. If you want to add all changes to the index, so you can push them to the online repo later, type the following:

$ git add -A .

Now all newly added or modified files have been added to the index. The index is a collection of files and changes to be collected for the next commit operation. Now you typically want to perform a commit operation and provide a commit-message, which identifies all changes you have done in this step. To commit all these changes to your local repository, type the following:

$ git commit -m "my important commit message"
[master ffdebed] my important commit message
 6 files changed, 329 insertions(+)
 create mode 100644 src/core/vrp
tw_core.py
$ 

Now you have bundled all changes applied on your code by this commit operation. All changes will now be reproducible later and you can checkout this very state of your repository at any point in time, if required. However, your changes haven’t yet been pushed and are only available on your local machine.

To upload your changes onto the current branch in your remote repository and make them available to other teammates, do the following:

$ git push
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 2.11 MiB | 983.00 KiB/s, done.
Total 18 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To https://github.com/company/repo_name.git
   38036ca..ffdebed  master -> master

Now your laptop can get lost, but your work is still safe. 🙂 I rather prefer to push once too much instead of risking to lose some of my work.

Revert all local changes

Sometimes just shit happens. If you want to reset local changes you have made to a file and not yet been added to the index, type the following:

$ git checkout -- <filename>

If you want to reset all your local changes and replace it with the version from the repository, type the following:

$ git fetch origin
$ git reset --hard origin/master

This would even delete new local files, which already had been added to the index.

How to create Tags

To handle tags you can use the git tag-command (see here for complete reference). To display the existing tags of your current repository switch to the required git repo and type the following:

$ git tag
v0.8
v1.0
v1.1
$ 

To add a new tag use the -a option and provide a message adding the -m option as shown in the following command:

$ git tag -a v1.1.1 -m "report test release for customer"

Now the tag only exists on your local machine. To push the newly created tag type:

$ git push origin v1.1.1
Counting objects: 1, done.
Writing objects: 100% (1/1), 179 bytes | 179.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/ims-fhs/customer.git
 * [new tag]         v1.1.1 -> v1.1.1
$ 

Alternatively, you could push all your tags which haven’t yet been pushed issueing the following command:

$ git push origin --tags
Writing objects: 100% (1/1), 179 bytes | 179.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/ims-fhs/customer.git
 * [new tag]         v1.1.1 -> v1.1.1
$