Skip to content

Latest commit

 

History

History
378 lines (256 loc) · 8.84 KB

File metadata and controls

378 lines (256 loc) · 8.84 KB

Git

Git is a Version control system.

Initialize Git repository

git init

to initialize an empty Git repository. It creates an .git folder. Never modify this folder.

tree .git/

to see the content of the folder.

git status

to see if every file is updated.

git add earth.txt

to add a file into the staging area. The status of the file pass from untracked to staged.

Config Git

git config --global user.name "Matteo Nurisso"
git config --global user.email "mnurisso@sissa.it"
git config --global core.editor "vim"

to setup Vim as standard editor for commit messages.

Commit

A commit is a screen shot of your working tree at a specif time. Another way to see them is as an increment of the previous work, everyone pointing to the previous one.

git commit

Open a text editor to edit the commit message. You need to add a commit message in order to do a proper commit. With the commit the file is now in the repository .git. Git doesn't see any other file in the working tree, because the file is now considered as unmodified wrt the last repository version.

By modifying a file wrt the repository version the command

git status

should show it as modified.

git diff

shows the differences between repository version and local modified version. The file has to be added to the staging area by

git add filename.txt

To commit in a single line:

git commit -m "git comment"

Dismiss modification

git checkout -- filename

To dismiss modification that you don't want to add and commit.

Change commit author

To change the author info of the last commit.

git commit --amend --reset-author

Add to the last commit

git add forgotten_file.txt
git commit --amend

To add a forgotten file to the last commit. The commit message will be the same as the previous one.

Commit log list

git log

shows a list of every commit with the commit comment and a unique identifier.

git checkout 946bdd #some characters from the identifier are enough

to go back to the specific commit. Move the HEAD, a pointer to the commit in which you are.

To show were you are in the commits (in a goodloking way):

git log --oneline --graph --all --decorate

* 06d935a (master) add humans
* 946bdd8 (HEAD) add earth.txt

you can also deine an alias for this command.

To go back to the newest version (of a specific branch):

git checkout master #master is the branch name

Recover file

git checkout -- .

to recover files if you by mistake delete important files. If you want to recover only a single file:

git checkout -- filename.txt

Change filename

If a file is renamed:

git add oldname.txt newname.txt

In this way git is able to compare the content of the deleted and new file and to recognize that a filename is changed.

git mv oldname.txt newname.txt

Automatically add the change to the staging area.

Custom aliases

To define a new alias available for every git repository:

git config --global alias.graph "log --all --graph --decorate --oneline"

this will create a new command git graph that will show the commit log in a nice way.

Branch

Branch is a pointer to a commit. The branch always point to the last commit done on the specific branch.

To create a new branch:

git branch moon

To see the list of branches:

git branch

* master
  moon

After the creation of a branch I'm still not in the new branch, in order to do that I need to use the checkout command.

git branch newbranch startingbranch #e.g. jupiter master

The branch is created from the actual position or adding another branchname to the branch command.

A general rule for branches is: new feature = new branch.

Switch branch

git checkout branch_name

or

git switch branch_name

The switch command is more recent and it's more clear, while checkout is more general and can be used for other things. To switch to a new branch and create it at the same time:

git switch -c branch_name

Merge

git checkout master #final branch
git merge branch_name

Fast-forward if there is nothing else apart from pushing forward the master branch. If it's not fast-forward a commit message is necessary to explain why the merge is necessary.

git branch -d branch_name

Delete a branch, but it's only a pointer so it's not deleting the commitments. It is useful to delete branches that are not used anymore to avoid confusion.

Merge conflicts

If the same file is modified in two different branches, git is not able to understand which is the correct version. In this case it's necessary to manually modify the file and then add and commit the changes. A file with merge conflicts has the following structure:

<<<<<<< HEAD
master version
=======
branch version
>>>>>>> branch_name

To solve the conflict it's necessary to manually modify the file and then add and commit the changes.

If you know that you want to sistematically use the version of a specific branch you can use the command:

git merge -s recursive -Xours branch_name

to automatically solve the conflicts using the version of the current branch

git merge -s recursive -Xtheirs branch_name

to automatically solve the conflicts using the version of the branch_name branch.

If while solving the conflicts you realize that you want to abort the merge you can use the command:

git merge --abort

Delete merged branches

It can be useful to delete old branches that are now merged into the master/main branch.

To automatically delete these branches run the following commands:

git fetch
git branch # to see the initial list
git branch --merged main | grep -v "^\* main" | xargs -n 1 -r git branch -d
git branch # to see what remains

If you want instead to delete branches that are not in the github anymore run:

git fetch
git branch -vv | grep ': gone]' | grep -v '\*' | awk '{ print $1; }' | xargs -r git branch -d

Fetch

The git fetch command downloads objects to the local machine without overwriting existing local code in the current branch.

The git fetch command retrieves commits, files, branches, and tags from a remote repository. The general syntax for command is:

git fetch <options> <remote name> <branch name>

With a git fetch the working directory is unaffected. The content can be accessed with git checkout on the fetched branch or can be merged by going in the final branch in which the merge should happen and with the command:

git merge <remote name> <branch name>

Tag

A lightweight tag is simply a pointer to a specific commit. An annotated tag instead has a comment and can be used to create a pointer to a specific version of your code with the changelog as comment.

git tag -a tag_name -m 'tag comment (changelog)'

-a specify the request of an annotated tag, while -m specify the comment.

git show tag_name

shows the comment related to the specific tag.

Instead a lightweight tag can be created by typing:

git tag tag_name

A tag can be added also to commit already created using its checksum:

git tag -a tag_name 9fceb02 #checksum or part of it

Compare different versions

If you want to compare the same file for different commitments you can use the git diff command

git diff <id1> <id2> filename

where the <id> refers to the version identifiers that can be found through the git log command.

Inspect repository

Grep-like command to search for a string in the repository:

git grep "string" #search for a string in the repository
git grep -n "string" #search for a string in the repository and show the line number
git grep -c "string" #search for a string in the repository and show the number of occurrences
git grep -c -n "string" #search for a string in the repository and show the number of occurrences and the line number