GIT in 10 minutes04 Dec 2021 / 9 minutes to read Elena Daehnhardt |
Introduction
Version control systems are handy to keep track of file versions. This is useful for tracking your code, scripts and text information. Currently, GIT is one of the best open-source and cross-platform version control solutions. It enables distributed repository management; it works fast over HTTP and ssh protocols. GIT is relatively easy to use, with command-line utility or Graphical User Interface.
Version Control with GIT
Version control systems are handy to keep track of file versions. This is useful for tracking your code, scripts and text information. Currently, GIT is one of the best open-source and cross-platform version control solutions. It enables distributed repository management; it works fast over HTTP and ssh protocols. GIT is relatively easy to use, with command-line utility or Graphical User Interface.
Personally, I have found several commands to be essential for tracking my thesis text sources (latex) and versions of code. In this brief tutorial I will share these commands with you. For simplicity reasons, I will give a starting point of setup, initialising a GIT repository, basic usage scenarios in just 10 minutes of your time.
Installation
Since I work on macOS, I use Homebrew package manager to install Git. To install Homebrew, we can get it from https://brew.sh or with:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Next, Git installation with Homebrew, and Git’s location and version:
# Installing git with brew
brew install git
# Checking its path
which git
# Checking its version
git --version
Setup
I keep my file copies locally on a few devices and remotely at GitHub and BitBucket websites. GIT allows me to pull and push my code or text between local and remote copies. Next, I will explain how I use GIT via the command line (in Terminal) on MAC OS. I will precede every command with a comment to explain what exactly it does.
Creating remote repository
Firstly, we can go to Bitbucket or GitHub website, create our user account. It would make sense to create the first repository. To avoid confusion, I prefer to name my local and remote repositories with the same name.
On my local machine
Locally, I went to the ~/Documents/elena directory, created there folder called /tutorials and went into it:
cd /Users/my_user_name/Documents/elena
mkdir tutorials
cd tutorials
With “git init .” we initialise the GIT repository inside of the /tutorials folder:
git init .
Creating the .gitignore file to define which files should not be tracked.DS_Store and the compiled Python byte code files:
touch .gitignore
nano .gitignore
Write these two lines (you can add more file templates that you do not want to track) into .gitignore file:
.DS_Store
*.pyc
Setting up User Name and E-mail
You can see your personal e-mail address used for your commits in Git by default:
git config --global user.email
To override the default global settings for the user name and email, we can configure an individual repository with a different identity. For this, we run in the root folder of the repository the following commands:
git config user.name "My name"
git config user.email my@email.com
To change the default user identity, run the following (stored in ~/.gitconfig):
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
Adding files to a repository
Adding all files (except) of the defined in the .gitignore to the current repository:
git add .
Committing these files with the text string indicating the “Initial commit”:
git commit -m "Initial commit"
Remote repositories
Next, we define the remote repository location, which can be seen from the website of remote repository host (BitBucket, GitHub, or even on your own web server with GIT server installed):
git remote add origin git@bitbucket.org:my_user_name/tutorials.git
Pushing the locally added files into the remote location called “origin” with the master branch (this we will explain in the “Advanced” section if you are interested):
git push origin master
Congratulations! We are successfully uploaded files into the remote GIT server! Please let me know if this did not work out for you.
When you change your local files, we can commit them as follows (you can change your commit message “minor changes” as you please ):
git commit -a -m "minor changes"
In case when we update the remote repository and want to pull the changes at another device, we can use:
git pull origin master
Workflow with GIT
git init --bare --share # Creating a bare repository inside of the current folder
git clone git@bitbucket.org:my_user_name/tutorials.git # Cloning the remote repository for working with it locally
git log # Log commits
git status # Check the status
git status -u # To see Untracked Files
git diff commit1 commit2 # See differences between two commits
git diff --name-only commit1 commit2 # See the files changed between commits
mkdir tools # creating /tools directory for further
git mv git.md tools # moving the git.md file into it
git status # see the status of operation
git commit -a -m "Moved git.md into /tools" # committing the move
git add -all /code # Add all untracked files
git commit -a -m "code directory added" # Update changes
git diff # See changes, unstaged (not committed yet)
git diff --staged # See staged changes
git fetch origin # Updating my local repository with new info on Remote
git checkout -- file # Revert file to the latest commit
git remote -v # See remotes
git remote show origin # Inspecting remote
git diff origin/master # See differences between local changes and remote
git remote rename myremoterepository origin # rename remote myremoterepository to new name origin
git push origin # Pushes local changes to origin
git pull origin master # Pulls from remote and merges
git show --pretty="format:" --name-only commit_id # show files' changes in commit_id
Using SSH keys without typing in your passwords
Suppose you want to use a secure connection and private GIT repositories. In that case, it makes sense to set up the SSH private key on your local machine and configure the Bitbucket settings with the public key. The use of public and private keys will ensure a secure encrypted connection and facilitate the user login without re-entering the user password. Use ssh-keygen (MAC OS) or PuTTYgen (for Windows) for generating the SSH keys as explained in the sources below.
Using Tokens
Last but not least, in GitHub, we can use personal access tokens to authenticate from the command line. We can perform git commands via HTTPS protocol [GIT user-manual last updated in 2.16.1]. For instance, use the following commands for creating the remote connection with a web address and further cloning the repository:
git remote set-url origin https://<token>@github.com/<my_user_name>/<repository_name>
git clone https://<token>@github.com/<my_user_name>/<repository_name>
Thanks
Thanks to the authors of the following websites that helped me to learn:
GIT user-manual last updated in 2.16.1
SSH login without password in OS X and Linux
Creating a personal access token
Conclusion
We created a GIT repository, set up its connection with Github, committed and pushed changes, and learned how to get repository status and log information. In the next post, I will go into the collaborative usage of GIT with multiple users.
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youAbout Elena Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.
|