zaterdag 20 februari 2021

Common Git scripts

Introduction

This blogpost is a collection of common scripts I use very often when I am working with Git. It just are some scripts for later use as reminder for me and off course you can use these too. Now, a nice overview of the different statuses is given by Jianping Zeng. Actually there five statuses. He states about five statuses :  modified, staged, committed, origin and pushed.


Let's see if we can make some examples that handles a couple of these statuses. 


This blogpost assumes that you have created a git repo in Azure Devops.


Starting from scratch 

To create a new repo, you can use the git init command in a folder. It is a command that you can execute once. a hidden git folder structure (.git) is into place and a main branch is created.


git init


Resulting in a message : Initialized empty Git repository in <folder>


Starting with a clone of a repository

The first step with working with repositories is setting your name and email, next is initializeing the directory, the next step is to clone your newly created repository from Azure DevOps to your local computer. From your repository page on Azure DevOps, click on the "Clone" button. Click the button "Clone" or download, and in the “Clone with HTTPs” section, copy the URL for your repository. Next open command shell in windows and execute.


git clone <repo>


This command will result in :

Cloning into '<repo>'...

warning: You appear to have cloned an empty repository.


Add a file to local repository

I've create a small text file in the directory where I've cloned the repository. With git status I'm able to retrieve the status of the local repository. It states that I've included a file but it is not part of the tracking because the file is in the so called working directory of git. 


> git status

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file1.txt

nothing added to commit but untracked files present (use "git add" to track)


Next step is to add the file to staging area of git. Adding the files to the staging area is executed with the git add command.


I use the following command quite a lot. There are different variations possible of this command.


> git add .


No result message is returned. With git status it is possible to see more about the status of the file.


> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file1.txt


The file is now present in the staging area.


Committing changes to the local repository

The next step is adding the code into the local repository. This can be done with the commit command with git commit -m "message". This command will transfer the file from the staging area into the local repository.



This is the command :


> git commit -m "this is a message"
[master (root-commit) 5d395d4] new file
 1 file changed, 2 insertions(+)
 create mode 100644 file1.txt


Let's see what the command git status returns.


> git status

On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean


Pushing the files to the remote repository

The next step to get the code in the remote repository is to push the code with the git push command. The command pick ups the files from the local repository and puts them in the remote repository.





Execute the following command.


> git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 227 bytes | 227.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Analyzing objects... (3/3) (5 ms)
remote: Storing packfile... done (484 ms)
remote: Storing index... done (109 ms)
To https://xxxxxx.visualstudio.com/GitFundamentals/_git/GitFundamentals
 * [new branch]      master -> master


Getting more information with git log

With git log it is possible to get more information about the changes that have been executed. In the dump below.

> git log
commit 5d395d43b890a38e8507db1d03caf33f2865833a (HEAD -> master, origin/master)
Author: Hennie de Nooijer <xxxx@xxxx.com>
Date:   Sun Feb 7 20:53:18 2021 +0100

    new file


Pulling changes from a remote repository (soft)

The next step is getting the changes from the remote repository with the git pull command. I've changed something on the remote repository. I've added an extra line to the file : Second line.




After a changein the file in Azure DevOps, it's possible to execute the git pull command. This will pull the code from the remote repository to my local repository.



And this is the result when the git pull command is executed:

>git pull
remote: Azure Repos
remote: Found 3 objects to send. (24 ms)
Unpacking objects: 100% (3/3), 249 bytes | 22.00 KiB/s, done.
From https://xxxxxx.visualstudio.com/GitFundamentals/_git/GitFundamentals
   5d395d4..22f8f5e  master     -> origin/master
Updating 5d395d4..22f8f5e
Fast-forward
 file1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


Fetching and merging from a remote repository

The problem with a pull is that the changes are merged into your local code and perhaps you want to look into the code that others have created. That's what you can do with git fetch. The code is fetched into the origin repository, you can inspect the code and then decide to merge the code from the remote branch into your local branch.




For this experiment I've changed the file in the git repo in Azure Devops once again. I've added a third line to the file. 




Now let's see what happens when git fetch is executed?

>git fetch
remote: Azure Repos
remote: Found 3 objects to send. (27 ms)
Unpacking objects: 100% (3/3), 257 bytes | 16.00 KiB/s, done.
From https://bifuture.visualstudio.com/GitFundamentals/_git/GitFundamentals
   22f8f5e..0f49d2d  master     -> origin/master


Now the idea is that you want to check out the changes that your colleague has done. So if you execute a merge command it will merge the code into your local branch. With git checkout you can checkout the fetched remote branch.


>git checkout origin/master
Note: switching to 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 0f49d2d Updated file1.txt

The commit is the same hash. I can now inspect the file :


So, now I can decide to merge this file into my local branch. I think I'm going to accept this. First switch back to my local main branch and issue a merge.

>git checkout master
Previous HEAD position was 0f49d2d Updated file1.txt
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Now merge the remote branch with the local branch. 

>git merge
Updating 22f8f5e..0f49d2d
Fast-forward
 file1.txt | 1 +
 1 file changed, 1 insertion(+)

And now I've the data in my local main branch.


The result of git status :

>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean


The result of git log :

>git log
commit 0f49d2d98fb3ba98af4590bb6e9e8315ffb6d699 (HEAD -> master, origin/master)
Author: hennie de nooijer <hdenooijer@hotmail.com>
Date:   Wed Feb 10 18:57:45 2021 +0000

    Updated file1.txt

commit 22f8f5e01ab41ffd57765f050699e6361b2f1350
Author: hennie de nooijer <hdenooijer@hotmail.com>
Date:   Sun Feb 7 20:17:11 2021 +0000

    Updated file1.txt

commit 5d395d43b890a38e8507db1d03caf33f2865833a
Author: Hennie de Nooijer <hdenooijer@gmail.com>
Date:   Sun Feb 7 20:53:18 2021 +0100

    new file

List of common git commands:

I've included a couple of the git commands.


Initializing a folder (.git directories)                             git init
Set email and username                                                     git config
Log of a branch's commits                                             git log
Current branch and files in working area or staging.     git status
Bring in changes but still merge with local branch     git fetch
A remote repository to clone locally                             git clone <url repo>
Bringing in changes locally and merging immediately     git pull
Sending changes to remote repository                             git push
Adding changes to working area                                     git add .
Adding changes to the staging area                             git commit -m “description”
Restore a modified file in the working area                     git restore <file>
Restore file to working area                                             git restore <naam bestand>
Differences between working directory and commit     git diff

Final thoughts

This blogpost is an introduction into Git commands. I've explained the details and provided a list of common git commands. Enjoy.

Hennie