git basics
🔗git branch -a
This will print all branches, local or remote, to see only local branches:
$ git branch
To only see remote branches:
$ git branch -r
When you clone a repo after typing git branch -a
the output will look
something like this:
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
master
is a branch in the local repository, remotes/origin/master
is a branch named master on the remote named origin
:
remotes/origin/master
| |
| name of branch
name of remote
The branch can be referred as either orign/master
or remotes/origin/master
remotes/origin/HEAD
is the default branch for the remote named origin
🔗Clean extra files
For testing within your repository do this:
$ touch foo{001..100}.tmp
To remove all the files and untracked directories you can do:
$ git clean -fd
🔗Search for commit content
To search for commit content, actual lines of source:
$ git grep <regexp> $(git rev-list --all)
or in case you get "Argument list too long":
$ git rev-list --all | xargs git grep expression
🔗merge, squash, rebase
So you are working on branch master
haven't add & commit your changes but
there has been an update on the origin/master, you would like to keep your
changes, update master to be in sync with the origin and later in in one single
commit once you are done working submit your changes.
So if you are in master
branch:
$ git fetch -p
The -p is for prune and clean unused branches
Now save your changes by creating a new branch:
$ git checkout -b my-changes
Add and commit your changes:
$ git add .
or
$ git commit -a -m "some changes"
To update master:
$ git checkout master
And then:
$ git pull
To continue working on your changes go back to your branch:
$ git checkout my-changes
You can work here commit etc, but once you are done if you would like to just
have 1 single commit, you can squash
your commits, for example to squash your
latest 6 commits, run this git log --pretty=oneline --abbrev-commit
:
1 * 2018-03-28 dd34521 (HEAD -> my-changes) 5 <nbari> (1 second ago)
2 * 2018-03-28 f31a830 4 <nbari> (8 seconds ago)
3 * 2018-03-28 15a6af0 3 <nbari> (18 seconds ago)
4 * 2018-03-28 9aaf8b3 2 <nbari> (24 seconds ago)
5 * 2018-03-28 452b764 1 <nbari> (29 seconds ago)
6 * 2018-03-28 447dae2 my changes for foo bar <nbari> (42 seconds ago)
| * 2018-03-28 4dcbf9b (origin/master, origin/HEAD) --- remote origin master --- <nbari> (9 minutes ago)
| * 2018-03-28 b8c35f4 Update date.txt <nbari> (37 minutes ago)
| * 2018-03-28 fae7a75 udpate <nbari> (49 minutes ago)
| * 2018-03-28 7ff5384 Update date.txt <nbari> (77 minutes ago)
|/
Create an alias gl
with this:
git log --decorate --graph --oneline --all --date=short --pretty=format:'%C(bold blue)%ad%Creset %C(yellow)%h%Creset%C(auto)%d%Creset %s %C(dim magenta)<%an >%Creset %C(dim green)(%ar)%Creset'"
To squash the latest commits:
$ git rebase -i HEAD~6
This will open your editor probably with something like this:
pick 447dae2 my changes for foo bar
s 452b764 1
s 9aaf8b3 2
s 15a6af0 3
s f31a830 4
s dd34521 5
# Rebase 5302aa0..dd34521 onto 5302aa0 (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Once you are done if you would like to merge (fast forward) you could rebase
your my-changes
branch with master
, so beeing in my-changes
branch, do:
$ git rebase master
If you have conflics fix them git rebase --continue
Then to merge your changes on master:
$ git checkout master
And finally:
$ git merge my-changes
🔗git pull --rebase
In some cases you may not want to create a branch to keep your changes and just would like to have the lates changes, so if some cases this can be achived by doing:
$ git pull --rebase
🔗reset local repository branch to be just like remote repository
In case you want have the repositories in sync:
$ git fetch origin
$ git reset --hard origin/master
You can do the same for a local branch:
$ git reset --hard master
Set fork to be the same as upstream:
git remote add upstream https://github.com/some_user/some_repo
git fetch upstream
git checkout master
git reset --hard upstream/master
git push origin master --force