Name | Topic | Objective & Instructions | Solution | Comments |
---|---|---|---|---|
My first Commit | Commit | Exercise | Solution | |
Time to Branch | Branch | Exercise | Solution | |
Squashing Commits | Commit | Exercise | Solution |
git directory
, working directory
and staging area
git pull
and git fetch
?gitignore
is used forgitignore
files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached.
git status
does?Next, you can try to enable `feature.manyFile` with `git config feature.manyFiles true`. This does two things: 1. Sets `index.version = 4` which enables path-prefix compression in the index 2. Sets `core.untrackedCache=true` which by default is set to `keep`. The untracked cache is quite important concept. What it does is to record the mtime of all the files and directories in the working directory. This way, when time comes to iterate over all the files and directories, it can skip those whom mtime wasn't updated. Before enabling it, you might want to run `git update-index --test-untracked-cache` to test it out and make sure mtime operational on your system. Git also has the built-in `git-maintainence` command which optimizes Git repository so it's faster to run commands like `git add` or `git fatch` and also, the git repository takes less disk space. It's recommended to run this command periodically (e.g. each day). In addition, track only what is used/modified by developers - some repositories may include generated files that are required for the project to run properly (or support certain accessibility options), but not actually being modified by any way by the developers. In that case, tracking them is futile. In order to avoid populating those file in the working directory, one can use the `sparse checkout` feature of Git. Finally, with certain build systems, you can know which files are being used/relevant exactly based on the component of the project that the developer is focusing on. This, together with the `sparse checkout` can lead to a situation where only a small subset of the files are being populated in the working directory. Making commands like `git add`, `git status`, etc. really quick
```
git checkout main
git pull
git checkout devel
git merge main
```
git branch </code></summary>
Git runs update-ref to add the SHA-1 of the last commit of the branch you're on into the new branch you would like to create
</details>
When you run git branch </code> how does Git know the SHA-1 of the last commit?</summary>
Using the HEAD file: `.git/HEAD`
</details>
What unstaged
means in regards to Git?
A file that is in the working directory but is not in the HEAD nor in the staging area is referred to as "unstaged".
True or False? when you git checkout some_branch
, Git updates .git/HEAD to /refs/heads/some_branch
True
### Merge
You have two branches - main and devel. How do you merge devel into main?
```
git checkout main
git merge devel
git push origin main
```
How to resolve git merge conflicts?
First, you open the files which are in conflict and identify what are the conflicts.
Next, based on what is accepted in your company or team, you either discuss with your
colleagues on the conflicts or resolve them by yourself
After resolving the conflicts, you add the files with `git add `
Finally, you run `git rebase --continue`
</p>
</b></details>
What merge strategies are you familiar with?
Mentioning two or three should be enough and it's probably good to mention that 'recursive' is the default one.
recursive
resolve
ours
theirs
This page explains it the best: https://git-scm.com/docs/merge-strategies
Explain Git octopus merge
Probably good to mention that it's:
- It's good for cases of merging more than one branch (and also the default of such use cases)
- It's primarily meant for bundling topic branches together
This is a great article about Octopus merge: http://www.freblogg.com/2016/12/git-octopus-merge.html
What is the difference between git reset
and git revert
?
`git revert` creates a new commit which undoes the changes from last commit.
`git reset` depends on the usage, can modify the index or change the commit which the branch head
is currently pointing at.
### Rebase
You would like to move forth commit to the top. How would you achieve that?
Using the `git rebase` command
In what situations are you using git rebase
?
Suppose a team is working on a `feature` branch that is coming from the `main` branch of the repo. At a point, where the feature development is done, and finally we wish to merge the feature branch into the main branch without keeping the history of the commits made in the feature branch, a `git rebase` will be helpful.
How do you revert a specific file to previous commit?
```
git checkout HEAD~1 -- /path/of/the/file
```
How to squash last two commits?
What is the .git
directory? What can you find there?
The .git
folder contains all the information that is necessary for your project in version control and all the information about commits, remote repository address, etc. All of them are present in this folder. It also contains a log that stores your commit history so that you can roll back to history.
This info copied from [https://stackoverflow.com/questions/29217859/what-is-the-git-folder](https://stackoverflow.com/questions/29217859/what-is-the-git-folder)
What are some Git anti-patterns? Things that you shouldn't do
- Not waiting too long between commits
- Not removing the .git directory :)
How do you remove a remote branch?
You delete a remote branch with this syntax:
git push origin :[branch_name]
Are you familiar with gitattributes? When would you use it?
gitattributes allow you to define attributes per pathname or path pattern.
You can use it for example to control endlines in files. In Windows and Unix based systems, you have different characters for new lines (\r\n and \n accordingly). So using gitattributes we can align it for both Windows and Unix with `* text=auto` in .gitattributes for anyone working with git. This is way, if you use the Git project in Windows you'll get \r\n and if you are using Unix or Linux, you'll get \n.
How do you discard local file changes? (before commit)
`git checkout -- `
</b></details>
How do you discard local commits?
`git reset HEAD~1` for removing last commit
If you would like to also discard the changes you `git reset --hard``
True or False? To remove a file from git but not from the filesystem, one should use git rm
False. If you would like to keep a file on your filesystem, use `git reset `
</b></details>
## References
How to list the current git references in a given repository?
`find .git/refs/`
## Git Diff
What git diff does?
git diff can compare between two commits, two files, a tree and the staging area, etc.
Which one is faster? git diff-index HEAD
or git diff HEAD
`git diff-index` is faster but to be fair, it's because it does less. `git diff index` won't look at the content,
only metadata like timestamps.
By which other Git commands does git diff used?
The diff mechanism used by `git status` to perform a comparison and let the user know which files are being tracked
## Git Internal
Describe how `git status` works
Shortly, it runs `git diff` twice:
1. Compare between HEAD to staging area
2. Compare staging area to working directory
If git status
has to run diff on all the files in the HEAD commit to those in staging area/index and another one on staging area/index and working directory, how is it fairly fast?
One reason is about the structure of the index, commits, etc.
- Every file in a commit is stored in tree object
- The index is then a flattened structure of tree objects
- All files in the index have pre-computed hashes
- The diff operation then, is comparing the hashes
Another reason is caching
- Index caches information on working directory
- When Git has the information for certain file cached, there is no need to look at the working directory file