Git: doing common tasks (part 1)
Git is an essential tool if you are a developer a sysadmin or a devops engineer, it can help you keep track changes in files you are…
Git is an essential tool if you are a developer a sysadmin or a devops engineer, it can help you keep track changes in files you are working and collaborate with others, lets see some very common tasks.
Installing git
On Centos / Red-Hat$ sudo yum install git
On Ubuntu / Debian$ sudo apt-get install git
Git global configuration
Before start working with git we need to do some initial configuration, the email of the user and its name; those settings are global meaning that will be applied in all repositories unless explicitly provide different mail and name of a specific repository.$ git config --global user.mail "kpatronas@gmail.com"
$ git config --global user.name "Konstantinos Patronas"
Creating a new repository
To create a new repository all we need to do is to create a directory and run git init in this directory.$ mkdir scripts
$ cd ./scripts
~/scripts$ git init
Initialized empty Git repository in /home/kpatronas/scripts/.git/
What happens when we create a file in a repository
By default files created inside the git directory are not tracked, this means that are not part of our repository despite being inside the repository directory and any changes in this file will not be committed.$ echo "hello" >> a.py
$ git status
On branch masterNo commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
a.pynothing added to commit but untracked files present (use "git add" to track)
How to add a file to the repository (track a file for changes)
To add a file we use the git add command.$ git add a.py
$ git status
On branch masterNo commits yetChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a.py
How to commit a file
To commit a file we use the git commit command after we have git add the file$ git commit -m "First Commit"
[master (root-commit) c2042df] First Commit
1 file changed, 1 insertion(+)
create mode 100644 a.py$ git status
On branch master
nothing to commit, working tree clean
How to view git logs
Using git log we can examine the commit history$ git log
commit c2042df026e657ed5fbaf559d2ec833df2af5592 (HEAD -> master)
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:05:16 2022 +0300First Commit
We can see more detailed info using the -p parameter
If we want to see more details like what lines added / removed we can use the -p switch.$ git log -p
commit 2ab69c996f155a1c100387a4c8dbeef52d76b899 (HEAD -> master)
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:13:09 2022 +0300second commitdiff --git a/a.py b/a.py
index ce01362..83acaae 100644
--- a/a.py
+++ b/a.py
@@ -1 +1,3 @@
hello
+
+adding some textcommit c2042df026e657ed5fbaf559d2ec833df2af5592
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:05:16 2022 +0300First Commitdiff --git a/a.py b/a.py
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/a.py
@@ -0,0 +1 @@
+hello
How to view one line per commit logs
We can tweak git log to show one commit per line along with a nice date/time format.$ git log --pretty=format:"%h %ad%x09%an%x09%s"
556972b Sun Jun 26 11:25:45 2022 +0300 Konstantinos Patronas Renaming a.py to test.py
02bc83b Sun Jun 26 11:21:29 2022 +0300 Konstantinos Patronas Deleting file b.txt
3d4c73c Sun Jun 26 11:15:35 2022 +0300 Konstantinos Patronas third commit
2ab69c9 Sun Jun 26 11:13:09 2022 +0300 Konstantinos Patronas second commit
c2042df Sun Jun 26 11:05:16 2022 +0300 Konstantinos Patronas First Commit
How to view statistics for your commits
Using the --stat argument in the git log command we can see statistics for our commits.$ git log --stat
commit 3d4c73c1061e274afc6236efd40e566fa2a4e214 (HEAD -> master)
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:15:35 2022 +0300third commitb.txt | 1 +
1 file changed, 1 insertion(+)commit 2ab69c996f155a1c100387a4c8dbeef52d76b899
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:13:09 2022 +0300second commita.py | 2 ++
1 file changed, 2 insertions(+)commit c2042df026e657ed5fbaf559d2ec833df2af5592
Author: Konstantinos Patronas <kpatronas@gmail.com>
Date: Sun Jun 26 11:05:16 2022 +0300First Commita.py | 1 +
1 file changed, 1 insertion(+)
How to delete a file from a repository
To delete a file always use the git rm command, then you need to commit the changes.$ ls
a.py b.txt
$ git rm b.txt
rm 'b.txt'
$ ls -ltrh
total 4.0K
-rw-r--r-- 1 kpatronas kpatronas 24 Jun 26 11:12 a.py
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: b.txt
$ git commit -m "Deleting file b.txt"
[master 02bc83b] Deleting file b.txt
1 file changed, 1 deletion(-)
delete mode 100644 b.txt
How to rename a file in a repository
To rename a file always use git mv command, the you need to commit the changes.$ git mv ./a.py ./test.py
$ git diff --staged
diff --git a/a.py b/test.py
similarity index 100%
rename from a.py
rename to test.py
$ git commit -m "Renaming a.py to test.py"
[master 556972b] Renaming a.py to test.py
1 file changed, 0 insertions(+), 0 deletions(-)
rename a.py => test.py (100%)
How to configure git not to include some files
Assume that we have binary files or just files that we don't want to include in our repository, to exclude those files we can use the .gitignore file.echo "*.jpg" > .gitignore
git add ./.gitignore
git commit -m "Adding git ignore"
Now if we put some jpg images in our directory git will not inform us that those files are not tracked.
How to review changes of files and decide if you want to stage the changes for a commit or not.
Using the -p switch you can review each file change and decide if you want to add the to the next commit.$ git add -p ./.gitignore
diff --git a/.gitignore b/.gitignore
index 2d0c929..76ce7fc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-*.gif
+*.jpg
(1/1) Stage this hunk [y,n,q,a,d,e,?]?
How to undo changes in a modified file that has not been staged
Using git checkout we can undo changes for files that have not been yet staged.$ vim test.py
$ cat test.py
#!/usr/bin/env python3print("hello")$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.pyChanges not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.py$ git checkout test.py
Updated 1 path from the index
$ cat test.py
#!/usr/bin/env python3print("hello")
print("hello again")
How to undo changes in a modified file that has been staged
Using git restore staged we can un-stage a file for next commit, note that this will not revert changes to a file, to do this you need then to run git checkout like we did in the previous example.$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.py$ git restore --staged test.py
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.pyno changes added to commit (use "git add" and/or "git commit -a")
How to do minor changes in commits
More than often you will forgot things when you commit, you might forgot to add a file, or to remove, or just to mention something on your commit message, there is always the option to create a new commit to add what you forgot but this is an ugly practice and even confusing, to avoid an extra confusing commit you can use the --amend argument$ echo "test" >> c.txt
$ echo "test" >> a.txt
$ echo "test" >> b.txt
$ git add ./a.txt
$ git add ./b.txt
$ git commit -m "adding a.txt b.txt and c.txt"
[master b044c66] adding a.txt b.txt and c.txt
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 b.txt
Oops! we forgot to add c.txt to our commit! now instead of making a new commit we can amend the last commit and add c.txt$ git add ./c.txt
Now we have two options, we can re-commit without editing our commit message and the option to edit the commit message, since that the commit message was ok and mentioned the file we forgot we will not edit the commit message with the--no-edit argument.$ git commit --amend --no-edit
How to revert a commit
To revert a commit use git revert, note that git revert revert changes only for files present to the current commit and will not do any changes like removing files that did not exist at the time of the commit you want to revert.$ git revert <commit-id>
How to view current branches
By default there is only one branch, called master. Branches are different line of development that have been diverged since a specific commit and will be merged again to the master branch when the development is completed.$ git branch
* master
How to create a new branch
Using git branch with a branch name as argument we can create a new branch.$ git branch new-feature
$ git branch
* master
new-feature
How to switch to a branch
Using git checkout we can switch to another branch.$ git checkout new-feature
Switched to branch 'new-feature'
$ git branch
master
* new-feature
How to delete a branch
There is a catch! using -d parameter the branch cannot be deleted if not merged with the master branch.$ git branch -d even-better-feature
error: The branch 'even-better-feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D even-better-feature'.
But it can be delete by force if we use the -D parameter.
How to merge a branch with another branch
Checkout to the branch you want to be merged with another branch, then using git merge will merge the two branches.$ git checkout master
$ git merge even-better-feature
Updating 0f7b7be..691c957
Fast-forward
script2.py | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 script2.py
How to deal with merge conflicts
Very often a merge conflict will occur, this is because the branch we have checked out has been moved to a newer commit than the one that the branch we wish to merge had originated and git cannot understand what changes we want to keep or remove, in this case it will ask us to manually resolve the config.$ vim script1.py
$ git add .
$ git commit -m "small change to create a merge conflict"
[master 47ea75b] small change to create a merge conflict
1 file changed, 1 insertion(+)
$ git checkout new-feature
Switched to branch 'new-feature'
$ ls
auto.txt gather.txt output.txt script1.py
$ vim script1.py
$ git add .
$ git commit -m "trying to generate a conflict"
[new-feature 4136748] ytrying to generate a conflict
1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
Switched to branch 'master'
$ git merge new-feature
Auto-merging script1.py
CONFLICT (content): Merge conflict in script1.py
Automatic merge failed; fix conflicts and then commit the result.
This is the end of part 1 of this article! those tasks apply to local repositories, in the next part of the article i will show you commands used to interact with remote repositories in order to collaborate with other people! i hope you found this article useful and easy to read!