Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > Git Notes

Git Notes

Basics

In repo (local dir holding files managed by git), git categorizes files in following way:

  • unmodified files
  • modified files: modified but not staged
  • staged files: modified and staged, ie they will be added to the next commit.

unmodified ----(save)----> modified ----(stage)----> staged----(commit)----+
        ^                                                                  |
        +------------------------------------------------------------------+

Commits are done in local git repo, so it's not needed to make changes perfect. Continue do commits until the work is done, and then do a push to push all the chanes to remote repo, that is shared with the team.

Work process:

  • edit files and save them
  • stage files that you want to commit
  • commit staged files

Visual Studio:

In 'Team Explorer | Home | Changes' view, right-click on changed files, and choose 'stage' to stage them. Staging a change creates a 'Staged Changes' section in team explorer. Only these staged changes will be committed.

Enter a comment, and click 'Commit Staged' or 'Commit All' (supposing there is no stage) button to commit the changes.

If there are some minor issues in the previous commit, it can be amended: select the modified files and stage them, then add a comment and choose 'Actions | Amend Previous Commit'. Of course, you can also make another commit.

Git command line:

# stage
git add

# commit
git commit

Branch

Git branch is very cheap to create. Branches can be used to do multi-tasking works.

Steps to create a branch:

  • make sure you are at the right place based on which the branch will be created: `git status`
  • create a new branch: `git branch <branch-name>`
  • switch to the branch: `git checkout <branch-name>`
  • to work on another task in another branch: `git checkout <another-branch>`

Visual Studio:

  • 'Team Explorer | Branches'
  • right-click on 'master', and choose 'New local branch from...', then enter a branch name, and click 'Create Branch'
  • or: click 'New Branch', then enter a branch name, and choose 'master' in the combo-box below, and click 'Create Branch'.

Delete branch: right-click on a branch and choose 'Delete'. Note that the branch currently checked out cannot be deleted. You can checkout another branch and then delete it.

Deleting a local branch won't affect the remote branch. The way to delete a remote branch is to choose the branch under 'remotes/origin', and right-click on it, and choose 'Delete'.

Note that thanks for branches, you don't need to have more than one repo in your local PC: when you need to work on multiple tasks, just create several branches, and swiche among them by checking out the branch you want to work on.

Push

Push the local changes to remote branch.

# check to make sure that you are on the right branch
git status

# push the branch to remote repo
git push origin <my-branch>

# push again to add new changes in local branch to remote

# other team members can get the changes
git fetch

Visual Studio

  • 'Team Explorer | Home | Sync'
  • click 'Push' under 'Outgoing commits'
  • if this is the first push from this branch to remote, you'll see a message saying 'The current branch does not track a remote branch. Push your changes ...'. click 'Push'

Conflicts: if there are conflicts between local changes and remote repo, you must resolve them first, then push the changes to remote repo. Do a pull to get remote changes, resolve conflicts, and do push.

Create a pull request to get code reviewed

Undo local changes that have not been committed

git checkout my_branch
git reset --hard [master]

The 'master' is optional. It uses master version to replace the local version.

(iDog: When you use git flow, you should do: 'git reset --hard /origin/develop')

Resolve error merge for local changes

"error: Your local changes to the following files would be overwritten by merge"

When there are local changes, merge to this branch cannot be done to protect the changes. So you should either commit the changes (to save them), or discard them.

Commit changes:

git commit -m 'my comment'

Stash changes: stashing acts like a stack: you can push changes, then pop them in reversed order.

git stash
git stash pop

discard changes:

git reset --hard [<dir>]
# or
git checkout -t -f remote/branch
# or
git checkout -- <filename>

iDog: note that 'git reset' works on a dir, while 'git checkout --' works on a file.

Put aside changes not yet committed

'stash' is used to discard changes not yet committed, but save them for possible later use.

git stash [save]

Now you can switch branches and do something else in that branch. Later, you can switch back to the original branch and do a pop to bring back the saved changes:

git stash pop

Also, there are following related commands:

git stash list   # list stashed changes
git stash apply  # apply stashed changes, but still keep them in stashed list

iDog: Note that in IntelliJ, there is a 'shelf' functionality, it seems to be similar to this. Shelved changes can be applied to any branch.

Resolving rebase issue

Sometimes you'll get this message when failed to do rebase onto a remote branch:

"You must edit all merge conflicts and then mark them as resolved using git add"

But the problem is that all conflicts have been resolved. The time when I got this seemed to be a filename conflict: I worked in Windows, and changed a file by both the contents and filename (one letter from lower case to upper case). And this seemed to be a nightmare. What I did first was 'git rebase --skip', then resolving the conflicts again, and then 'git rm <the_file_causing_trouble>', and then 'git rebase --continue', but this won't fix it. Eventually when I decide to rm both the old and new file that caused trouble, and then 'continue', it worked, and surprisingly, the updated file was still there, although I removed it...

Someone mentioned that this was a bug in git, and the solution is very easy:

git diff
git rebase --continue

Someone also suggested that:

  • avoid having any unstaged files
  • if you don't want the unstaged files change, then discard the changes with 'git rm <the_file>'

Cherry pick

'cherry pick' is a convenient way to move some selected changes from one branch to another.

In 'Git Extensions' GUI application, the operation is as follows:

  • check out the destination branch (the branch to accept the changes)
  • click on the source branch (the branch that contains the changes to be picked), now in 'Diff' all the changes in this branch are listed.
  • choose the file that contains the changes you'd like to apply to dest branch, right-click on it, and choose "Cherry pick file's changes". Now the changes appear in the current branch as local changes.