This is the command to update branch pointer to a specific commit:
$ git update-ref refs/heads/[branch name] [commit hash]
This is the command to update branch pointer to a specific commit:
$ git update-ref refs/heads/[branch name] [commit hash]
I have a case whereby I regret to commit a code on top of a commit. Since I have not yet push the change, I want to change my commit based on another parent commit. This what I did to make it happen:
$ git rebase --onto [new parent hash] [old parent hash]
After I executed this command, there are some conflicted files. I resolved the conflicts, and continue the rebase using this command:
$ git rebase --continue
By now, new commit created on that parent commit with my changes applied. Thanks to git rebase.
Terminal is very useful tool on our IntelliJ Idea editor. Unfortunately, this terminal does not recognize git commands since it is configured by default to run cmd.exe. When we are already get used to use git command on the terminal, running git command on this terminal could save us from opening another git command line outside our editor.
Follow this step to configure our editor: 1. Click File > Settings... 2. Browse to Tools > Terminal 3. Change Shell path from "git.exe" to "[path to git installation]\bin\sh.exe --login" 4. Click OK.
Now when you open new terminal on IntelliJ Idea, it will recognize git commands.
Use this command to stop tracking some files from git repository. Please add the file / folder to .gitignore to prevent the file getting staged and tracked again.
$git rm --cached [filename]
This is git command we can execute from command line to browse when there is no editor.
Once we get hash of a commit (eg. from git log command) we can look into that commit information using git cat-file
First, we can use git log to obtain the commit hash: $git log --oneline --decorate --graph --all
output:
* 117826d (HEAD, origin/master, master) Add .gitignore * af606a6 Initial commit
To check if the hash is a commit: $git cat-file -t 117826d
output:
commit
To check the commit detail: $git cat-file -p 117826d
output:
tree a3f84d1bfbe30dbf340f8fc34ea6dcb643afbf4b parent af606a6a54070289384ad57aa763ce36020469e2 author User1430399773 +0800 committer User 1430399826 +0800
To check the filel under this commit: $git cat-file -p a3f84d1b
output:
100644 blob cd3d2f4b1b1855bcd3d83966ac7907a2f46bca6c .gitignore 100644 blob 072e5e57717b1c6097806209c1565ca68f2813d5 build.gradle 040000 tree 666e30ac8fabbd7cc7bbaccff94314b3f3f85327 gradle 100644 blob 91a7e269e19dfc62e27137a0b57ef3e430cee4fd gradlew 100644 blob 8a0b282aa6885fb573c106b3551f7275c5f17e8e gradlew.bat 100644 blob b318381fe4b0c5e7a460b4295ece4d55f6f9a85d helloworld.iml 100644 blob 31258af8faeca28e68d996886f89523756ee70e2 settings.gradle 040000 tree 26ed19d88a76b22b8c5e5a0f2339fc08759afca6 src
We can do the same for the file hash: $git cat-file -p cd3d2f4b
output:
.idea/ .gradle/ build/
I often use this command to show the log tree from command line:
git log --oneline --graph --decorate --all
This is command to show remote branches:
$ git remote
origin
$ git remote show origin
user@example.com's password:
* remote origin
Fetch URL: user@example.com:/home/user/git/project.git
Push URL: user@example:/home/user/git/project.git
HEAD branch: master
Remote branches:
develop tracked
feature/feature1 tracked
master tracked
release tracked
Local branches configured for 'git pull':
develop merges with remote develop
feature/feature1 merges with remote feature/feature1
master rebases onto remote master
release merges with remote release
Local refs configured for 'git push':
develop pushes to develop (up to date)
feature/feature1 pushes to feature/feature1 (up to date)
master pushes to master (up to date)
release pushes to release (up to date)
This is command to checkout remote branch to local, and set the local branch to track remote branch:
git checkout --track origin/branchname
It is often that we started a project in our local laptop, and we want this project secured in our server. I know, it may not possible to execute git clone
command from server to clone the project from your local computer due to firewall or at that moment your laptop does not have public IP like your server does.
Before pushing the project to server, I assumed that we have created git repository in the server:
$ mkdir projectname.git
$ cd projectname.git
$ git init --bare
Or just:
$ git init --bare projectname.git
From our local, execute these command to push the git local repository to the server:
$ git remote add origin ssh://user@server.com:port/path/to/projectname.git
$ git push origin master
Now we have our project safe in the server. We can safely continue our work in local laptop, we can clone to other laptop if required, and push the changes to server anytime we want.