Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Wednesday, September 6, 2023

How to add remote to GIT repo

Git remote provide information to sync with a Git repository to do stuff like git fetch, git push, git pull.

This information is stored in .git/config. In the case of a new directory, that does not have git, start by configuring it with the command git init. Managing remote can be done by the following commands.

Adding a remote

git remote add <name> <url>


Example

git remote add git_user1 user1@git.myserver.local/repo/package.git

git remote add ssh_user1 ssh://user1@10.1.1.100/repo/package.git


View the current remote and detailed remote information

git remote

git remote -v


Remove a remote

git remote remove <name>

git remote rm <name>


Example

git remote rm remote_user1


Push changes of the specific remote

git push <name>


Example

git push ssh_user1

or to push as a new branch

git push -u ssh_user1 new_branch


Show log of the remote

git remote show <name>


Example

git remote show ssh_user1


Show status of CURRENT branch

git status


Change url of an existing remote

git remote set-url <name> <new url>


Example

git remote set-url remote_user1 ssh://user1@10.1.1.23/repo/package.git


Tuesday, March 28, 2023

GIT tag and retag

Working with GIT allows tagging specific points along the repository that have some importance. Commonly, tag is used when a version is released. Here are examples of listing tags, adding and deleting a tag.

List tags

List tag on local

git tag -l "v1.*"

git tag

List tag on repository

git ls-remote --tags

Display details of a tag

git show v1.0.2


Add tag to current branch

git tag -a v1.0.2 HEAD -m "Update for version 1.0.2"

git push origin --tags

Tag can be added to a specific commit.

git tag -a v1.0.2 f8c3501-m "Update for version 1.0.2" 


Retagging

This requires deleting current tag, then publish changes to remote repository.

git tag -d v1.0.2

git push origin :refs/tags/v1.0.2


Wednesday, December 29, 2021

Howto clone existing GIT repository

There are many cases where remote git repositories provide a central location to store files such as source codes. The command 'clone' is used to retrieve a remote git repository. Depends on how a user is provided access to the server, by http, ssh or other methods. 

Here is how its done on Centos Linux, where it defaults to create a folder which is the same name as the repository.

cd /var/www

git clone ssh://nicholas@remoteserver:22/repo/tboxmy.git

Another approach is to create in our empty folder myproject

git clone ssh://nicholas@remoteserver:22/repo/tboxmy.git myproject


How to add user to the remote

git remote add newuser ssh://nicholas@remoteserver:22/repo/tboxmy.git

View remote users

git remote -v 

Friday, September 3, 2021

Howto compare 2 GIT branch

 The benefits of GIT is being able to work in different branches from the master codebase. Commonly a new feature being developed would be published to a new branch for review before being merged to the master branch. Once it is merged to the master branch, the new branch would be removed from the remote site.

Before merge to the master branch, it is possible to compare between the 2 branches. This will allow a check on files changes and where are the code changes. Here, lets see how to retrieve the new branch from remote, then compare changes made. The git command format in general would be;

git option <arguments>

Assumptions:

  • code base is in the branch master
  • remote name is origin
  • new remote branch is FEATURE
  • git version 1.8 and 2.19

Retrieve a remote branch

This section is more of a recap where the remote branch does not exist on your local machine. The option branch can use -b or -t depending on your needs. There is the example with -t where it creates the required branch locally and fetch its contents from remote.

git branch -t origin/FEATURE

git checkout FEATURE


View latest commit in the branch

git log

git log --oneline

Compare difference between branch

git diff master..FEATURE

View only the files that changed

git diff --name-status master..FEATURE

This describes use with TWO dots in the diff option. There is a THREE dots with the diff, but display results that compares the common between the 2 branches. More commonly, in cases that feature is already merged to master, it will produce no results.

Compare difference between commits

Displaying differences in commit between branches is ideal to identify developer notes in the commit between branches at level of commits.

git log master..FEATURE

There are many options to display log difference. Here is one example

git log --oneline --graph --decorate --abbrev-commit master..FEATURE

Compare difference in a file

When working to identify where a specific file has changed between two branches, just mention the relative path of the file

git diff master..FEATURE -- public/js/app.js


Compare with difftool

Here is an option to launch external editors to view the diff results. 

git difftool master..FEATURE

If required, a full directory diff can be done by adding the argument -d. This works fine on our linux systems, but on a GUI environment, the graphical code editors can be configured and activated with the argument -g.


Thats the basics to compare two branches using the diff and log options. Comparing existing branch or commits can be done with shorthand notations, which is much easier and I did not mention them here. This will help to ensure a smoother management of source codes.




Thursday, July 16, 2020

Create a local Git Repository

How to create a remote GIT repository and connect to it.

Git Repository provides storage of project files managed by git. There is a set of notes maintained at Notes on Git.

To setup a project with support for Git, use the following command, in the same folder as the project root folder.

git init

The basic configuration for a username and email is required, to access any remote git repositories. 

git config --global user.name "Tboxmy"
git config --global user.email "username@gmail.com"

There are many more configurations. Another example, for MS Windows PC that wants to preserve its end of line format, we can use

git config --global core.autocrlf false

In order to have a centralised repository, where one or many users can send changes to their files from other computers, we use a remote repository. This centralised repository is known as Bare Repository where users can send their project files over. Use the following command.

git init --bare <repo_name>.git

Then users can connect to this remote repository, and keep their files updated there.
git remote add <remote_name> <remote_repo_url>

In order to update remote repository with changes in files, a user can PUSH files there

git push -u <remote_name> <local_branch_name>


Wednesday, September 12, 2018

Git and undo last commit

Updating a remote git repository would mean all other users having a same source code. Sometimes, a local commit may be done and its found that there are changes that should not be push to the remote. The solution, remove not needed files then move to the previous commit on the local machine.

The command is
$ git reset --hard HEAD~1

Unstage any other files with the command
$ git reset


Friday, July 14, 2017

Source code revision control with Git


Version control system is used to manage versions of files and is used widely with programming source codes. Common examples are SVN and Git.

Here is an example of the using GIT with Heroku (Working with GIT2). It demonstrates the git command for;
  • clone
  • branch
  • checkout
  • commit
  • status

Basic commands to manage a local version control repository

Typical process to prepare a folder where files are to be tracked/staged uses the commands init, status and add. It would be good to have created a folder with 4 or 5 files and try out the following commands.
 # Initialise Git repository   
  $ git init   
     
  # Identify status and which files are being track or untracked.   
  $ git status   
     
  # Add all files to the repository   
  $ git add .  
   
 # Identify files are being track as staged  
  $ git status   

At this time, a folder to manage the repository is created with the name ".git". Have a look at content of the files in this folder.

Filename: .git/HEAD
ref: refs/heads/master

Filename: .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly

Filename: .git/description
Unnamed repository; edit this file 'description' to name the repository.


To remove a file from being tracked.
 # Remove a file from list of staged files  
 $ git rm --cached somefile.txt.  

Process to place files into the repository.
 # Commit files to repository  
 $ git commit -m "Place comments here"  
   
 $ git status  

Retrieve tracked file from the repository. The latest thats found in the repository, that is.
 # Checkout a file from the repository  
 $ git checkout programming.txt  
   
 $ git status  

Cache and other files that should not be kept in the repository or tracked, can be listed in a file ".gitignore" within that folder. Example to ignore the folder "temp" and file "nofile.txt".

Filename: .gitignore
/temp
nofile.txt

end

Blog Archive