Sunday, July 22, 2012

Git examples





I have been working on gitlab recently and needed to write down some git examples, so here is what I came up with:


 
 
 
To roll back files from your last commit

If the commit ID is ce4c1645cad94cc69221607201bd86c8f33b6cc0, run the following command


git reset --hard ce4c1645cad94cc69221607201bd86c8f33b6cc0

git reset without the --hard option resets the commit history but not the files. With the --hard option also files in working tree are reset

To then push these files back to the master branch
 


git commit --all
git push origin master --force 

To recovery one file from a previous commit
To find out what files have been deleted from the previous commit, run


git log --diff-filter=D --summary

This should give you information about which commit the file was delete from.
Once the commit ID is found, run the following command:


git checkout ce4c1645cad94cc69221607201bd86c8f33b6cc0 -- version_one_file_2

where ce4c1645cad94cc69221607201bd86c8f33b6cc0 is the commit ID and -- version_one_file_2 is the file

then run the following commands:


git commit
git push


Adding a FILE to a git repo

This is a very simple example but an important concept. Commands can vary depending on this example on what you are trying to do

create a new file


touch this_is_a_new_file

add the file to the git repo


git add this_is_a_new_file

this adds the file to the current working git directory


git commit this_is_a_new_file

This commits the file to a git snapshot


git push origin master

this pushes the files to the master branch


I deleted a file 3 commits ago. How do I recover that file

How to reproduce the example

1 - clone git repo


see above :)

2 - create a new file A


touch A
git add A

3 - commit and push


git commit A 
git push origin master

4 - create a new file B and delete A


touch B 
git add B 
git rm A

5 - commit and push


git commit
git push origin master

6 - create a new file C


touch C
git add C

7 - commit and push


git commit
git push origin master

8 - try to find file A

run this command


git log --diff-filter=D --summary

it will show you what files have been deleted.

You need to checkout the previous branch; ie the commit before the deletion
You then need to make sure the file you are recoverying is the one you want
You then need to switch back to the master branch
You then need to add the file to the master branch
You then need to commit the recovered file
You then need to push everything to the repo


so..


git log --diff-filter=D --summary $git checkout 
 cdeb3d757f3adcc346da2ab171a825c113bdb50b~1 A 

# note the ~1 rolls back to the previous commit. ~2 would go back 2 commits etc.. 

this just grabs that file in that commit and does not change branches 

git branch 

# check what branch you are on 

git add A

# to add A back to the master branch 

git commit A 
git push origin master


Create a branch, adding file that does not conflict and adding the files to the master branch

Run git branch to show which branch you are using


git branch
* master

This shows you are using the master branch

To add a branch, run the following command


git branch new_branch
git branch 

* master new_branch

the new_branch branch has been created.

You have not checkout the new branch yet. First, the following command shows the files in the master branch.


git ls-files

INSTALL
README
config.xml


To switch to the new branch and to add a new file, run the following commands


git checkout new_branch 

Switched to branch 'new_branch'

git branch

master
* new_branch

notice the * is now next to the new_branch. This is to show which branch you are working on.

You can check the files are the same as the master branch by running


git ls-files 

INSTALL
README
config.xml

Add a new file to the new_branch


touch file_added_to_branch
git add file_added_to_branch
git commit file_added_to_branch
git push origin new_branch

run the following to list the files in the new_branch


git ls-files 

INSTALL
README 
config.xml 
file_added_to_branch

switch to the master branch and list the git repo


git ls-files 
INSTALL 
README 
config.xml

Notice that the file file_added_to_branch is not in the master repo. To add this file, you can merge the new_branch to the master repo by running the following command


git merge new_branch 

Updating 76a5cab..30c41cc Fast-forward 0 files changed, 0 insertions(+), 0 deletions(-) 

create mode 100644 new_branch_file

Note: You have to be on the master branch to merge to the master. We changed to the master branch before running the above git merge command

push the files to the master branch on the git server

git push origin master

The following shows the branch files and how to delete any branches


git ls-files 

INSTALL
README
config.xml 
new_branch_file 

git branch 

* master 
new_branch

git branch -d new_branch 

Deleted branch new_branch (was 30c41cc). $ git branch * master

Delete the branch on the gitlab server


$git push origin :new_branch 

To git@puppet-gitlab:oliver.leach/puppet-cressex-lab.git 
 [deleted] new_branch

1 comment:

  1. Nice post! Thanks for SHARING a good stuff related to DevOps, Explination is good, nice Article
    anyone want to learn advance devops tools or devops online training

    DevOps Training

    ReplyDelete