|
|
test |
|
|
\ No newline at end of file |
|
|
# What is Git?
|
|
|
--------------
|
|
|
|
|
|
Git is a version control system used for synchronized and **distributed** collaborations. Being distributed is the major difference from SVN: everybody has a full copy of all files and no special info is stored on servers.
|
|
|
Another major feature are **commits**. A commit is a selection of files added / deleted / modified: when you apply changes to your files, you (usually) do not do it file by file but in batches (commits). This is a difference from cloud storage services (e.g., Dropbox): on the cloud every file has its own history, in Git commits define the history.
|
|
|
|
|
|
This is a very basic tutorial with a collection of the most important commands and a minimal example. Please see [this](https://git-scm.com/doc) and [this](https://www.atlassian.com/git/tutorials/) for more info.
|
|
|
|
|
|
And see [this guide](https://ias-group.slack.com/files/simone/F1NKF97A8/Git_Workflow.md) for a correct workflow.
|
|
|
|
|
|
|
|
|
# Workflow Example
|
|
|
------------------
|
|
|
|
|
|
### Locally
|
|
|
-----------
|
|
|
|
|
|
To **clone** locally a repository run
|
|
|
|
|
|
git clone git@git.ias.informatik.tu-darmstadt.de:sl/sl.git
|
|
|
|
|
|
You now have the *sl* folder. Move into it and run
|
|
|
|
|
|
git remote -v
|
|
|
|
|
|
You will see
|
|
|
|
|
|
origin git@git.ias.informatik.tu-darmstadt.de:sl/sl.git (fetch)
|
|
|
origin git@git.ias.informatik.tu-darmstadt.de:sl/sl.git (push)
|
|
|
|
|
|
`origin` is the (default) name of your remote host used for fetching and pushing.
|
|
|
|
|
|
By running
|
|
|
|
|
|
git fetch
|
|
|
|
|
|
you copy all commits and the history saved remotely (more on pushing later).
|
|
|
|
|
|
Let's say that now you want to modify some files but you also want to keep the current repository clean. You then create a new **branch** by
|
|
|
|
|
|
git checkout -b my_branch
|
|
|
|
|
|
This way, you create a new copy of all files (namely, a branch) named *my_branch* and immediately move into it. Any change that you apply here will not affect the files in other branches.
|
|
|
You can see all your local branches by
|
|
|
|
|
|
git branch
|
|
|
|
|
|
The optional argument `-a` shows you all branches, even the remote ones.
|
|
|
|
|
|
Now you want to do your stuff. Let's create a new file by running `touch asd.txt` and then run
|
|
|
|
|
|
git status
|
|
|
|
|
|
You will be told that there is an **untracked** file (the one you just created). To **stage** it (so that Git will keep track of it) run
|
|
|
|
|
|
git add asd.txt
|
|
|
|
|
|
>> DO NOT USE `-A`! It will add all untracked files and it is easy to add also files you do not want to track.
|
|
|
|
|
|
If you now run again `git status` you see that the file is staged and ready to be committed. Run
|
|
|
|
|
|
git commit asd.txt -m <comment>
|
|
|
|
|
|
to commit it. The argument `-m` allows you to directly comment the commit (which is mandatory). Otherwise the default text editor will open and you will be asked to add a comment.
|
|
|
You can add the argument `-a` to add all uncommitted files, but it is often better to group commits in such a way that similar files / changes belong to the same commit, as commits define the history of your repo.
|
|
|
|
|
|
If you did a commit by mistake, you can **revert** it by running
|
|
|
|
|
|
git reset --soft HEAD~1
|
|
|
|
|
|
Once you are done with your modifications and you want to apply all of them to the master branch, you run
|
|
|
|
|
|
git checkout master
|
|
|
git merge my_branch
|
|
|
|
|
|
that is, you first move into the master and then import all changes from *my_branch* (you can do the same the other way around).
|
|
|
It can happen that there are **conflicts** between branches (for instance, if you would have created the same file in the master but with different changes). In that case, you will be asked to resolve the conflicts by manually modifying the files (which will have both changes).
|
|
|
|
|
|
It can also happen that you try to checkout but you receive an error because you have uncommitted files. However, you do not want to commit them because they are still incomplete. In this case you run
|
|
|
|
|
|
git stash
|
|
|
|
|
|
to create a **local** node that you can retrieve later.
|
|
|
|
|
|
>> Do stashing only for short-term developing, as it can easily become messy.
|
|
|
|
|
|
By running
|
|
|
|
|
|
git stash pop
|
|
|
|
|
|
you revert the changes you stashed and merge with the new stuff. However, remember that everything is done **locally**! If you forgot to `stash pop` and you edit the same file, you will get a conflict and you will be asked to solve it. Also, you can do multiple stash and pop, you can give names to stashes and so on ...
|
|
|
|
|
|
### Remotely
|
|
|
------------
|
|
|
|
|
|
So far we have worked locally. Now we want to push everything remotely by running
|
|
|
|
|
|
git checkout my_branch
|
|
|
git push origin my_branch
|
|
|
|
|
|
To checkout a new remote branch run
|
|
|
|
|
|
git checkout origin/<name>
|
|
|
|
|
|
If some changes are done remotely, you can update your local branch by running
|
|
|
|
|
|
git pull
|
|
|
|
|
|
If you have some commits to be pushed, you will be asked to do a merge. If there are some conflicts, you will be asked to resolve them.
|
|
|
|
|
|
|
|
|
### Additional Commands
|
|
|
---------------------
|
|
|
|
|
|
git log #allows you to see a complete log of all activities on the repo
|
|
|
|
|
|
git diff <file> #allows you to see differences of uncommitted files
|
|
|
|
|
|
|
|
|
### Git Gui
|
|
|
-----------
|
|
|
|
|
|
sudo apt-get install git-gui
|
|
|
git gui
|
|
|
|
|
|
It is an interface that easily allows you to add unstaged / uncommitted files, to commit and to push them. It is nice because you can see the differences of each file between your local and remote copies. However, for some things (adding many files, merge requests, ...) it can be easier to use the terminal.
|
|
|
|
|
|
|
|
|
# FAQs
|
|
|
-------
|
|
|
|
|
|
1. How do I create a new branch?
|
|
|
|
|
|
git checkout -b <name>
|
|
|
|
|
|
2. How do I commit files?
|
|
|
|
|
|
git commit <files> -m <comment>
|
|
|
|
|
|
4. How do I undo a commit?
|
|
|
|
|
|
git reset --soft HEAD~1
|
|
|
|
|
|
5. How do I retrieve files I stashed?
|
|
|
|
|
|
git stash pop
|
|
|
|
|
|
6. How do I merge two branches?
|
|
|
|
|
|
git merge <branch> <into_branch> #if not specified, <into_branch> is the current branch
|
|
|
|
|
|
7. How do I push and pull?
|
|
|
|
|
|
git push origin <branch> #to push local commits to the remote server
|
|
|
git pull #to retrieve remote changes
|
|
|
|
|
|
8. How do I delete a branch?
|
|
|
|
|
|
git branch -d <branch> #to delete it locally
|
|
|
git branch -dr origin/<branch> #to delete it remotely
|
|
|
git push origin --delete <branch> #alternative to delete it remotely
|
|
|
|
|
|
9. How do I change the remote host?
|
|
|
|
|
|
git remote set-url origin <new_host>
|
|
|
|
|
|
10. How do I reset my branch to a specific commit?
|
|
|
|
|
|
git reset <id> #you get <id> from git log
|
|
|
|
|
|
or
|
|
|
|
|
|
git reset <id> --hard #additionally discards any change to tracked files since the commit before head |
|
|
\ No newline at end of file |