If you work in or are interested in the development world, you’ve probably heard of Git or GitHub. These tools can make life easier for developers, but knowing exactly what they do and the differences between them can be confusing.
This post explains how to use Git to manage source code in a repository and the functions of the different branches you may need. You can then use GitHub to publicly publish your repository so that other developers can contribute.
What are GitHub and Git?
GitHub is a cloud-based repository service for Git, the version control software designed by Linus Torvalds. This software allows multiple developers to create and modify code together by managing versioning, code evolutions, and different development branches.
How do we create a remote repository on GitHub?
To create a repository, first, you need to define a valid name and an access type (public or private). The repo will need to be initialized before it can be used. This will generate the ‘main’ branch, which is where the default code will be uploaded.
After that, use the Git commands to synchronise your code with the repository. You will need to download the client for Windows, Mac, and Linux environments by following this link.
How do we obtain the remote path of a repository to copy it to your local drive?
To start working, you need to clone a repository to your local drive. To do this, access the repo and select ‘code’. The remote path of the repo to be used for the cloning process will appear under the ‘clone’ tab.
** Git supports different protocols: https://, git:// o bien user@server:path/to/repo.git.
This information will be used later when we start cloning the repository to the local drive.
Now we can get started with git, the fun part 😉
First of all, we need to understand how git works. Basically, the git system consists of three parts:
- Working directory: this is the folder on our computer that contains the files (our code, for example).
- Index (or stage/cache): this is a staging area for registration of the files that have been modified and will be sent in the next commit.
- Repository: this is where the changes are stored for synchronization with a remote repository.
** HEAD: this refers to the most recent commit (the latest version of the code on the current branch which is added to the repository).
Upon running the git command with the ‘add’ option the changes are registered in the index.
Subsequently, select the ‘commit’ option to add these changes to the local repository.
Finally, if you want to upload the changes to your remote GitHub repository, simply select the ‘push’ option.
Another important aspect is the concept of branches.
Branches are used to develop the isolated functionalities which taken together characterize each of the different projects.
For example, the ‘develop’ branch is commonly used, although you can also create other branches to perform parallel developments, such as ‘feature’, ‘hotfix’ and so on.
Let’s get started! How do we use Git?
Run ‘git -version’ via the system to view the current version and also check that it is correctly installed. Use ‘git -help’ to access the Help section and its different commands.
To clone a repo to the local drive, position yourself on the path where you want to host the repos, for example, a working directory, and from there run the command:
~]$ git clone https://<remote_github_server>/<my_user>/<my_repo>.git
Or if you want to clone it to a different folder:
~]$ git clone https://<remote_github_server>/<my_user>/<my_repo>.git myfolder
Once cloned, you can access it to view its content and the branches created:
~]$ git branch
# o bien:
~]$ git branch -v
incluso con ‘-vv’
As any programmer knows, it is better to not make any changes to the ‘main’ branch, which is why we create a ‘develop’ branch.
~]$ git branch develop
We can then switch to that branch:
~]$ git checkout develop
** Run the ‘git checkout -b <branch_name>’ command to create the branch and switch to it directly.
To delete a branch you have created (for example ‘dev2’), run the following command:
~]$ git branch -d
Once in the desired branch, any changes you make (modification of code or files) will be reflected. The ‘git status’ command shows any changes made to the files, while ‘git diff’ shows the lines that have been modified:
~]$ git status
~]$ git diff
~]$ git diff develop origin/develop
# to see the differences before publishing the changes.
To transfer these changes to the index (modifications and the creation or deletion of files), run the following command:
~]$ git add [|]
~]$ git add.
You then need to commit those changes to the local repository:
~]~]$ git commit -m “<mensaje descriptivo>”
Check the status of the changes again and you will see that the output has changed:
~]$ git status
If you make a mistake with the message or wish to rectify any of the changes, use the ‘amend’ option as follows:
~]$ git commit –amend -m “<nuevo mensaje>”
** The ‘amend’ option only allows correction of the most recent commit. If you forget to add a file, you can also use it after running ‘git add’.
To upload the information of a new branch (which did not previously exist on the server) so that other developers can use it, run the following:
~]$ git push -u origin develop
For subsequent uploads of changes you can omit the ‘-u’ (short for –set-upstream-to):
Once this is done, any team member can download the changes again by entering from within their local copy of the repository:
~]$ git pull
# if there are no changes notification will be sent.
If the new person wishes to review the modifications that have been made to the repository, the following commands may be used:
~]$ git log
~]$ git log –oneline
# to display the information in short version
~]$ git log –graph
# to display a graph of the branches
To see the changes of a specific commit, run the following command:
~]$ git show <id_commit>
How do we revert changes in Git?
Any changes added to the index (git add) may be undone with the command:
~]$ git rm –cached <file_name>
Once this is done, you will see that the README file will once again have ‘untracked’ status’.
As you continue with the development, you can go back to modify files, run a ‘git add’ and a ‘git commit’ and use ‘git diff develop origin/develop’ to view the differences between the local ‘develop’ branch and the remote one:
If you have already committed the files to the local repository (git commit), you can go back to the previous version using the following command:
~]$ git checkout HEAD~1 <file>
~]$ git revert HEAD
If you have already published the changes to the remote repository, you can go back to a previous commit using the ‘reset’ option. This parameter supports two options: ‘soft’ (maintaining local changes) or ‘hard’ (without maintaining local changes).
~]$ git reset –hard HEAD~
The ‘reset’ option allows you to move the development branch to a specific commit by either indicating the commit ID or the HEAD ordinal. This will also modify the commit history:
** Git references each commit with a 40-digit SHA-1 hash, although for most informative messages it uses a more user-friendly abbreviated form (01d3d6d) consisting of 7 digits. The ‘git log’ or ‘git show’ command allows you to see the complete hash:
How do we merge the main part with the development part?
How can we merge the main branch and the ‘develop’ branch? Easy!
Position yourself in the target branch and request the merge from there in the following manner:
~]$ git checkout main
~]$ git merge develop
The changes in the ‘develop’ branch will be integrated in the ‘main’ branch.
How do I create a repo locally?
To do this, firstly create a folder for the local repo and initialise it:
~]$ git init
Then add a remote repository:
~]$ git add remote origin https://<remote_github_server>/<my_user>/<my_repo>
Review it using the command:
~]$ git remote -v
How to use Git Stash?
This option gives you a dedicated space (the ‘stash’) to save the changes you have made and go back to the last commit in the event of conflicts or if you wish to pause a line of development. You can restore the changes whenever you want from the stash. The commands would be:
~]$ git stash save “message”
# to save the changes to the stash.
~]$ git stash show
# to save the changes to the stash.
~]$ git stash list
# to show the stashes created.
There are two ways to restore the changes to the stash:
- Pop: restores the files and deletes the stash after a commit.
- Apply: restores the files but maintains the stash.
Once this is done, the parts of the code that have been modified are marked.
What if we create a new branch from a past commit?
Run the ‘git log’ command to identify a commit and use it to make a change to that branch.
After that, all you have to do is create a new branch and then you will have a new line of development 😉
Additional information to become an expert
As for repositories, there are two types of files you should be familiar with:
- README.md: This is a file to create documentation about your developments in markdown. We will discuss this further in future posts 😉
- .gitignore: This is a plain text file to indicate which files or directories should be excluded from your projects.
Simply indicate the pattern or the full names of the files to be ignored.
Un ejemplo sería este:
# ignore txt files
If you want to include a specific file that was previously excluded, use an exclamation mark:
Or, for example, you can use a pattern like this:
# Ignore debug0.log, debug1.log … debug9.log
# Ignore any LOG folder
If you have made a commit (before creating a .gitignore file) and add an unwanted file, you can remove it from the repository with the following command:
~]$ git rm –-cached <file_name>
Git is a world of its own, but with a little research you can overcome your fears and start using it. I hope this brief introduction gives you enough information to get started and I encourage you to explore all the different options and put them into practice.
Santander Global T&O is a global company of Santander Group with more than 3,000 employees and based in Madrid, we work to make Santander an open platform for financial services.
Check out the positions we have open here to join this great team and Be Tech! with Santander.