Skip to content

Instantly share code, notes, and snippets.

@marcelomgarcia
Last active January 18, 2023 10:45
Show Gist options
  • Save marcelomgarcia/9db704808ec0853b187f1be993aa0e7e to your computer and use it in GitHub Desktop.
Save marcelomgarcia/9db704808ec0853b187f1be993aa0e7e to your computer and use it in GitHub Desktop.
Using Git

Git Cookbook

Using Git for every work.

Branch

Creating a branch

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git checkout -b dockerfile_no_ssh_key
Switched to a new branch 'dockerfile_no_ssh_key'
mgarcia@wsl2:~/Documents/Work/orcid-integration$

To push the new branch to remove repository

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git push --set-upstream origin dockerfile_no_ssh_key
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote:
remote: To create a merge request for dockerfile_no_ssh_key, visit:
remote:   https://gitlab.(...)-integration/-/merge_requests/new?merge_request%5Bsource_branch%5D=dockerfile_no_ssh_key
remote:
To gitlab.(...):garcm0b/orcid-integration.git
 * [new branch]      dockerfile_no_ssh_key -> dockerfile_no_ssh_key
Branch 'dockerfile_no_ssh_key' set up to track remote branch 'dockerfile_no_ssh_key' from 'origin'.
mgarcia@wsl2:~/Documents/Work/orcid-integration$

Checking the status

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git status
On branch dockerfile
nothing to commit, working tree clean
mgarcia@wsl2:~/Documents/Work/orcid-integration$

Checking the difference between to branches

mgarcia@wsl2:~/Documents/Work/lib-ioi$ git diff out_of_tirc master
diff --git a/Dockerfile b/Dockerfile
index 9d35c5d..59f247e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -21,7 +21,7 @@ ENV ORCID_SRC orcid
 ENV MY_USER www-data
 ENV MY_GROUP www-data
 WORKDIR ${ORCID_HOME}
-ADD --chown=${MY_USER}:${MY_GROUP} ${ORCID_SRC} ${ORCID_HOME}
+COPY --chown=${MY_USER}:${MY_GROUP} ${ORCID_SRC}/* ${ORCID_HOME}
(...)

To merge a branch with master

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
mgarcia@wsl2:~/Documents/Work/orcid-integration$ git merge dockerfile
Updating 49496b6..21da2a0
Fast-forward
 .gitlab-ci.yml     |  1 -
 README.md          |  8 ++++++++
 docker-compose.yml | 21 +++++++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 docker-compose.yml
mgarcia@wsl2:~/Documents/Work/orcid-integration$

After the merge, push the changes to the remote repository

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git push
Total 0 (delta 0), reused 0 (delta 0)
To gitlab.kaust.edu.sa:garcm0b/orcid-integration.git
   632f40d..a10c5c4  master -> master
mgarcia@wsl2:~/Documents/Work/orcid-integration$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Finally, delete the test branch

mgarcia@wsl2:~/Documents/Work/orcid-integration$ git branch -d tirc-helpers
Deleted branch tirc-helpers (was a10c5c4).
mgarcia@wsl2:~/Documents/Work/orcid-integration$

Changing Origin

Change the protocol used by git

a-garcm0b@library:~/test/lib-ioi$ git remote -v
origin  https://gitlab+deploy-token-39:Biq8_w4GnFExN7n4H84s@gitlab.kaust.edu.sa/tirc-infrastructure/external-projects/lib-ioi.git (fetch)
origin  https://gitlab+deploy-token-39:Biq8_w4GnFExN7n4H84s@gitlab.kaust.edu.sa/tirc-infrastructure/external-projects/lib-ioi.git (push)
a-garcm0b@library:~/test/lib-ioi$

Now we want to change from https to ssh

a-garcm0b@library:~/test/lib-ioi$ git remote set-url origin git@gitlab.kaust.edu.sa:tirc-infrastructure/external-projects/lib-ioi.git
a-garcm0b@library:~/test/lib-ioi$

Checking the repository

a-garcm0b@library:~/test/lib-ioi$ git remote -v
origin  git@gitlab.kaust.edu.sa:tirc-infrastructure/external-projects/lib-ioi.git (fetch)
origin  git@gitlab.kaust.edu.sa:tirc-infrastructure/external-projects/lib-ioi.git (push)
a-garcm0b@library:~/test/lib-ioi$

Tags

How to work with Git tags.

To list the current tags

PS C:\Users\garcm0b\Work\hello_ansible> git tag -l
01_playbook
10_adhoc
PS C:\Users\garcm0b\Work\hello_ansible>

Creating a new tag

PS C:\Users\garcm0b\Work\hello_ansible> git tag -a 01_playbook -m "Starting with Playbook"

Renaming a Tag

Renaming a tag

PS C:\Users\garcm0b\Work\hello_ansible> git tag -l
01_playbook
adhoc
PS C:\Users\garcm0b\Work\hello_ansible> 
PS C:\Users\garcm0b\Work\hello_ansible> git tag 10_adhoc adhoc
PS C:\Users\garcm0b\Work\hello_ansible> 
PS C:\Users\garcm0b\Work\hello_ansible> git tag -l 
01_playbook
10_adhoc
adhoc
PS C:\Users\garcm0b\Work\hello_ansible>
PS C:\Users\garcm0b\Work\hello_ansible> git tag -d adhoc
Deleted tag 'adhoc' (was 3c207a0)
PS C:\Users\garcm0b\Work\hello_ansible>

Puhing the New Tag

git push origin new old
PS C:\Users\garcm0b\Work\hello_ansible> git push origin 10_adhoc :adhoc
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/kaust-library-systems/hello_ansible.git
 - [deleted]         adhoc
 * [new tag]         10_adhoc -> 10_adhoc
PS C:\Users\garcm0b\Work\hello_ansible>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment