Tech.Git History
Hide minor edits - Show changes to markup
(:sourceend:)
(:sourceend:) Remove the last commit from Git history (:source lang=bash :) git reset --hard HEAD~1 (:sourceend:) If the commit has already been pushed to the remote, and you want to propagate the change (WARNING: this is bad if someone already has the commit that will be deleted into his history): (:source lang=bash :) git push -f (:sourceend:)
It will create a new commit. The reverted commit is still present in the history.
It will create a new commit. The reverted commit is still present in the history.
How to rewrite history
The git rebase command allows you to easily change a series of commits, modifying the history of your repository. You can reorder, edit, or squash commits together. Warning: Because changing your commit history can make things difficult for everyone else using the repository, it's considered bad practice to rebase commits when you've already pushed to a repository. Rebase all the commits between another branch and the current branch state (:source lang=bash :) git rebase --interactive other_branch_name (:sourceend:) Rebasing commits against a point in time To rebase the last few commits in your current branch, you can enter the following command in your shell: (:source lang=bash :) git rebase --interactive HEAD~7 (:sourceend:)
To remove a remote branch
To remove a remote branch or tag
To remove a remote tag
To remove a local tag
git push origin :refs/tags/tagtodelete
git push origin :branch_to_delete
git push origin --delete branch_or_tag_to_delete
(:sourceend:)
Work on a remote branch
(:source lang=bash :) git fetch origin git checkout -b mybranch origin/mybranch
It is better to don't use an already existing tag name to name your branch.
Note: It is better to don't use an already existing tag name to name your branch.
Some other http://mislav.uniqpath.com/2010/07/git-tips/ git tips
Some other git tips
Some other http://mislav.uniqpath.com/2010/07/git-tips/ git tips
How to synchronize a branch with the remore repository?
If the branch has no upstream: (:source lang=bash :) git pull origin v1.4-maintenance (:sourceend:) The best option is to provide the -u option when pushing to the remote to specify the upstream automatically. To easily track a remote branch from someone else (:source lang=bash :) git checkout -t origin/feature (:sourceend:) It creates and checks out "feature" branch that tracks "origin/feature".
How to push a new branch to the remote
How to push a new branch to the remote?
How to delete a remote branch/tag
To remove a remote branch
How to create a tag?
It will create an annotated tag (recommended) (:source lang=bash :) git tag -a v1.4 -m 'my version 1.4' (:sourceend:) To list tags: (:source lang=bash :) git tag git tag -l v1.4/* (:sourceend:)
How to delete a remote branch/tag?
How to delete a remote branch
How to delete a remote branch/tag
To remove a remote branch
(:sourceend:) To remove a remote tag (:source lang=bash :) git tag -d tagtodelete git push origin :refs/tags/tagtodelete
Push a new branch to the remote
How to push a new branch to the remote
Delete a remote branch
How to delete a remote branch
Push a new branch to the remote
It is better to don't use an already existing tag name to name your branch. (:source lang=bash :) git push origin newbranch (:sourceend:)
Delete a remote branch
(:source lang=bash :) git push origin :branch_to_delete (:sourceend:)
You can also check this useful post
You can also check this useful post
How to revert a commit?
It is as simple as (:source lang=bash :) git revert 5f1b62549477 (:sourceend:) It will create a new commit. The reverted commit is still present in the history.
(:sourceend:)
Remove untracked files
(:source lang=bash :) git clean -f (:sourceend:) If you want to also remove directories, run (:source lang=bash :) git clean -f -d (:sourceend:) If you just want to remove ignored files, run (:source lang=bash :) git clean -f -X (:sourceend:) If you want to remove ignored as well as non-ignored files, run (:source lang=bash :) git clean -f -x
- Then, Compare any branch from your local repository to any remote you've added.
- Then, compare any branch from your local repository to any remote you've added.
Git info
There is no git info (like svn info) command available with git :( Instead you can use following commands.
List remote URL
(:source lang=bash :) git remote -v (:sourceend:)
List branches
Remote branches: (:source lang=bash :) git branch -r (:sourceend:) Local branches: (:source lang=bash :) git branch (:sourceend:)
Git configuration
(:source lang=bash :) cat .git/config (:sourceend:)
Most recent commit
(:source lang=bash :) git —no-pager log —max-count=1 (:sourceend:)
(:sourceend:)
How to diff between a fork with its origin repository?
- First, in your forked repository, add the origin as a remote
(:source lang=bash :) git remote add <aname-refering-to-the-origin-repo> <origin-git-url> (:sourceend:)
- Fetch origin changes into your forked repository. It won't change your working copy.
(:source lang=bash :) git fetch <aname-refering-to-the-origin-repo> (:sourceend:)
- Then, Compare any branch from your local repository to any remote you've added.
(:source lang=bash :) git diff master <aname-refering-to-the-origin-repo>/master
If you are many people working on the same software, your central git repository will end with a lot of branches and merge. Indeed, developers start to work on a revision and, when they are ready to push their changes, there will probably be new revisions from other developers. The default behavior of git when pushing your changes is to define a branch, add your commits to this branch and then merge the branch back to the master. To avoid that, especially if you know there is no conflict, you can use git rebase. See Git rebasing (french).
If you are many people working on the same software, your central git repository will end with a lot of branches and merge. Indeed, developers start to work on a revision and, when they are ready to push their changes, there will probably be new revisions from other developers. The default behavior of git when pulling changes from the central repository is to define a branch, add your commits to this branch and then merge the branch back to the master. To avoid that, especially if you know there is no conflict, you can use git rebase as explained here or here (french).
Hopefully, there is a shorcut for that. You can use pull with git pull --rebase. Git can be configured to make it the default behavior: (:source lang=bash :) git config --global --bool pull.rebase true (:sourceend:)
You can also check this useful post
How to avoid many branches in your repository
How to avoid many branches in your repository?
See Git rebasing ( french).
See Git rebasing (french).
See Git rebasing (or (french)).
See Git rebasing ( french).
(:sourceend:)
(:sourceend:)
How to avoid many branches in your repository
If you are many people working on the same software, your central git repository will end with a lot of branches and merge. Indeed, developers start to work on a revision and, when they are ready to push their changes, there will probably be new revisions from other developers. The default behavior of git when pushing your changes is to define a branch, add your commits to this branch and then merge the branch back to the master. To avoid that, especially if you know there is no conflict, you can use git rebase. See Git rebasing (or (french)).
Git How-tos
Git How-tos
(:sourceend:)
(:sourceend:)
- Then, fetch origin changes into your forked repository
- Then, fetch origin changes into your forked repository
- List branches to see new available branches from the origin repository
- List branches to see new available branches from the origin repository
- Now, we can merge
- Now, we can merge
How to synchronize a fork with its origin repository?
MyA is a fork of the repository A. You use MyA for your devs and made updates. In the same time, the origin repository A still follows its way (new push, etc.). How to do if you want to (re)synchronize your forked repository with its origin?
- First, in your forked repository, add the origin as a remote
(:source lang=bash :) git remote add <aname-refering-to-the-origin-repo> <origin-git-url> (:sourceend:)
- Then, fetch origin changes into your forked repository
(:source lang=bash :) git fetch <aname-refering-to-the-origin-repo> (:sourceend:)
- List branches to see new available branches from the origin repository
(:source lang=bash :) git branch -a (:sourceend:)
- Now, we can merge
(:source lang=bash :) git merge remotes/<aname-refering-to-the-origin-repo>/<branch-name> (:sourceend:)