A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Thursday, July 19, 2012


6:14:00 PM Posted by Satish Kumar , , , No comments
The following will guide you through a typical Git workflow. You will create a few files, create a local Git repository and commit your file into this repository. Afterwards, you clone the repository and push and pull some changes between the repositories. The comments (marked with #) before the commands explain the specific actions.

Open a command line / shell for the operations.

Create content

The following creates some files with some content that will later be placed under version control.

#Switch to home
cd ~/
# Create a directory
mkdir ~/repo01
# Switch into it
cd repo01
# Create a new directory
mkdir datafiles
# Create a few files
touch test01
touch test02
touch test03
touch datafiles/data.txt
# Put a little text into the first file
ls >test01 

Create repository, add and commit

# Initialize the local Git repository
git init
# Add all (files and directories) to the Git repository
git add .
# Make a commit of your file to the local repository
git commit -m "Initial commit"
# Show the log file
git log 
Every Git repository is stored in the .git folder of the directory in which the Git repository has been created. This directory contains the complete history of the repository. The .git/config file contains the local configuration for the repository.

The following will create a Git repository, add the files to the repository's index and commit the changes.

See differences via diff and commit changes

The git diff command allows the user to see the changes made. In order to test this, make some changes to a file and check what the git diff command shows to you. Then, commit the changes to the repository.

# Make some changes to the file
echo "This is a change" > test01
echo "and this is another change" > test02

# Check the changes via the diff command 
git diff

# Commit the changes, -a will commit changes for modified files
# but will not add automatically new files
git commit -a -m "These are new changes" 

Status, Diff and Commit Log

The following helps you see the current status and the list of commits in your repository.

# Make some changes in the file
echo "This is a new change" > test01
echo "and this is another new change" > test02

# See the current status of your repository 
# (which files are changed / new / deleted)
git status
# Show the differences between the uncommitted files 
# and the last commit in the current branch
git diff

# Add the changes to the index and commit
git add . && git commit -m "More chaanges - typo in the commit message"

# Show the history of commits in the current branch
git log
# This starts a nice graphical view of the changes
gitk --all 

Correction of commit messages - git amend

The git amend command makes it possible to change the last commit message.

In the above example the commit message was incorrect as it contained a typo. The following will correct this via the --amend parameter.

git commit --amend -m "More changes - now correct" 

Delete files

If you delete a file which is under version control git add .will not pick this file up. You need to use the git commit command with the -a flag or the -A flag in the git add command.

# Create a file and put it under version control
touch nonsense.txt
git add . && git commit -m "a new file has been created"
# Remove the file
rm nonsense.txt
# Try standard way of committing -> will not work 
git add . && git commit -m "a new file has been created"
# Now commit with the -a flag
git commit -a -m "File nonsense.txt is now removed"
# Alternatively you could add deleted files to the staging index via
git add -A . 
git commit -m "File nonsense.txt is now removed" 


Post a Comment