Difference between revisions of "Basic git"

From Notes_Wiki
m
m
Line 202: Line 202:
</pre>
</pre>
The above requires privileges to overwrite history (rebase) selected branch on git server.
The above requires privileges to overwrite history (rebase) selected branch on git server.
If the wrong commit was pull from origin on other hosts where branch is not changed use:
<pre>
git checkout <other-branch>  #EG master
git branch -D <affected-branch>
git checkout <affected-branch>
</pre>
This will ensure that old affected branch with wrong commits is discarded and a fresh copy of affected branch is pulled from orign.





Revision as of 08:50, 22 September 2019

<yambe:breadcrumb self="Basic git">Git|Git</yambe:breadcrumb>

Basic git

Git config

When using git for first time we should set global config values that can be used in all git projects using

    git config --global user.name "Saurabh Barjatiya"
    git config --global user.email "saurabh@sbarjatiya.com"
    git config --global push.default current
    git config --global color.ui true
    git config --global color.status auto
    git config --global color.branch auto 
    git config --global core.editor vim 

The above commands mostly end up creating a file '~/.gitconfig' which includes:

[user]
        name = Saurabh Barjatiya
        email = saurabh@sbarjatiya.com


We can use

    git config user.name "Saurabh Barjatiya"
    git config user.email "saurabh@sbarjatiya.com"

to configure a project specific name and email address. This configuration goes in .git/config file of the project


Initializing a new repository

To initialize a directory as working copy of repository we can use command:

git init

when current working directory is parent most directory of folder which we want to initialize as git repository.


Committing changes

To commit changes for every new / modified file that we want to go as part of commit we need to run:

git add <filename>

Note that this is different from most centralized version control systems where we do add only once and future commits automatically check-in modifications of the file. In git we can modify a file and choose not to include modifications in commit by not using git add on modified file. Once all the files that are intended to be committed are added we can use:

git commit

to commit the changes.


If we want to add all the modified files which have been committed in some earlier version for committing then we can use:

git add -u

which automatically adds all the modified files (not the new files). We can combine commands 'git add -u; git commit ' to single ' git commit -a' command which does both adding of modified files to index and commiting without adding new files.


Revert changes before commit

If a file has been modified since last commit and changes have been staged for next commit using 'git add' then compare it with last commit using:

git diff <last-commit-id> <file-name>

If the staged changes need to be unstaged then use:

git reset HEAD <file-name>

After that revert file to state of last commit using:

git checkout <file-name>

At any time to discard all changes since last commit use:

git reset --hard HEAD

Warning with above all unsaved changes would be lost permanently.

Refer:


Status of changes

We can use:

git status

which lists three different types of files: 1. Files that were modified and added to index. These modifications will become part of next commit. 2. Files that were modified but not added. These modification will not become part of commit if performed now before making any other changes. 3. Files that are not being traced by git.


Seeing commit logs

We can use:

git log

command to see log of commits to current repository. If we are interested only in commit logs of a specific file then we can use

git log <file_name>

We can also see how many lines were changed due to each commit using --stat option as

git log --stat <filename>


Seeing differences

We can use git diff to get differences since last commit, difference wrt some branch or some tag using :

git diff  [  |<branch_name> | <tag_name> | HEAD ]

or

git diff --stat

command to see differences since last commit. We can use git diff to see difference between current files and some other branch etc.


Tagging current commit

Adding tags

We can tag current commit with help of

git tag <tagname>


Listing tags

We can list tags matching given pattern with

git tag -l <pattern>

If pattern is not specified then all tags are listed.


Deleting tags

We can delete tag using

git tag -d <tagname>


Specifically ignoring files

We can ask git to specifically ignore files by mentioned their names in '.gitignore' file. This ignore file can understand patterns and applies to all sub-folders and files. We can create .gitignore in any folder and then it will apply to its subsequent sub-folders and files.

Sample .gitignore may have lines:

# Lines starting with '#' are considered comments.
# Ignore any file named foo.txt.
foo.txt
# Ignore (generated) html files,
*.html
# except foo.html which is maintained by hand.
!foo.html
# Ignore objects and archives.
*.[oa]


Restoring file from previous commit

We can restore file to earlier state using:

  1. First use 'git log <file>' to get different commits made to file
  2. Choose the commit to which you want to restore. You can use 'git diff <commit> <file>' to diff file at time of given commit to current file
  3. Reset the file to given commit state as per index using 'git reset <commit> <file>'
  4. Restore older version of file from index using 'git checkout <file>'


Undo one more more commit

To undo one commit use:

git reset --soft HEAD~1

Here, soft will preserve changes as uncommited changes since last commit. To understand this in detail refer https://www.git-tower.com/learn/git/faq/undo-last-commit

After this do normal operations and make a new commit.

Now if wrong commit was even pushed to parent, then parent has some other commits compared to local branch. Thus, we need to force overwrite origin branch using:

git push origin <branch-name> --force

The above requires privileges to overwrite history (rebase) selected branch on git server.

If the wrong commit was pull from origin on other hosts where branch is not changed use:

git checkout <other-branch>   #EG master
git branch -D <affected-branch>
git checkout <affected-branch>

This will ensure that old affected branch with wrong commits is discarded and a fresh copy of affected branch is pulled from orign.


A good tutorial on git is located at http://www.vogella.com/articles/Git/article.html


<yambe:breadcrumb self="Basic git">Git|Git</yambe:breadcrumb>