Say you have two branches, branchA and branchB.
Assume that in branchA you have
file1.txt
file3.txt
whereas in branchB you have
file2.txt
file3.txt
Say you want to make some change that will be common to both branchA and branchB; that is, for example, you want to add the same file to them, so that branchA shall become
file1.txt
file3.txt
file4.txt
and branchB shall become
file2.txt
file3.txt
file4.txt
To do this, you first need to commit to either branch, say branchA.
$ git checkout branchA
$ # write file4.txt
$ git add file4.txt
$ git commit file4.txt -m 'add file4'
Next, you just need to clone the very last commit using git cherry-pick
$ git checkout branchB
$ git cherry-pick branchA
If you have more branches, then simply repeat the above cherry-pick steps to branchC, branchD, and so forth.
Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts
Sunday, October 1, 2017
Tuesday, February 14, 2017
Fundamental Git Concepts and Useful Git Commands
Summary of fundamental git concepts:
- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit
Summary of most frequently used commands in git:
# show current status
$ git status
# show commits in chronological order [with statistics]
$ git log [--stat]
# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2
# show difference between two commits
$ git diff commit1 commit2
# show difference between the specified commit and its direct parent
$ git show commit_hash
# load specified commit
$ git checkout commit_hash
# load specified branch
$ git checkout branch_name
# save current work as a new branch
$ git checkout -b new_branch_name
# show [hidden] branches
$ git branch [-a]
# create a new branch
$ git branch new_branch_name
# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2
# add files to staging area
$ git add .
# remove files from staging area
$ git reset
- repository: a collection of all commits and logs
- commit: a snapshot of the files at the time it was created
- branch: label for a commit and its subsequent commits
- master: the main branch
- HEAD: current commit
- origin: remote repository
- staging area: intermediate step before creating a commit
Summary of most frequently used commands in git:
# show current status
$ git status
# show commits in chronological order [with statistics]
$ git log [--stat]
# show branching graph between two branches [represented in single lines]
$ git log --graph [--oneline] branch1 branch2
# show difference between two commits
$ git diff commit1 commit2
# show difference between the specified commit and its direct parent
$ git show commit_hash
# load specified commit
$ git checkout commit_hash
# load specified branch
$ git checkout branch_name
# save current work as a new branch
$ git checkout -b new_branch_name
# show [hidden] branches
$ git branch [-a]
# create a new branch
$ git branch new_branch_name
# add branch2 into branch1; i.e., branch1 will incorporate branch2
$ git merge branch1 branch2
# add files to staging area
$ git add .
# remove files from staging area
$ git reset
How to Push an Existing Repository to Git Server
Say you have a Github account or a local git server where you want to publish your work either publicly or privately as a bare repository. This post will explain how to create a bare repository on Github or git server. Refer here for official Github documentation.
First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.
If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git
Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.
Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"
Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY
where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.
Finally, push your current repository to the remote repository:
$ git push -u origin master
Now, the two repositories should be in sync!
First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.
If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git
Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.
Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"
Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY
where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.
Finally, push your current repository to the remote repository:
$ git push -u origin master
Now, the two repositories should be in sync!
Saturday, October 15, 2016
Android Studio .GITIGNORE File
This is my personal Android Studio .gitignore file:
.gradle
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
*.apk
*.ap_
*.dex
*.class
bin/
gen/
Thumbs.db
.DS_Store
.classpath
.project
*.iml
.idea
.gradle
build/
obj/
/*/out
/*/*/build
/*/*/production
*.iws
*.ipr
*~
*.swp
*.so
*.a
.gradle
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
*.apk
*.ap_
*.dex
*.class
bin/
gen/
Thumbs.db
.DS_Store
.classpath
.project
*.iml
.idea
.gradle
build/
obj/
/*/out
/*/*/build
/*/*/production
*.iws
*.ipr
*~
*.swp
*.so
*.a
Wednesday, October 12, 2016
How to Apply .gitignore File To an Existing Repository
When you update or add .gitignore file to not track unnecessary files, such as object files and binary files, this is what you can do to apply the change to an existing repository.
$ git commit -m "outstanding changes before applying .gitignore"
$ git rm -r --cached .
$ git add .
$ git commit -m "applying .gitignore"
That's it!
$ git commit -m "outstanding changes before applying .gitignore"
$ git rm -r --cached .
$ git add .
$ git commit -m "applying .gitignore"
That's it!
Saturday, July 16, 2016
How to Import Existing Git Repository and Upload into Git Server
Say you have a local git repository called my_project in your home folder ~ of your local machine, and you'd like upload this into your personal git server. Here is how you can do this. For more info, please refer to here.
First, make sure that your git repository folder exists in the working directory.
$ pwd
/home/your_username/
$ ls
my_project/
...
Then, create a bare git repository from the existing repository
$ git clone --bare my_project my_project.git
You should now have the cloned bare repository
$ ls
my project/
my project.git/
...
Noe that .git ending is a conventional indication of server repository.
Next, copy the cloned repository to your server, usually through scp
$ scp -r my_project.git git_server_user@your.git.server:/git/
Here, you will need to replace git_server_user, your.git.server, and /git/ with the appropriate server username, server address, and destination path in the server, respectively.
That should be it. Now, to clone from your server, simply run the following from a client system
$ git clone ssh://git_server_user@your.git.server:/git/my_project.git
Of course, you may want to delete the bare repository from the local machine (not the server).
$ rm -rf my_project.git
That should do it!
Monday, July 4, 2016
Use Git to Version Control DOCX Files for Windows / Cygwin
Git is a wonderful tool for version control of various texts and documents, such as source code. Unfortunately however, git by default will not be able to produce diff result for Microsoft's word document format (.docx) files. In this post, I will go over how to use git diff docx files on Cygwin. This post is based on this.
Let's see what happens when we try to use git for docx files. Download Cygwin x86 for 32-bit system or Cygwin x64 for 64-bit system from the Cygwin download page.
Run the installer file to install it on the system. Just go through installation with default settings until Select Packages section. Here, search for and install unzip (Archive), git (Devel), and vim (Editors). Note that I will assume the default Cygwin installation directory to be c:\cygwin. Note that if you download 64-bit version, the default directory will be c:\cygwin64.
Let's run Cygwin. You should see bash terminal. Say you have your docx files saved in c:\Users\unixnme\Documents\docx folder. To change directory into this folder, run
$ cd /cygdrive/c/Users/unixnme/Documents/docx
Let's assume that there is a docx file called test.docx in the folder.
$ ls
test.docx
Let's initiate a git repository and commit test.docx file.
$ git init
Initialized empty Git repository in /cygdrive/c/Users/unixnme/Documents/docx.git
$ git add test.docx
$ git config --global user.name your_name
$ git config --global user.email your_email@address.com
$ git commit -m "initial commit"
[master (root-commit) 90cf9f2] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100755 test.docx
Next, make any changes to test.docx file, save, and run git diff command:
$ git diff
diff --git a/test.docx b/test.docx
index 41c6200..ce81ceb 100755
Binary files a/test.docx and b/test.docx differ
Let's see what happens when we try to use git for docx files. Download Cygwin x86 for 32-bit system or Cygwin x64 for 64-bit system from the Cygwin download page.
Run the installer file to install it on the system. Just go through installation with default settings until Select Packages section. Here, search for and install unzip (Archive), git (Devel), and vim (Editors). Note that I will assume the default Cygwin installation directory to be c:\cygwin. Note that if you download 64-bit version, the default directory will be c:\cygwin64.
Let's run Cygwin. You should see bash terminal. Say you have your docx files saved in c:\Users\unixnme\Documents\docx folder. To change directory into this folder, run
$ cd /cygdrive/c/Users/unixnme/Documents/docx
Let's assume that there is a docx file called test.docx in the folder.
$ ls
test.docx
Let's initiate a git repository and commit test.docx file.
$ git init
Initialized empty Git repository in /cygdrive/c/Users/unixnme/Documents/docx.git
$ git add test.docx
$ git config --global user.name your_name
$ git config --global user.email your_email@address.com
$ git commit -m "initial commit"
[master (root-commit) 90cf9f2] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100755 test.docx
Next, make any changes to test.docx file, save, and run git diff command:
$ git diff
diff --git a/test.docx b/test.docx
index 41c6200..ce81ceb 100755
Binary files a/test.docx and b/test.docx differ
As expected, git diff will complain that test.docx is a binary file. Now, let us enable git diff for docx files.
First, we need to install docx2txt utility. Download docx2txt-1.4.tgz file from here to your download folder, say c:\Users\unixnme\Downloads. Extract the files by running tar command
$ cd /cygdrive/c/Users/unixnme/Downloads
$ tar vxzf docx2txt-1.4.tgz
Go into the extracted directory and enable execution flag for windows installer batch file.
$ cd docx2txt-1.4
$ chmod u+x WInstall.bat
We are ready to install it. Enter the installation folder path and Perl path as follows:
$ ./WInstall.bat
Welcome to command line installer for docx2txt.
Where should the docx2txt tool be installed? Specify the location
without surrounding quotes.
Installation Folder :c:\cygwin\bin
Please specify fully qualified paths to utilities when requested.
Perl.exe is required for docx2txt tool as well as for this installation.
Path to Perl.exe : c:\cygwin\bin\perl.exe
Continuing with simple installation ....
Copying script files to "c:\cygwin\bin" ....
Please adjust perl, unzip and cakecmd paths (as needed) in
"c:\cygwin\bin\docx2txt.bat" and "c:\cygwin\bin\docx2txt.config"
Note that I simply installed docx2txt in c:\cygwin\bin, but you may want to change it as desired. The default perl executable is installed in the cygwin/bin directory, which in this case is c:\cygwin\bin\perl.exe. Note that if you have installed the 64-bit version of cygwin, then you will need to replace cygwin with cygwin64 for paths. If it asks for unzip and cakecmd paths, simply leave them blank as they are not necessary.
Next, we need to create /usr/bin/docx2txt file by
$ vim /usr/bin/docx2txt
with the following content.
#!/bin/bash
docx2txt.pl $1 -
Make sure to set the executable flag
$ chmod u+x /usr/bin/docx2txt
Finally, go back to the git repository folder and create .gitattributes file to apply filter for docx files
$ cd /cygdrive/c/Users/unixnme/Documents/docx
$ echo "*.docx diff=word" > .gitattributes
Finally, edit git config file to run /usr/bin/docx2txt script file for word filter
$ git config diff.word.textconv docx2txt
When everything is successful, you should see diff result for the modified docx file like a regular plain text file
$ git diff
diff --git a/test.docx b/test.docx
index 41c6200..ce81ceb 100755
--- a/test.docx
+++ b/test.docx
@@ -1 +1 @@
-This is a test file
+This is a test file, which I have modified after commit.
Enjoy git on your Microsoft Word documents!
Saturday, July 2, 2016
How to Setup Git Server on Mac OS X
In this tutorial, I will go over the instructions to setup a git server on Mac OS X. Here, I will assume that the server IP address is 12.34.56.78.
First, you will need to add a user named git, into which client machines will ssh into. To do so,
1. Open up System Preferences -> Users & Groups.
2. You may need to click the Lock image to make any changes.
3. Click on the + button to add a user.
4. Add a standard user whose full name and account name is git. Enter password.
5. Click on Create User button.
Next, you will need to allow ssh login for user git from client machines. To do so,
1. Open up System Preferences -> Sharing
2. Check Remote Login box.
3. Add the user git in the Allow access for field by clicking on the + button.
From your client system, make sure that you can remote log into the server's git account.
$ ssh git@12.34.56.78 -p 22
Note that here I am assuming that you are using the port 22 to access the server. Depending on your router or firewall configuration, the port number may differ.
If you can successfully log into the account, then you are pretty much done. Let's assume that you will want to create a repository named Project. From the git account from the server or through ssh, run
$ pwd
/Users/git
$ git init --bare Project.git
This will create a bare Project repository on the server, which clients can clone, push to, and pull from. The location of this repository in this case is /Users/git/Project.git, as you can see from pwd command above. To clone this repository from a client, you will need to run the following from the client
$ git clone ssh://git@12.34.56.78:22/Users/git/Project.git
You will need to enter the password for the server's git account, as if you are remote logging into it through ssh. You should now have a personal git server.
Note: If you are concerned with safety, you may want to disable password login and instead only allow public key authentication. I will cover how to do so in the future post.
First, you will need to add a user named git, into which client machines will ssh into. To do so,
1. Open up System Preferences -> Users & Groups.
2. You may need to click the Lock image to make any changes.
3. Click on the + button to add a user.
4. Add a standard user whose full name and account name is git. Enter password.
5. Click on Create User button.
Next, you will need to allow ssh login for user git from client machines. To do so,
1. Open up System Preferences -> Sharing
2. Check Remote Login box.
3. Add the user git in the Allow access for field by clicking on the + button.
From your client system, make sure that you can remote log into the server's git account.
$ ssh git@12.34.56.78 -p 22
Note that here I am assuming that you are using the port 22 to access the server. Depending on your router or firewall configuration, the port number may differ.
If you can successfully log into the account, then you are pretty much done. Let's assume that you will want to create a repository named Project. From the git account from the server or through ssh, run
$ pwd
/Users/git
$ git init --bare Project.git
This will create a bare Project repository on the server, which clients can clone, push to, and pull from. The location of this repository in this case is /Users/git/Project.git, as you can see from pwd command above. To clone this repository from a client, you will need to run the following from the client
$ git clone ssh://git@12.34.56.78:22/Users/git/Project.git
You will need to enter the password for the server's git account, as if you are remote logging into it through ssh. You should now have a personal git server.
Note: If you are concerned with safety, you may want to disable password login and instead only allow public key authentication. I will cover how to do so in the future post.
Sunday, December 27, 2015
How to Compile the First Version of Git on Linux
Here, we will learn how to use git, the widely-used version control system initially developed by Linus Torvalds for Linux kernel development.
First, obtain the software git by
$ sudo apt-get install git -y
Next, clone git repository from GitHub by
$ git clone https://github.com/git/git
When download is complete, it will have create a folder, so let's enter.
$ cd git
Well, we could simply build the current version of git, but for the sake of learning how to use git and getting the taste of what it is capable of doing, let's check out the very first version of git written by Linus in 2 weeks.
To do this, we first need to search for different commits:
$ git log --oneline
Now, scroll the to the very bottom by pressing 'end' key several times.When you reach the end, press 'q' key. You should see the following output:
e83c516 Initial revision of "git", the information manager from hell
This is the very first version (present in the repository). Let's check out this version by
$ git checkout e83c516
Now, you will see that the files in the folder have been de-dated back to 2005! How do I know? Well, run
$ git log
Because we will make changes and commit this soon, we will create a branch here, so simply run
$ git checkout -b ver1.0
Let's take a look at what branches we have now
$ git branch
Note that ver1.0 branch has been added, where master is the current version of git.
There are only a handful of files here at this very early stage. Feel free to read Linus' code. When done, let's compile. First, you will need to install libraries:
$ sudo apt-get install libssl-dev zlib1g-dev -y
When you run
$ make
It will probably complain about some define references. We will need to edit the Makefile file and include some libraries:
$ vim Makefile
Add -lz and -lcrypto parameters for LIBS flag, so line #11 should read
LIBS= -lssl -lz -lcrypto
Save the file and let's try to compile again
$ make
This should compile! Now, let's commit the change to the local repository:
$ git add Makefile
$ git commit -m "edit Makefile to compile in Ubuntu 14.04LTS"
To go back to the current version of git, we check out the master branch$ git checkout master
To go back to the compilable first version of git, simply run
$ git checkout ver1.0
I will go through some more advanced git tutorials later.
First, obtain the software git by
$ sudo apt-get install git -y
Next, clone git repository from GitHub by
$ git clone https://github.com/git/git
When download is complete, it will have create a folder, so let's enter.
$ cd git
Well, we could simply build the current version of git, but for the sake of learning how to use git and getting the taste of what it is capable of doing, let's check out the very first version of git written by Linus in 2 weeks.
To do this, we first need to search for different commits:
$ git log --oneline
Now, scroll the to the very bottom by pressing 'end' key several times.When you reach the end, press 'q' key. You should see the following output:
e83c516 Initial revision of "git", the information manager from hell
This is the very first version (present in the repository). Let's check out this version by
$ git checkout e83c516
Now, you will see that the files in the folder have been de-dated back to 2005! How do I know? Well, run
$ git log
Because we will make changes and commit this soon, we will create a branch here, so simply run
$ git checkout -b ver1.0
Let's take a look at what branches we have now
$ git branch
Note that ver1.0 branch has been added, where master is the current version of git.
There are only a handful of files here at this very early stage. Feel free to read Linus' code. When done, let's compile. First, you will need to install libraries:
$ sudo apt-get install libssl-dev zlib1g-dev -y
When you run
$ make
It will probably complain about some define references. We will need to edit the Makefile file and include some libraries:
$ vim Makefile
Add -lz and -lcrypto parameters for LIBS flag, so line #11 should read
LIBS= -lssl -lz -lcrypto
Save the file and let's try to compile again
$ make
This should compile! Now, let's commit the change to the local repository:
$ git add Makefile
$ git commit -m "edit Makefile to compile in Ubuntu 14.04LTS"
To go back to the current version of git, we check out the master branch$ git checkout master
To go back to the compilable first version of git, simply run
$ git checkout ver1.0
I will go through some more advanced git tutorials later.
Subscribe to:
Posts (Atom)