Using Git

  • Post author:
  • Post last modified:November 14, 2024
  • Reading time:7 mins read

Git is a source code management system originally developed by Linus Torvalds in 2005. Git can be used locally on a user’s system and also in a distributed environment with a Git server hosting the repository from which the users can push and pull commits. Here, the top 15 Git commands are listed for easy reference and use.

1.0 Install Git

On an Ubuntu Linux system, Git can be installed with the command,

$ sudo apt-get install git

2.0 Set Identity

Before using Git, the user's identity (name, email) has to be set

$ git config --global user.email "user1@example.com"
$ git config --global user.name "Joe Bloggs"

3.0 Initialize git repository

The first job for putting a project under source code management is to create a repository.

$ cd project-root-directory
$ git init
Initialized empty Git repository in /home/user1/project-root-directory/.git/

4.0 Status

The status of the working tree can be seen with the git status command.

$ git status

5.0 Add files, commit changes

We can add file contents to the staging area with the git add command. The changes can be committed to the repository with the git commit command.

$ cd project-root-directory
$ # See the differences between the HEAD and the current files.
$ git diff
$ # Check what files would be added to the staging area (the index).
$ git add -n .
$ Add files to the staging area.
$ git add .
$ # See the differences between the HEAD and the files in the staging area.
$ git diff --cached
$ # Commit the changes.
$ git commit -m "Minor modification."
$ # Add and commit in a single command.
$ git commit -a -m "Minor modification."

6.0 Undo changes

We can undo the changes made since checking in the last version, or, since the last commit.

$ # Discard changes in the working directory.
$ git checkout -- <file> ...
$ # Discard changes in all the files in the working directory.
$ git checkout -- .
$ # To unstage file(s)
$ git reset HEAD <file> ...
$ # To unstage all files.
$ git reset HEAD .
$ # remove all working directory and staged changes
$ git reset --hard
$ # remove untracked files
$ git clean -fd

7.0 Add tags and see log

We can add tags with the git tag command. We can see the log of commits with the git log command.

$ # Add the tag vm.n for version m.n
$ git tag -a vm.n -m "Version m.n"
$ # See the log of commits to the repository
$ git log --decorate --all --oneline --graph
$ # Or,
$ git log --pretty=format:'%C(yellow)%h %Cgreen%ad %C(cyan)%an%Cgreen%d %Creset%s' --date=short

8.0 Branching

Branching helps in creating a parallel line of development. One can branch from the main version, do some development and / or testing and optionally merge the work back in the main version.

$ # List branches
$ git branch
$ # Create a branch
$ git branch <branch-name>
$ # Switch to <branch-name>
$ git checkout <branch-name>

After working on the branch, we can add files in the branch to the staging area and commit. After the commit, we may merge the branch in the master.

$ # Switch to the master.
$ git checkout master
$ # Merge <branch-name> in the master.
$ git merge <branch-name>
$ # Delete branch.
$ git branch -d <branch-name>
$ # Force switch to master, discarding any changes that are not committed.
$ git checkout -f master

9.0 Reverting to an earlier commit

Suppose you are not happy with the current version and you want to go to an earlier commit. First, check the log.

$ git log --decorate --all --oneline --graph
* c98863e (HEAD -> master) cosmetic bugs fixed
* 8482dc8 (tag: v1.4, origin/master, origin/HEAD) minor bugs fixed
* 472dc10 critical bugs fixed
  ...

We can use the commands given in para 7 to go to the last commit, c98863e. But suppose we wish to go back two versions before that, say, to 472dc10. We can see what work we will lose which we committed in 8482dc8 and c98863e commits by using the git diff command.

$ git diff 472dc10 c98863e
diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser
index 9151372..a2fc214 100644
Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ
+++ b/.idea/gradle.xml
  ...

After reviewing the diff output, we can go 472dc10 commit by giving the commands,

$ git reset --hard 472dc10
$ git push origin master --force

The commits, 8482dc8 and c98863e and the associated history is lost forever.

10.0 Remote Git Server

A remote Git server provides a copy of the repository so that the others can collaborate on a project.

11.1 Create a remote repository

On the remote system, the following commands need to be executed.

$ cd repository
$ mkdir project-root-directory
$ cd project-root-directory
$ git --bare init
Initialized empty Git repository in /home/user1/repository/project-root-directory/

You can also configure the server for the Git protocol for read-only access.

11.0 Pushing the repository from local system to the remote server

We can add the remote repository name and URL at the local system.

$ # Add remote repository. 
$ git remote add origin git-user@git-server:repository/project-root-directory
$ git remote add u1 user1@git-server:repository/project-root-directory
$ # Show remote repositories.
$ git remote -v
$ # change URL of remote repository
$ git remote set-url origin [USER@]HOST:absolute-path-of repository

After that, updates to the local repository can be pushed to the remote repository.

$ git push origin master --tags

12.0 Setting up local repository from remote repository

We can get the data from the remote repository to the local repository using the git fetch command. Using the git merge command, we can merge the updates to the local repository with the current branch.

$ # Initialize an empty repository
$ git init
$ git remote add origin git@git-server:/home/git/hello
$ git fetch 
$ # Initial checkout.
$ git checkout master

We can periodically update our local repository and then the current branch with the commands,

$ git fetch
$ git merge

13.0 Clone the repository at the local system

Using the read-only Git protocol, we can clone the repository on the local system.

$ git clone git://git-server:repository/project-root-directory

Karunesh Johri

Software developer, working with C and Linux.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments