Most git commands should be executed at the base directory of a repo, I.e. where the .git folder is.
git config --global user.name "Arhtur Dent"
git config --global user.email "[email protected]"git clone https://gitlab.com/arthur.dent/TestRepo.git
cd TestRepo
touch README.md
git add README.md
git commit -m "add README"
git push -u origin mastercd existing_folder
git init
git remote add origin https://gitlab.com/arthur.dent/TestRepo.git
git add .
git commit -m "Initial commit"
git push -u origin mastercd existing_repo
git remote add origin https://gitlab.com/arthur.dent/TestRepo.git
git push -u origin --all
git push -u origin --tagsgit add .
git commit -m "some clever comment"git push -u origin mastergit pull origingit add .
git commit -m "some clever comment"
git checkout -- filenamegit checkout ....and move back into staging.
git reset --soft HEAD^...and delete new files
git reset HEAD --hard
git clean -fdgit branch -agit checkout -b myBranch
-- same as –
git branch myBranch # creates a branch
git checkout myBranch # switches to itMake sure you don’t have any uncommited changes, and then do:
git checkout myBranchgit remote -vMost useful for local branches that should be made up-to-date with their parent (master) branch. This will make changes to your target branch only.
First make sure that your master is up to date:
git checkout master
git pull originThen make sure that your target branch is ready for rebasing:
git checkout targetBranchNow there are two options
Option 1:
<add/commit all changes>Option 2:
git stashThen, on the target branch, perform the rebase:
git rebase masterIf you opted for Option2
git stash popScenario:
- You create branch f1 from main and make some commits to it.
- You make a pull request from f1 to main and wait for reviewers.
- While waiting you create branch f2 from branch f1 and start developing the next feature.
- Reviews arrive for the f1 pull request, you fix, commit and push. This goes on for a few rounds.
- You continue to develop on f2 in the "idle" time between reviews and fixes.
- The f1 pull request is accepted and you merge to main, optionaly squashing before merge.
- To bring f2 up to date with main with surprising ease you do:
git checkout main
git pull
git checkout f2
git rebase --onto main <commit hash>Where <commit hash> is the last hash that is common both to f1 and f2. Now f2 is up to date with main (squash and all) and should only contain the commits you made to f2. You can now continue to develop on it, push, and create a pull request to main.
This is commonly needed when you have pushed a branch to remote and then rebase it locally:
git push origin feature/add-aliens --force
Warning: Please don't do this if other people are working on the same branch. Others that have pulled this branch to their local machines will need to do some work to continue to use it, see https://stackoverflow.com/questions/9813816/git-pull-after-forced-update.
Most useful for merging your working branch into the parent (master) branch. Switch to the target branch:
git checkout masterThen do the merge:
git merge myBranchgit branch -d myBranchWhile in other branch:
git branch -m oldname newnameRename the current branch:
git branch -m newnamegit clone https://github.com/arthur.dent/myfork.git
git remote add upstream git://github.com/sourceuser/sourceoffork.git
git fetch upstream
git rebase upstream/master git stashgit stash
git stash branch myNewBranchgit checkout --theirs myFileor
git checkout --ours myFileor edit file in editor then
git add myFileor
git rm myFilegit merge --abortTwo ways of doing this
Use
git rebase -i <after-this-commit>and replace "pick" on the second and subsequent commits with "squash" or "fixup", as described in the manual.
In this example, is either the SHA1 hash or the relative location from the HEAD of the current branch from which commits are analyzed for the rebase command. For example, if the user wishes to view 5 commits from the current HEAD in the past the command is
git rebase -i HEAD~5. Credit StackOverflow
Second way of doing this
You can do this fairly easily without git rebase or git merge --squash. In this example, we'll squash the last 3 commits.
If you want to write the new commit message from scratch, this suffices:
git reset --soft HEAD~3 &&
git commitIf you want to start editing the new commit message with a concatenation of the existing commit messages (i.e. similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit:
git reset --soft HEAD~3 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash. Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit (i.e. it already has all the changes from the commits that you are about to “throw away”). Credit StackOverflow
This command deletes remote tracking branches that don't exist anymore on the remote. To see which branches would be deleted:
git remote prune --dry-run originTo actually prune the branches:
git remote prune origingit taggit tag -a v1.4 -m "my version 1.4" # Annotated tag
git tag v1.4-lw # Lightweight taggit checkout v1.4 # For viewing
git checkout -b v1.4.1 # ...branch v1.4 tagged source into new branch v1.4.1Or, all in one command:
git checkout -b v1.4.1 v1.4 # check out source with tag v1.4 and create new branch v1.4.1git log -- <filename>git cat-file -p <sha1>:./file.tex > wherever.tex