Git guide for basic and advanced git operations
Adding a file for a commit
$> git add <filename>
Removing a file from git
$> git rm <filename>
Remove file from the to be committed list of files
$> git reset HEAD <filename>
Remove the last local commit that was not yet pushed and preserve changes
$> git reset HEAD^
Remove the last local commit that was not yet pushed and revert changes
$> git reset --hard HEAD~1
(make backup first you will loose changes)
Remove untagged changes (which prevent a git rebase or a git pull)
$> git checkout -- <filename>
(this is the same as svn revert)
Change commit message (commit not yet pushed)
git commit --amend -m "New commit message"
Undo commit, but preserve changes (when you screwed up)
$> git reset --soft HEAD^
# note that the ^ is important
Rename a branch both local and remote
$> git branch new-branch-name origin/old-branch-name
$> git push origin --set-upstream new-branch-name
$> git push origin :old-branch-name
# Then, to see the old branch name, each client of the repository would have to do:
$> git fetch origin
$> git remote prune origin
Scrap all working changes
$> git reset --hard
Revert forcefully all local change (warning will even delete your local non-pushed commits!!)
$> git reset --hard origin/master $> git submodule foreach git reset --hard
WARNING: This is the most dangerous command I know in git regarding your local data, it is even more destructive than git reset --hard since it will scrap all your local commits that are not pushed yet.
Committing in a new branch
$> git checkout -b new-branch-name $> git commit -m "greatest code ever"
Push to git repository in new branch that we just created in step above
$> git push origin my-branch-name
Committing directly in master branch (safe way with rebase)
$> git commit -m "greatest code ever" $> git fetch origin $> git rebase origin/master $> git push or git push origin master
Note: rebase is useful when multiple persons are committing on the same repo and you don't want to mess up the commit history (if someone pushed while you were working and you did not pull yet). This is the workflow that causes the most simple commits (like we had in SVN).
Warning: Do not rebase if you don't have a commit operation pending to be pushed. I know it sounds silly but that's just the way git works. Otherwise if you do a rebase operation and don't have a local commit, it will appear as if you merge your branch onto the same branch instead of from the master branch, which will then cause multiple conflicts with yourself when replaying the changes during rebase. Rebasing without commit is like autoimmune disease, the branch attacks itself.
Rebase your local branch to the latest master branch (safe way)
# assumes you are on your local branch # the commit operation is mandatory if you don't want weird things to happen. Without a staged commit rebase will do a different operation and screw up history and come back later on with revenge on phantom auto-immune conflict against your own branch's code. $> git add (...) $> git commit -m "Greatest code ever" $> git fetch origin $> git rebase origin/master Note sometimes "git fetch origin" does not seems to fetch all the changes from the other branches, and I have to specifically switch to that branch then pull, then switch back to my work branch, then the rebase will work. If anyone knows why please let me know.
Fixing error cannot rebase: you have unstaged changes
$> git stash $> git rebase origin/master $> git stash pop
Another way to scrap all working changes that prevents pulling or switching branch
$> git stash $> git stash drop
This will put all working changes in stash, then delete the stash, thus deleting all changes on versioned files.
Reduce size of your git repo
$> git remote prune origin
# this will cleanup stale remote-tracking branches. In human terms, it will cleanup branches that have been removed on the git server that are taking unnecessary space.
Delete an already pushed branch
$> git push origin :my-branch-name
If you also want to delete locally :
$> git branch -d my-branch-name
Manage two parallel products sharing same code-base
View my article on correct branching and merging when using GIT (in progress...)
Recent Comments